Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerGenerator.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
19  private $entityGeneratorFactory;
20 
24  private $customerTemplateGenerator;
25 
29  private $resourceConnection;
30 
34  private $connection;
35 
41  public function __construct(
42  EntityGeneratorFactory $entityGeneratorFactory,
43  CustomerTemplateGenerator $customerTemplateGenerator,
44  \Magento\Framework\App\ResourceConnection $resourceConnection
45  ) {
46  $this->entityGeneratorFactory = $entityGeneratorFactory;
47  $this->customerTemplateGenerator = $customerTemplateGenerator;
48  $this->resourceConnection = $resourceConnection;
49  }
50 
58  public function generate($customers, array $fixtureMap)
59  {
60  $this->entityGeneratorFactory
61  ->create([
62  'entityType' => CustomerInterface::class,
63  'customTableMap' => [
64  'customer_entity' => [
65  'handler' => $this->getCustomerEntityHandler()
66  ],
67 
68  'customer_address_entity' => [
69  'handler' => $this->getCustomerAddressEntityHandler()
70  ]
71  ],
72  ])->generate(
73  $this->customerTemplateGenerator,
74  $customers,
75  function ($customerId) use ($fixtureMap) {
76  $fixtureMap['customer_data'] = call_user_func($fixtureMap['customer_data'], $customerId);
77  return $fixtureMap;
78  }
79  );
80 
81  $this->addDefaultAddresses();
82  }
83 
91  private function getCustomerEntityHandler()
92  {
93  return function ($entityId, $entityNumber, $fixtureMap, $binds) {
94  return array_map(
95  'array_merge',
96  $binds,
97  array_fill(0, count($binds), $fixtureMap['customer_data']['customer'])
98  );
99  };
100  }
101 
109  private function getCustomerAddressEntityHandler()
110  {
111  return function ($entityId, $entityNumber, $fixtureMap, $binds) {
112  return array_map(
113  'array_merge',
114  array_fill(0, count($fixtureMap['customer_data']['addresses']), reset($binds)),
115  $fixtureMap['customer_data']['addresses']
116  );
117  };
118  }
119 
125  private function addDefaultAddresses()
126  {
127  $this->getConnection()->query(
128  sprintf(
129  '
130  update `%s` customer
131  join (
132  select
133  parent_id, min(entity_id) as min, max(entity_id) as max
134  from `%s`
135  group by parent_id
136  ) customer_address on customer_address.parent_id = customer.entity_id
137  set
138  customer.default_billing = customer_address.min,
139  customer.default_shipping = customer_address.max
140  ',
141  $this->resourceConnection->getTableName('customer_entity'),
142  $this->resourceConnection->getTableName('customer_address_entity')
143  )
144  );
145  }
146 
150  private function getConnection()
151  {
152  if (null === $this->connection) {
153  $this->connection = $this->resourceConnection->getConnection();
154  }
155 
156  return $this->connection;
157  }
158 }
__construct(EntityGeneratorFactory $entityGeneratorFactory, CustomerTemplateGenerator $customerTemplateGenerator, \Magento\Framework\App\ResourceConnection $resourceConnection)