| Conditions | 15 |
| Paths | 6 |
| Total Lines | 59 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 38 | public function register( |
||
| 39 | CustomerInterface $customer, |
||
| 40 | ChannelInterface $channel, |
||
| 41 | bool $optInStatus = false, |
||
| 42 | bool $createOnly = false |
||
| 43 | ) { |
||
| 44 | if (!$this->enabled) { |
||
| 45 | return false; |
||
| 46 | } |
||
| 47 | |||
| 48 | $customerId = (string) $customer->getId(); |
||
| 49 | $storeId = $channel->getCode(); |
||
| 50 | $customerAddress = $this->getCustomerAddress($customer); |
||
| 51 | $firstName = $this->getCustomerFirstName($customer, $customerAddress); |
||
| 52 | $lastName = $this->getCustomerLastName($customer, $customerAddress); |
||
| 53 | |||
| 54 | $response = $this->ecommerceApi->getCustomer($storeId, $customerId); |
||
|
|
|||
| 55 | $isNew = !isset($response['id']); |
||
| 56 | |||
| 57 | // Do nothing if the customer exists |
||
| 58 | if (false === $isNew && true === $createOnly) { |
||
| 59 | return false; |
||
| 60 | } |
||
| 61 | |||
| 62 | $data = [ |
||
| 63 | 'id' => $customerId, |
||
| 64 | 'email_address' => $customer->getEmail(), |
||
| 65 | 'opt_in_status' => $optInStatus, |
||
| 66 | 'first_name' => $firstName ?: '', |
||
| 67 | 'last_name' => $lastName ?: '', |
||
| 68 | ]; |
||
| 69 | |||
| 70 | if ($customerAddress) { |
||
| 71 | $data['company'] = $customerAddress->getCompany() ?: ''; |
||
| 72 | $data['address'] = [ |
||
| 73 | 'address1' => $customerAddress->getStreet() ?: '', |
||
| 74 | 'city' => $customerAddress->getCity() ?: '', |
||
| 75 | 'province' => $customerAddress->getProvinceName() ?: '', |
||
| 76 | 'province_code' => $customerAddress->getProvinceCode() ?: '', |
||
| 77 | 'postal_code' => $customerAddress->getPostcode() ?: '', |
||
| 78 | 'country_code' => $customerAddress->getCountryCode() ?: '', |
||
| 79 | ]; |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($isNew) { |
||
| 83 | $event = new GenericEvent($customer, ['data' => $data, 'channel' => $channel]); |
||
| 84 | $this->eventDispatcher->dispatch($event, 'mailchimp.customer.pre_add'); |
||
| 85 | $data = $event->getArgument('data'); |
||
| 86 | |||
| 87 | $response = $this->ecommerceApi->addCustomer($storeId, $data); |
||
| 88 | } else { |
||
| 89 | $event = new GenericEvent($customer, ['data' => $data, 'channel' => $channel]); |
||
| 90 | $this->eventDispatcher->dispatch($event, 'mailchimp.customer.pre_update'); |
||
| 91 | $data = $event->getArgument('data'); |
||
| 92 | |||
| 93 | $response = $this->ecommerceApi->updateCustomer($storeId, $customerId, $data); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $response; |
||
| 97 | } |
||
| 171 |