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/ImportCustomerAddressRelation.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Radosław Łańcucki <[email protected]>
 * @copyright Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com)
 */
declare(strict_types=1);

namespace Cnc\Customer\Setup\Patch\Data;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class ImportCustomerAddressRelation extends \Kaliop\Core\Model\Import\AbstractImport implements DataPatchInterface
{
    private const FILENAME = 'customer_address.csv';
    private const CSV_DELIMITER = ',';

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

    /**
     * ImportCustomerAddressRelation constructor.
     * @param File $fileSystem
     * @param DirectoryList $directoryList
     * @param Reader $moduleReader
     * @param ResourceConnection $resourceConnection
     */
    public function __construct(
        File $fileSystem,
        DirectoryList $directoryList,
        Reader $moduleReader,
        ResourceConnection $resourceConnection
    ) {
        parent::__construct($fileSystem, $directoryList, $moduleReader);
        $this->resourceConnection = $resourceConnection;
    }

    public static function getDependencies(): array
    {
        return [
            ImportCustomer::class,
            ImportCustomerAddress::class
        ];
    }

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

    public function apply(): void
    {
        $this->importCustomerAddressRelation();
    }

    private function importCustomerAddressRelation(): void
    {
        $data = $this->loadImportData();
        $defaultAddresses = [];
        $addresses = [];

        $customerIds = array_unique(array_column($data, 'customers_id'));
        $customers = $this->getCustomers($customerIds);
        $customerAddresses = $this->getAddresses($customerIds);

        foreach ($data as $address) {
            $customerId = $address['customers_id'];
            //On previous platform there were only one default address defined
            $defaultAddressId = $address['customers_default_address_id'];
            $addressId = $address['address_book_id'];

            //Do not process not imported Customers
            if (!isset($customers[$customerId])) {
                continue;
            }

            $customerEntityId = $customers[$customerId]['entity_id'];

            //Process only imported Default Address
            if (isset($customerAddresses[$customerId][$defaultAddressId])) {
                $defaultAddresses[$customerId] = [
                    'entity_id' => $customerEntityId,
                    'default_billing' => $customerAddresses[$customerId][$defaultAddressId]['entity_id'],
                    'default_shipping' => $customerAddresses[$customerId][$defaultAddressId]['entity_id']
                ];
            }

            //Process only imported Address
            if (isset($customerAddresses[$customerId][$addressId])) {
                $addresses[] = [
                    'entity_id' => $customerAddresses[$customerId][$addressId]['entity_id'],
                    'parent_id' => $customerEntityId
                ];
            }
        }

        $connection = $this->resourceConnection->getConnection();
        foreach ($defaultAddresses as $defaultAddress) {
            $connection->insertOnDuplicate(
                $connection->getTableName('customer_entity'),
                $defaultAddress,
                ['default_billing', 'default_shipping']
            );
        }

        foreach ($addresses as $address) {
            $connection->insertOnDuplicate(
                $connection->getTableName('customer_address_entity'),
                $address,
                ['parent_id']
            );
        }
    }

    /**
     * @return array
     * @throws \Magento\Framework\Exception\FileSystemException
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    private function loadImportData(): array
    {
        $data = [];

        $handle = $this->openFile('Cnc_Customer', self::FILENAME);
        $header = $this->fileSystem->fileGetCsv($handle, 0, self::CSV_DELIMITER);

        while ($row = $this->fileSystem->fileGetCsv($handle, 0, self::CSV_DELIMITER)) {
            $data[] = array_filter(array_combine($header, $row));
        }

        return $data;
    }

    /**
     * @param array $customerIds
     * @return array
     */
    private function getCustomers(array $customerIds): array
    {
        $connection = $this->resourceConnection->getConnection();

        $select = $connection->select()
            ->from($connection->getTableName('customer_entity'), ['customer_id', 'entity_id'])
            ->where("customer_id IN (?)", $customerIds);

        return $connection->fetchAssoc($select);
    }

    /**
     * @param array $customerIds
     * @return array
     */
    private function getAddresses(array $customerIds): array
    {
        $connection = $this->resourceConnection->getConnection();

        $select = $connection->select()
            ->from(
                $connection->getTableName('customer_address_entity'),
                ['address_id', 'customer_id', 'entity_id']
            )
            ->where("customer_id IN (?)", $customerIds);

        $addresses = [];
        foreach ($connection->fetchAssoc($select) as $addressId => $address) {
            $customerId = $address['customer_id'];
            $entityId = $address['entity_id'];

            if (!isset($addresses[$customerId])) {
                $addresses[$customerId] = [];
            }

            $addresses[$customerId][$addressId] = [
                'entity_id' => $entityId
            ];
        }

        return $addresses;
    }
}

Spamworldpro Mini