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/vendor/extmag/shiplab/Helper/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/extmag/shiplab/Helper/ImportShippingMethods.php
<?php
/**
 * Copyright © Extmag. All rights reserved.
 */

namespace Extmag\Shiplab\Helper;

use Exception;
use Extmag\Shiplab\Model\ShippingMethods;
use Extmag\Shiplab\Model\ShippingMethodsFactory;
use Extmag\Shiplab\Model\ShippingMethodsRepository;
use Magento\Config\Model\Config\Backend\File;
use Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\DB\TransactionFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Driver\File as FileOrig;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Magento\MediaStorage\Model\File\UploaderFactory;

class ImportShippingMethods extends File
{
    /**
     * @var File
     */
    protected $file;
    /**
     * @var ShippingMethodsRepository
     */
    protected $shippingMethodsRepository;
    /**
     * @var ShippingMethodsFactory
     */
    protected $shippingMethods;
    /**
     * @var ManagerInterface
     */
    protected $messageManager;
    /**
     * @var TransactionFactory
     */
    protected $transactionFactory;

    /**
     * ImportShippingMethods constructor.
     *
     * @param Context $context
     * @param Registry $registry
     * @param ScopeConfigInterface $config
     * @param TypeListInterface $cacheTypeList
     * @param UploaderFactory $uploaderFactory
     * @param RequestDataInterface $requestData
     * @param Filesystem $filesystem
     * @param FileOrig $file
     * @param ShippingMethodsRepository $shippingMethodsRepository
     * @param ShippingMethodsFactory $shippingMethods
     * @param ManagerInterface $messageManager
     * @param TransactionFactory $transactionFactory
     * @param AbstractResource|null $resource
     * @param AbstractDb|null $resourceCollection
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        ScopeConfigInterface $config,
        TypeListInterface $cacheTypeList,
        UploaderFactory $uploaderFactory,
        RequestDataInterface $requestData,
        Filesystem $filesystem,
        FileOrig $file,
        ShippingMethodsRepository $shippingMethodsRepository,
        ShippingMethodsFactory $shippingMethods,
        ManagerInterface $messageManager,
        TransactionFactory $transactionFactory,
        AbstractResource $resource = null,
        AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $registry,
            $config,
            $cacheTypeList,
            $uploaderFactory,
            $requestData,
            $filesystem,
            $resource,
            $resourceCollection,
            $data
        );
        $this->file = $file;
        $this->shippingMethodsRepository = $shippingMethodsRepository;
        $this->shippingMethods = $shippingMethods;
        $this->messageManager = $messageManager;
        $this->transactionFactory = $transactionFactory;
    }

    /**
     * Process additional data before save config
     *
     * @return $this
     * @throws LocalizedException
     */
    public function beforeSave()
    {
        parent::beforeSave();
        $value = $this->getValue();
        if (!empty($value)) {
            $rows = [];
            $file = $this->file->fileOpen($this->_getUploadDir() . '/' . $value, 'r');
            try {
                $i = 0;
                $fields = [];
                $sizeOfFields = 0;
                while ($i < 999999999) {
                    $resultRow = $this->file->fileGetCsv($file, 0, ',', '"');
                    if ($sizeOfFields == 0) {
                        if (empty($resultRow)) {
                            break;
                        }

                        $fields = $resultRow;
                        $sizeOfFields = count($fields);
                    } elseif (count($resultRow) == $sizeOfFields) {
                        foreach ($resultRow as $key => $val) {
                            $rows[$i][$fields[$key]] = $val;
                        }

                        $i++;
                    } else {
                        $this->messageManager->addWarningMessage(__('Error export on line ' . $i));
                        $rows = [];
                        break;
                    }
                }
            } catch (Exception $e) {
                // do nothing
            }

            $this->file->fileClose($file);

            if (!empty($rows)) {
                try {
                    $transaction = $this->transactionFactory->create();
                    foreach ($rows as $key => $row) {
                        /**
                         * @var ShippingMethods $model
                         */
                        $model = $this->shippingMethods->create();
                        $model->setData($row);
                        $transaction->addObject($model);
                        //$this->shippingMethodsRepository->save($model);
                    }

                    $transaction->save();
                    $this->messageManager->addSuccessMessage(__('Shipping methods imported successfully'));
                } catch (Exception $e) {
                    $this->messageManager->addWarningMessage(
                        __('Error of import shipping methods') . ': ' . $e->getMessage());
                }
            }
        }

        return $this;
    }

    protected function _getAllowedExtensions()
    {
        return ['csv'];
    }
}

Spamworldpro Mini