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/Checkout/Model/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Checkout/Model/DeliveryEstimationImporter.php
<?php
/**
 * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Cnc
 * Krzysztof Majkowski <[email protected]>
 */

namespace Cnc\Checkout\Model;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\Serialize\SerializerInterface;

class DeliveryEstimationImporter
{
    /**
     * @var \Magento\Framework\Filesystem\Directory\ReadFactory
     */
    private $readFactory;

    /**
     * @var \Magento\Framework\Filesystem
     */
    private $filesystem;

    /**
     * @var SerializerInterface
     */
    private $serializer;

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

    /**
     * DeliveryEstimationImporter constructor.
     * @param ResourceConnection $resourceConnection
     * @param \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory
     * @param \Magento\Framework\Filesystem $filesystem
     * @param SerializerInterface $serializer
     */
    public function __construct(
        ResourceConnection $resourceConnection,
        \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
        \Magento\Framework\Filesystem $filesystem,
        SerializerInterface $serializer
    ) {
        $this->readFactory = $readFactory;
        $this->filesystem = $filesystem;
        $this->serializer = $serializer;
        $this->resourceConnection = $resourceConnection;
    }

    public function import(\Magento\Framework\DataObject $object)
    {
        $importFieldData = $object->getFieldsetDataValue('import');
        if (empty($importFieldData['tmp_name'])) {
            return $this;
        }

        $csvFile = $importFieldData['tmp_name'];

        $tmpDirectory = ini_get('upload_tmp_dir') ? $this->readFactory->create(ini_get('upload_tmp_dir'))
            : $this->filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
        $path = $tmpDirectory->getRelativePath($csvFile);
        $stream = $tmpDirectory->openFile($path);

        // check and skip headers
        $headers = $stream->readCsv();
        if ($headers === false || count($headers) < 3) {
            $stream->close();
            throw new \Magento\Framework\Exception\LocalizedException(__('Please correct File Format.'));
        }

        while (false !== ($csvLine = $stream->readCsv())) {
            if (empty($csvLine)) {
                continue;
            }

            $row = $this->getRow($csvLine, $headers);
            $this->importRow($row);
        }
        $stream->close();
    }

    protected function getRow($row, $headers)
    {
        $data = [];
        $data['country_code'] = $row[0];
        $data['country_name'] = $row[1];

        $tmp = [];
        for ($i = 2; $i < count($row); $i++) {
            $v = $row[$i];
            $tmp[trim($headers[$i])] = trim($v);
            if ($v == '') {
                $tmp[$headers[$i]] = '0';
            }
        }
        $data['estimation'] = $this->serializer->serialize($tmp);

        return $data;
    }

    protected function importRow($row)
    {
        $connection = $this->resourceConnection->getConnection();
        $connection->insertOnDuplicate('cnc_delivery_estimation', $row);
    }
}

Spamworldpro Mini