1 | <?php |
||
2 | |||
3 | /** |
||
4 | * TechDivision\Import\Customer\Address\Observers\CustomerAddressObserver |
||
5 | * |
||
6 | * PHP version 7 |
||
7 | * |
||
8 | * @author Tim Wagner <[email protected]> |
||
9 | * @copyright 2018 TechDivision GmbH <[email protected]> |
||
10 | * @license https://opensource.org/licenses/MIT |
||
11 | * @link https://github.com/techdivision/import-customer-address |
||
12 | * @link http://www.techdivision.com |
||
13 | */ |
||
14 | |||
15 | namespace TechDivision\Import\Customer\Address\Observers; |
||
16 | |||
17 | use TechDivision\Import\Customer\Address\Utils\ColumnKeys; |
||
18 | use TechDivision\Import\Customer\Address\Utils\MemberNames; |
||
19 | use TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface; |
||
20 | use TechDivision\Import\Customer\Address\Utils\CoreConfigDataKeys; |
||
21 | use TechDivision\Import\Observers\CleanUpEmptyColumnsTrait; |
||
22 | use TechDivision\Import\Observers\StateDetectorInterface; |
||
23 | use TechDivision\Import\Utils\ConfigurationKeys; |
||
24 | use TechDivision\Import\Utils\RegistryKeys; |
||
25 | |||
26 | /** |
||
27 | * Observer that create's the customer address itself. |
||
28 | * |
||
29 | * @author Tim Wagner <[email protected]> |
||
30 | * @copyright 2018 TechDivision GmbH <[email protected]> |
||
31 | * @license https://opensource.org/licenses/MIT |
||
32 | * @link https://github.com/techdivision/import-customer-address |
||
33 | * @link http://www.techdivision.com |
||
34 | */ |
||
35 | class CustomerAddressObserver extends AbstractCustomerAddressImportObserver |
||
36 | { |
||
37 | |||
38 | use CleanUpEmptyColumnsTrait; |
||
39 | |||
40 | /** |
||
41 | * The customer address bunch processor instance. |
||
42 | * |
||
43 | * @var \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface |
||
44 | */ |
||
45 | protected $customerAddressBunchProcessor; |
||
46 | |||
47 | /** |
||
48 | * Initialize the observer with the passed customer bunch processor instance. |
||
49 | * |
||
50 | * @param \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor The customer address bunch processor instance |
||
51 | * @param \TechDivision\Import\Observers\StateDetectorInterface $stateDetector The state detector instance |
||
52 | */ |
||
53 | public function __construct( |
||
54 | CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor, |
||
55 | StateDetectorInterface $stateDetector = null |
||
56 | ) { |
||
57 | parent::__construct($stateDetector); |
||
58 | $this->customerAddressBunchProcessor = $customerAddressBunchProcessor; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Return's the customer address bunch processor instance. |
||
63 | * |
||
64 | * @return \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface The customer address bunch processor instance |
||
65 | */ |
||
66 | protected function getCustomerAddressBunchProcessor() |
||
67 | { |
||
68 | return $this->customerAddressBunchProcessor; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Process the observer's business logic. |
||
73 | * |
||
74 | * @return void |
||
75 | */ |
||
76 | protected function process() |
||
77 | { |
||
78 | |||
79 | // prepare the static entity values |
||
80 | $customerAddress = $this->initializeCustomerAddress($this->prepareAttributes()); |
||
81 | |||
82 | if (!empty($customerAddress)) { |
||
83 | if ($this->hasChanges($customerAddress)) { |
||
84 | // insert the entity and set the entity ID |
||
85 | $this->setLastEntityId($this->persistCustomerAddress($customerAddress)); |
||
86 | } else { |
||
87 | $this->setLastEntityId($customerAddress[MemberNames::ENTITY_ID]); |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Prepare the attributes of the entity that has to be persisted. |
||
94 | * |
||
95 | * @return array The prepared attributes |
||
96 | * @throws \Exception |
||
97 | */ |
||
98 | protected function prepareAttributes() |
||
99 | { |
||
100 | |||
101 | // load the website ID for the given code |
||
102 | $websiteId = $this->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::WEBSITE)); |
||
103 | |||
104 | // load the customer |
||
105 | $customer = $this->loadCustomerByEmailAndWebsiteId($this->getValue(ColumnKeys::EMAIL), $websiteId); |
||
106 | |||
107 | if (!$customer) { |
||
108 | $message = sprintf( |
||
109 | 'the imported address has no customer with email %s', |
||
110 | $this->getValue(ColumnKeys::EMAIL) |
||
111 | ); |
||
112 | $this->mergeStatus( |
||
113 | array( |
||
114 | RegistryKeys::NO_STRICT_VALIDATIONS => array( |
||
115 | basename($this->getFilename()) => array( |
||
116 | $this->getLineNumber() => array( |
||
117 | ColumnKeys::EMAIL => $message |
||
118 | ) |
||
119 | ) |
||
120 | ) |
||
121 | ) |
||
122 | ); |
||
123 | return []; |
||
124 | } |
||
125 | |||
126 | // initialize the customer values |
||
127 | $entityId = $this->getValue(ColumnKeys::ENTITY_ID); |
||
128 | $city = $this->getValue(ColumnKeys::CITY, ''); |
||
129 | $company = $this->getValue(ColumnKeys::COMPANY); |
||
130 | $countryId = $this->getValue(ColumnKeys::COUNTRY_ID, ''); |
||
131 | $fax = $this->getValue(ColumnKeys::FAX); |
||
132 | $firstname = $this->getValue(ColumnKeys::FIRSTNAME, ''); |
||
133 | $lastname = $this->getValue(ColumnKeys::LASTNAME, ''); |
||
134 | $middlename = $this->getValue(ColumnKeys::MIDDLENAME); |
||
135 | $postcode = $this->getValue(ColumnKeys::POSTCODE); |
||
136 | $prefix = $this->getValue(ColumnKeys::PREFIX); |
||
137 | $region = $this->getValue(ColumnKeys::REGION); |
||
138 | $regionCode = $this->getValue(ColumnKeys::REGION_CODE); |
||
139 | $regionId = $this->getValue(ColumnKeys::REGION_ID); |
||
140 | |||
141 | if (empty($regionId) && !empty($regionCode)) { |
||
142 | $regionId = $this->getCountryRegionIdByCode($regionCode); |
||
143 | } |
||
144 | |||
145 | $street = $this->getValue(ColumnKeys::STREET, ''); |
||
146 | $suffix = $this->getValue(ColumnKeys::SUFFIX); |
||
147 | $telephone = $this->checkCustomerPhoneConfig($this->getValue(ColumnKeys::TELEPHONE, '')); |
||
148 | $vatId = $this->getValue(ColumnKeys::VAT_ID); |
||
149 | $vatIsValid = $this->getValue(ColumnKeys::VAT_IS_VALID); |
||
150 | $vatRequestId = $this->getValue(ColumnKeys::VAT_REQUEST_ID); |
||
151 | $vatRequestSuccess = $this->getValue(ColumnKeys::VAT_REQUEST_SUCCESS); |
||
152 | |||
153 | // load the customer's addtional attributes |
||
154 | $incrementId = $this->getValue(ColumnKeys::INCREMENT_ID); |
||
155 | $isActive = $this->getValue(ColumnKeys::IS_ACTIVE); |
||
156 | |||
157 | // prepare the date format for the created at/updated at dates |
||
158 | $createdAt = $this->getValue(ColumnKeys::CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate')); |
||
159 | $updatedAt = $this->getValue(ColumnKeys::UPDATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate')); |
||
160 | $vatRequestDate = $this->getValue(ColumnKeys::VAT_REQUEST_DATE, null, array($this, 'formatDate')); |
||
161 | |||
162 | // return the prepared customer |
||
163 | return $this->initializeEntity( |
||
164 | array( |
||
165 | MemberNames::ENTITY_ID => $entityId, |
||
166 | MemberNames::INCREMENT_ID => $incrementId, |
||
167 | MemberNames::PARENT_ID => $customer[MemberNames::ENTITY_ID], |
||
168 | MemberNames::CREATED_AT => $createdAt, |
||
169 | MemberNames::UPDATED_AT => $updatedAt, |
||
170 | MemberNames::IS_ACTIVE => $isActive, |
||
171 | MemberNames::CITY => $city, |
||
172 | MemberNames::COMPANY => $company, |
||
173 | MemberNames::COUNTRY_ID => $countryId, |
||
174 | MemberNames::FAX => $fax, |
||
175 | MemberNames::FIRSTNAME => $firstname, |
||
176 | MemberNames::LASTNAME => $lastname, |
||
177 | MemberNames::MIDDLENAME => $middlename, |
||
178 | MemberNames::POSTCODE => $postcode, |
||
179 | MemberNames::PREFIX => $prefix, |
||
180 | MemberNames::REGION => $region, |
||
181 | MemberNames::REGION_ID => $regionId, |
||
182 | MemberNames::STREET => $street, |
||
183 | MemberNames::SUFFIX => $suffix, |
||
184 | MemberNames::TELEPHONE => $telephone, |
||
185 | MemberNames::VAT_ID => $vatId, |
||
186 | MemberNames::VAT_IS_VALID => $vatIsValid, |
||
187 | MemberNames::VAT_REQUEST_DATE => $vatRequestDate, |
||
188 | MemberNames::VAT_REQUEST_ID => $vatRequestId, |
||
189 | MemberNames::VAT_REQUEST_SUCCESS => $vatRequestSuccess |
||
190 | ) |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Initialize the customer address with the passed attributes and returns an instance. |
||
196 | * |
||
197 | * @param array $attr The customer address attributes |
||
198 | * |
||
199 | * @return array The initialized customer address |
||
200 | */ |
||
201 | protected function initializeCustomerAddress(array $attr) |
||
202 | { |
||
203 | if (empty($attr)) { |
||
204 | return []; |
||
205 | } |
||
206 | |||
207 | // try to load the customer address with the given entity ID |
||
208 | if ($entity = $this->loadCustomerAddress($attr[MemberNames::ENTITY_ID])) { |
||
209 | // clear row elements that are not allowed to be updated |
||
210 | $attr = $this->clearRowData($attr); |
||
211 | |||
212 | return $this->mergeEntity($entity, $attr); |
||
213 | } |
||
214 | |||
215 | // remove the entity ID |
||
216 | // if nothing found with entity_id then remove to try with incement id or create new one |
||
217 | unset($attr[MemberNames::ENTITY_ID]); |
||
218 | |||
219 | // try to load the customer address with the given increment ID and the customer id |
||
220 | if (!empty($attr[MemberNames::INCREMENT_ID]) && $entity = $this->loadCustomerAddressByIncrementId($attr[MemberNames::INCREMENT_ID], $attr[MemberNames::PARENT_ID])) { |
||
221 | // clear row elements that are not allowed to be updated |
||
222 | $attr = $this->clearRowData($attr, true); |
||
223 | |||
224 | return $this->mergeEntity($entity, $attr); |
||
225 | } else { |
||
226 | // cleanup __EMPTY__VALUE__ entries, don't remove array elements |
||
227 | $attr = $this->clearRowData($attr, false); |
||
228 | } |
||
229 | |||
230 | // New Customer address always active |
||
231 | if ($attr[MemberNames::IS_ACTIVE] == null) { |
||
232 | $attr[MemberNames::IS_ACTIVE] = 1; |
||
233 | } |
||
234 | |||
235 | // simply return the attributes |
||
236 | return $attr; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Return's the customer with the passed entity ID. |
||
241 | * |
||
242 | * @param integer $id The entity ID of the customer to return |
||
243 | * |
||
244 | * @return array|null The customer |
||
245 | */ |
||
246 | protected function loadCustomerAddress($id) |
||
247 | { |
||
248 | return $this->getCustomerAddressBunchProcessor()->loadCustomerAddress($id); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Return's the customer with the passed increment ID. |
||
253 | * |
||
254 | * @param string|integer $incrementId The increment ID of the customer to return |
||
255 | * @param string|integer $customerId The entity_id of the customer |
||
256 | * |
||
257 | * @return array|null The customer |
||
258 | */ |
||
259 | protected function loadCustomerAddressByIncrementId($incrementId, $customerId) |
||
260 | { |
||
261 | return $this->getCustomerAddressBunchProcessor()->loadCustomerAddressByIncrementId($incrementId, $customerId); |
||
0 ignored issues
–
show
|
|||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Return's the customer with the passed email and website ID. |
||
266 | * |
||
267 | * @param string $email The email of the customer to return |
||
268 | * @param string $websiteId The website ID of the customer to return |
||
269 | * |
||
270 | * @return array|null The customer |
||
271 | */ |
||
272 | protected function loadCustomerByEmailAndWebsiteId($email, $websiteId) |
||
273 | { |
||
274 | return $this->getCustomerAddressBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Persist's the passed customer address data and return's the ID. |
||
279 | * |
||
280 | * @param array $customerAddress The customer address data to persist |
||
281 | * |
||
282 | * @return string The ID of the persisted entity |
||
283 | */ |
||
284 | protected function persistCustomerAddress($customerAddress) |
||
285 | { |
||
286 | return $this->getCustomerAddressBunchProcessor()->persistCustomerAddress($customerAddress); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Set's the ID of the customer that has been created recently. |
||
291 | * |
||
292 | * @param string $lastEntityId The entity ID |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | protected function setLastEntityId($lastEntityId) |
||
297 | { |
||
298 | $this->getSubject()->setLastEntityId($lastEntityId); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Return's the store website for the passed code. |
||
303 | * |
||
304 | * @param string $code The code of the store website to return the ID for |
||
305 | * |
||
306 | * @return integer The store website ID |
||
307 | * @throws \Exception Is thrown, if the store website with the requested code is not available |
||
308 | */ |
||
309 | protected function getStoreWebsiteIdByCode($code) |
||
310 | { |
||
311 | return $this->getSubject()->getStoreWebsiteIdByCode($code); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Return's the region ID for the given code, if it exists. |
||
316 | * |
||
317 | * @param string|null $code The code of the requested customer group |
||
318 | * |
||
319 | * @return integer|null The region ID |
||
320 | */ |
||
321 | protected function getCountryRegionIdByCode($code) |
||
322 | { |
||
323 | return $this->getSubject()->getCountryRegionIdByCode($code); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @param string $value value of customer column |
||
328 | * |
||
329 | * @return string |
||
330 | * @throws \Exception |
||
331 | */ |
||
332 | public function checkCustomerPhoneConfig($value) |
||
333 | { |
||
334 | try { |
||
335 | $telConfig = $this->getSubject()->getCoreConfigData(CoreConfigDataKeys::CUSTOMER_ADDRESS_TELEPHONE_SHOW); |
||
336 | } catch (\Exception $e) { |
||
337 | return $value; |
||
338 | } |
||
339 | if (isset($telConfig) && $telConfig !== 'req') { |
||
340 | return !empty($value) ? $value : ''; |
||
341 | } |
||
342 | return $value; |
||
343 | } |
||
344 | } |
||
345 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.