CustomerGenerator.php 4.61 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Setup\Model\FixtureGenerator;

use Magento\Customer\Api\Data\CustomerInterface;

/**
 * Customer generator
 */
class CustomerGenerator
{
    /**
     * @var EntityGeneratorFactory
     */
    private $entityGeneratorFactory;

    /**
     * @var CustomerTemplateGenerator
     */
    private $customerTemplateGenerator;

    /**
     * @var \Magento\Framework\App\ResourceConnection
     */
    private $resourceConnection;

    /**
     * @var \Magento\Framework\DB\Adapter\AdapterInterface
     */
    private $connection;

    /**
     * @param EntityGeneratorFactory $entityGeneratorFactory
     * @param CustomerTemplateGenerator $customerTemplateGenerator
     * @param \Magento\Framework\App\ResourceConnection $resourceConnection
     */
    public function __construct(
        EntityGeneratorFactory $entityGeneratorFactory,
        CustomerTemplateGenerator $customerTemplateGenerator,
        \Magento\Framework\App\ResourceConnection $resourceConnection
    ) {
        $this->entityGeneratorFactory = $entityGeneratorFactory;
        $this->customerTemplateGenerator = $customerTemplateGenerator;
        $this->resourceConnection = $resourceConnection;
    }

    /**
     * Generate entities
     *
     * @param int $customers
     * @param array $fixtureMap
     * @return void
     */
    public function generate($customers, array $fixtureMap)
    {
        $this->entityGeneratorFactory
            ->create([
                'entityType' => CustomerInterface::class,
                'customTableMap' => [
                    'customer_entity' => [
                        'handler' => $this->getCustomerEntityHandler()
                    ],

                    'customer_address_entity' => [
                        'handler' => $this->getCustomerAddressEntityHandler()
                    ]
                ],
            ])->generate(
                $this->customerTemplateGenerator,
                $customers,
                function ($customerId) use ($fixtureMap) {
                    $fixtureMap['customer_data'] = call_user_func($fixtureMap['customer_data'], $customerId);
                    return $fixtureMap;
                }
            );

        $this->addDefaultAddresses();
    }

    /**
     * Creates closure that is used
     * to replace default customer data with data from fixture
     *
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
     * @return \Closure
     */
    private function getCustomerEntityHandler()
    {
        return function ($entityId, $entityNumber, $fixtureMap, $binds) {
            return array_map(
                'array_merge',
                $binds,
                array_fill(0, count($binds), $fixtureMap['customer_data']['customer'])
            );
        };
    }

    /**
     * Creates closure that is used
     * to replace default customer address data with data from fixture
     *
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
     * @return \Closure
     */
    private function getCustomerAddressEntityHandler()
    {
        return function ($entityId, $entityNumber, $fixtureMap, $binds) {
            return array_map(
                'array_merge',
                array_fill(0, count($fixtureMap['customer_data']['addresses']), reset($binds)),
                $fixtureMap['customer_data']['addresses']
            );
        };
    }

    /**
     * Set default billing and shipping addresses for customer
     *
     * @return void
     */
    private function addDefaultAddresses()
    {
        $this->getConnection()->query(
            sprintf(
                '
                    update `%s` customer
                    join (
                        select 
                            parent_id, min(entity_id) as min, max(entity_id) as max
                        from `%s`
                        group by parent_id
                    ) customer_address on customer_address.parent_id = customer.entity_id
                    set
                      customer.default_billing = customer_address.min,
                      customer.default_shipping = customer_address.max
                ',
                $this->resourceConnection->getTableName('customer_entity'),
                $this->resourceConnection->getTableName('customer_address_entity')
            )
        );
    }

    /**
     * @return \Magento\Framework\DB\Adapter\AdapterInterface
     */
    private function getConnection()
    {
        if (null === $this->connection) {
            $this->connection = $this->resourceConnection->getConnection();
        }

        return $this->connection;
    }
}