Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Cnc/Sales/Console/Command/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Sales/Console/Command/UpdateTmpTables.php
<?php
/**
 * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Cnc
 * Radosław Stępień <[email protected]> <[email protected]>
 */
namespace Cnc\Sales\Console\Command;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Module\Dir;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\Phrase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UpdateTmpTables extends Command
{
    const FILES_TO_LOAD = [
        'orders.csv' => 'cnc_tmp_orders_migration',
        'orders_products.csv' => 'cnc_tmp_orders_migration_item',
        'orders_totals.csv' => 'cnc_tmp_orders_migration_totals',
        'orders_status_history.csv' => 'cnc_tmp_orders_migration_status'
    ];

    /**
     * @var File
     */
    protected $fileSystem;

    /**
     * @var Reader
     */
    protected $moduleReader;

    /**
     * @var ResourceConnection
     */
    protected $resourceConnection;

    /**
     * UpdateTmpTables constructor.
     * @param File $fileSystem
     * @param Reader $moduleReader
     * @param ResourceConnection $resourceConnection
     * @param string|null $name
     */
    public function __construct(
        File $fileSystem,
        Reader $moduleReader,
        ResourceConnection $resourceConnection,
        string $name = null
    ) {
        parent::__construct($name);
        $this->fileSystem = $fileSystem;
        $this->moduleReader = $moduleReader;
        $this->resourceConnection = $resourceConnection->getConnection();
    }

    /**
     * @return void
     */
    protected function configure()
    {
        $this->setName('sales:orders_import:update_tmp_tables');
        $this->setDescription('Updates tmp tables data');
        parent::configure();
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        try {
            foreach (self::FILES_TO_LOAD as $fileName => $dbName) {
                $data = $this->getFileData('Cnc_Sales', $fileName);
                $tableName = $this->resourceConnection->getTableName($dbName);
                // Clear tmp table before every import to keep fresh data
                $this->resourceConnection->truncateTable($tableName);
                if (!empty($data)) {
                    $chunkedData = array_chunk($data, 5000);
                    foreach ($chunkedData as $toInsertData) {
                        $this->resourceConnection->insertMultiple($tableName, $toInsertData);
                    }
                }
            }
        } catch (\Exception $exception) {
            var_export($exception->getMessage());
        }
    }

    /**
     * @param string $moduleName
     * @param string $filename
     * @return array
     * @throws LocalizedException
     * @throws FileSystemException
     */
    public function getFileData(string $moduleName, string $filename)
    {
        $data = [];
        $filePath = $this->moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, $moduleName) . '/data/' . $filename;
        $file = $this->fileSystem->fileOpen($filePath, "r");
        if (!$file) {
            throw new LocalizedException(new Phrase('Unable to open %1 file', [$filename]));
        }
        $header = $this->fileSystem->fileGetCsv($file, 0, ";");
        $header[] = 'is_migrated';
        while ($row = $this->fileSystem->fileGetCsv($file, 0, ";")) {
            //set additional columns 'is_migrated' as false by default during import.
            $row[] = 0;
            $currentRow = array_combine($header, $row);
            foreach ($currentRow as $key => $value) {
                $currentRow[$key] = trim($value);
            }
            $data[] = $currentRow;
        }

        return $data;
    }
}

Spamworldpro Mini