CustomersFixture.php 3.86 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Setup\Fixtures;

use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
use Magento\Setup\Model\Customer\CustomerDataGenerator;
use Magento\Setup\Model\Customer\CustomerDataGeneratorFactory;
use Magento\Setup\Model\FixtureGenerator\CustomerGenerator;

/**
 * Generate customers based on profile configuration
 * Supports the following format:
 * <customers>{customers amount}</customers>
 * Customers will have normal distribution on all available websites
 *
 * Each customer will have absolutely the same data
 * except customer email, customer group and customer addresses
 *
 * @see \Magento\Setup\Model\FixtureGenerator\CustomerTemplateGenerator
 * to view general customer data
 *
 * @see \Magento\Setup\Model\Customer\CustomerDataGenerator
 * if you need dynamically change data per each customer
 *
 * @see \Magento\Setup\Model\Address\AddressDataGenerator
 * if you need dynamically change address data per each customer
 *
 * @see setup/performance-toolkit/config/customerConfig.xml
 * here you can change amount of addresses to be generated per each customer
 * Supports the following format:
 * <customer-config>
 *      <addresses-count>{amount of addresses}</addresses-count>
 * </customer-config>
 *
 * @see setup/performance-toolkit/profiles/ce/small.xml
 */
class CustomersFixture extends Fixture
{
    /**
     * @var int
     */
    protected $priority = 70;

    /**
     * @var CustomerGenerator
     */
    private $customerGenerator;

    /**
     * @var CustomerDataGeneratorFactory
     */
    private $customerDataGeneratorFactory;

    /**
     * @var array
     */
    private $defaultCustomerConfig = [
        'addresses-count' => 2
    ];

    /**
     * @var CollectionFactory
     */
    private $collectionFactory;

    /**
     * @param FixtureModel $fixtureModel
     * @param CustomerGenerator $customerGenerator
     * @param CustomerDataGeneratorFactory $customerDataGeneratorFactory
     * @param CollectionFactory $collectionFactory
     */
    public function __construct(
        FixtureModel $fixtureModel,
        CustomerGenerator $customerGenerator,
        CustomerDataGeneratorFactory $customerDataGeneratorFactory,
        CollectionFactory $collectionFactory
    ) {
        parent::__construct($fixtureModel);

        $this->customerGenerator = $customerGenerator;
        $this->customerDataGeneratorFactory = $customerDataGeneratorFactory;
        $this->collectionFactory = $collectionFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function execute()
    {
        $customersNumber = $this->getCustomersAmount();
        if (!$customersNumber) {
            return;
        }

        /** @var CustomerDataGenerator $customerDataGenerator */
        $customerDataGenerator = $this->customerDataGeneratorFactory->create(
            $this->getCustomersConfig()
        );

        $fixtureMap = [
            'customer_data' => function ($customerId) use ($customerDataGenerator) {
                return $customerDataGenerator->generate($customerId);
            },
        ];

        $this->customerGenerator->generate($customersNumber, $fixtureMap);
    }

    /**
     * @return int
     */
    private function getCustomersAmount()
    {
        return max(0, $this->fixtureModel->getValue('customers', 0) - $this->collectionFactory->create()->getSize());
    }

    /**
     * {@inheritdoc}
     */
    public function getActionTitle()
    {
        return 'Generating customers';
    }

    /**
     * {@inheritdoc}
     */
    public function introduceParamLabels()
    {
        return [
            'customers' => 'Customers'
        ];
    }

    /**
     * @return array
     */
    private function getCustomersConfig()
    {
        return $this->fixtureModel->getValue('customer-config', $this->defaultCustomerConfig);
    }
}