Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomersFixture.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Fixtures;
8 
9 use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
13 
42 {
46  protected $priority = 70;
47 
51  private $customerGenerator;
52 
56  private $customerDataGeneratorFactory;
57 
61  private $defaultCustomerConfig = [
62  'addresses-count' => 2
63  ];
64 
68  private $collectionFactory;
69 
76  public function __construct(
78  CustomerGenerator $customerGenerator,
79  CustomerDataGeneratorFactory $customerDataGeneratorFactory,
80  CollectionFactory $collectionFactory
81  ) {
82  parent::__construct($fixtureModel);
83 
84  $this->customerGenerator = $customerGenerator;
85  $this->customerDataGeneratorFactory = $customerDataGeneratorFactory;
86  $this->collectionFactory = $collectionFactory;
87  }
88 
92  public function execute()
93  {
94  $customersNumber = $this->getCustomersAmount();
95  if (!$customersNumber) {
96  return;
97  }
98 
100  $customerDataGenerator = $this->customerDataGeneratorFactory->create(
101  $this->getCustomersConfig()
102  );
103 
104  $fixtureMap = [
105  'customer_data' => function ($customerId) use ($customerDataGenerator) {
106  return $customerDataGenerator->generate($customerId);
107  },
108  ];
109 
110  $this->customerGenerator->generate($customersNumber, $fixtureMap);
111  }
112 
116  private function getCustomersAmount()
117  {
118  return max(0, $this->fixtureModel->getValue('customers', 0) - $this->collectionFactory->create()->getSize());
119  }
120 
124  public function getActionTitle()
125  {
126  return 'Generating customers';
127  }
128 
132  public function introduceParamLabels()
133  {
134  return [
135  'customers' => 'Customers'
136  ];
137  }
138 
142  private function getCustomersConfig()
143  {
144  return $this->fixtureModel->getValue('customer-config', $this->defaultCustomerConfig);
145  }
146 }
__construct(FixtureModel $fixtureModel, CustomerGenerator $customerGenerator, CustomerDataGeneratorFactory $customerDataGeneratorFactory, CollectionFactory $collectionFactory)