Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerTemplateGenerator.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Customer\Model\AddressFactory;
12 use Magento\Customer\Model\CustomerFactory;
14 
19 {
23  private $customerFactory;
24 
28  private $addressFactory;
29 
33  private $storeManager;
34 
40  public function __construct(
41  CustomerFactory $customerFactory,
42  AddressFactory $addressFactory,
43  StoreManagerInterface $storeManager
44  ) {
45  $this->customerFactory = $customerFactory;
46  $this->addressFactory = $addressFactory;
47  $this->storeManager = $storeManager;
48  }
49 
53  public function generateEntity()
54  {
55  $customer = $this->getCustomerTemplate();
56  $customer->save();
57  $address = $this->getAddressTemplate($customer->getId());
58  $address->save();
59 
60  return $customer;
61  }
62 
68  private function getCustomerTemplate()
69  {
70  $customerRandomizerNumber = crc32(mt_rand(1, PHP_INT_MAX));
71 
72  $now = new \DateTime();
73 
74  return $this->customerFactory->create([
75  'data' => [
76  'email' => sprintf('user_%[email protected]', $customerRandomizerNumber),
77  'confirmation' => null,
78  'created_at' => $now->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
79  'created_in' => 'Default',
80  'default_billing' => '1',
81  'default_shipping' => '1',
82  'disable_auto_group_change' => '0',
83  'dob' => '12-10-1991',
84  'firstname' => 'Firstname',
85  'gender' => 1,
86  'group_id' => '1',
87  'lastname' => 'Lastname',
88  'middlename' => '',
89  'password_hash' => '',
90  'prefix' => null,
91  'rp_token' => null,
92  'rp_token_created_at' => null,
93  'store_id' => $this->storeManager->getDefaultStoreView()->getId(),
94  'suffix' => null,
95  'taxvat' => null,
96  'website_id' => $this->storeManager->getDefaultStoreView()->getWebsiteId(),
97  'password' => '123123q',
98  ]
99  ]);
100  }
101 
106  private function getAddressTemplate($customerId)
107  {
108  return $this->addressFactory->create([
109  'data' => [
110  'parent_id' => $customerId,
111  'attribute_set_id' => 2,
112  'telephone' => 3468676,
113  'postcode' => 75477,
114  'country_id' => 'US',
115  'city' => 'CityM',
116  'company' => 'CompanyName',
117  'street' => 'Green str, 67',
118  'lastname' => 'Smith',
119  'firstname' => 'John',
120  'region_id' => 1,
121  'fax' => '04040404',
122  'middlename' => '',
123  'prefix' => '',
124  'region' => 'Arkansas',
125  'suffix' => '',
126  'vat_id' => '',
127  'default_billing_' => '1',
128  'default_shipping_' => '1',
129  ]
130  ]);
131  }
132 }
$customer
Definition: customers.php:11
$storeManager
$address
Definition: customer.php:38
$addressFactory
Definition: quote.php:20
__construct(CustomerFactory $customerFactory, AddressFactory $addressFactory, StoreManagerInterface $storeManager)