Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerDataGenerator.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  private $config;
17 
21  private $addressDataGenerator;
22 
26  private $groupCollectionFactory;
27 
31  private $customerGroupIds;
32 
38  public function __construct(
39  \Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory,
40  \Magento\Setup\Model\Address\AddressDataGenerator $addressDataGenerator,
41  array $config
42  ) {
43  $this->groupCollectionFactory = $groupCollectionFactory;
44  $this->addressDataGenerator = $addressDataGenerator;
45  $this->config = $config;
46  }
47 
54  public function generate($customerId)
55  {
56  return [
57  'customer' => [
58  'email' => sprintf('user_%[email protected]', $customerId),
59  'group_id' => $this->getGroupIdForCustomer($customerId)
60  ],
61 
62  'addresses' => $this->generateAddresses(),
63  ];
64  }
65 
71  private function getGroupIdForCustomer($customerId)
72  {
73  if (!$this->customerGroupIds) {
74  $this->customerGroupIds = $this->groupCollectionFactory->create()->getAllIds();
75  }
76 
77  return $this->customerGroupIds[$customerId % count($this->customerGroupIds)];
78  }
79 
87  private function generateAddresses()
88  {
89  $addresses = [];
90  $addressesCount = $this->config['addresses-count'];
91 
92  while ($addressesCount) {
93  $addresses[] = $this->addressDataGenerator->generateAddress();
94  $addressesCount--;
95  }
96 
97  return $addresses;
98  }
99 }
$config
Definition: fraud_order.php:17
$addresses
Definition: address_list.php:7
__construct(\Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory, \Magento\Setup\Model\Address\AddressDataGenerator $addressDataGenerator, array $config)