| Conditions | 16 |
| Paths | 16 |
| Total Lines | 96 |
| Code Lines | 59 |
| 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 |
||
| 66 | public function register(OrderInterface $order, bool $createOnly = false) |
||
| 67 | { |
||
| 68 | if (!$this->enabled) { |
||
| 69 | return false; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** @var CustomerInterface $customer */ |
||
| 73 | $customer = $order->getCustomer(); |
||
| 74 | $channel = $order->getChannel(); |
||
| 75 | |||
| 76 | if ( |
||
| 77 | null == $customer || |
||
| 78 | null == $channel || |
||
| 79 | count($order->getItems()) == 0 |
||
| 80 | ) { |
||
| 81 | return false; |
||
| 82 | } |
||
| 83 | |||
| 84 | $storeId = $channel->getCode(); |
||
| 85 | $cartId = (string) $order->getId(); |
||
| 86 | |||
| 87 | $response = $this->ecommerceApi->getCart($storeId, $cartId); |
||
|
|
|||
| 88 | $isNew = !isset($response['id']); |
||
| 89 | |||
| 90 | // Do nothing if the cart exists |
||
| 91 | if (false === $isNew && true === $createOnly) { |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | // Registering the customer to ensure that exist on Mailchimp |
||
| 96 | $response = $this->customerRegisterHandler->register($customer, $channel, false, $createOnly); |
||
| 97 | |||
| 98 | if (!isset($response['id']) && $response !== false) { |
||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | // Assigning the token value to the order |
||
| 103 | $this->orderTokenAssigner->assignTokenValueIfNotSet($order); |
||
| 104 | $this->entityManager->flush(); |
||
| 105 | |||
| 106 | // Creating continue purchase url |
||
| 107 | $context = $this->router->getContext(); |
||
| 108 | $context->setHost($channel->getHostname()); |
||
| 109 | $continuePurchaseUrl = $this->router->generate('odiseo_sylius_mailchimp_plugin_shop_continue_cart_purchase', [ |
||
| 110 | '_locale' => $order->getLocaleCode() ?: 'en', |
||
| 111 | 'tokenValue' => $order->getTokenValue(), |
||
| 112 | ], RouterInterface::ABSOLUTE_URL); |
||
| 113 | |||
| 114 | $data = [ |
||
| 115 | 'id' => $cartId, |
||
| 116 | 'customer' => [ |
||
| 117 | 'id' => (string) $customer->getId(), |
||
| 118 | ], |
||
| 119 | 'checkout_url' => $continuePurchaseUrl, |
||
| 120 | 'currency_code' => $order->getCurrencyCode() ?: 'USD', |
||
| 121 | 'order_total' => $order->getTotal() / 100, |
||
| 122 | 'tax_total' => $order->getTaxTotal() / 100, |
||
| 123 | 'lines' => [], |
||
| 124 | ]; |
||
| 125 | |||
| 126 | if ($this->session->has('campaingId')) { |
||
| 127 | $data['campaign_id'] = $this->session->get('campaingId'); |
||
| 128 | } |
||
| 129 | |||
| 130 | foreach ($order->getItems() as $item) { |
||
| 131 | $product = $item->getProduct(); |
||
| 132 | $variant = $item->getVariant(); |
||
| 133 | |||
| 134 | if (null == $product || null == $variant) { |
||
| 135 | continue; |
||
| 136 | } |
||
| 137 | |||
| 138 | $data['lines'][] = [ |
||
| 139 | 'id' => (string) $item->getId(), |
||
| 140 | 'product_id' => (string) $product->getId(), |
||
| 141 | 'product_variant_id' => (string) $variant->getId(), |
||
| 142 | 'quantity' => $item->getQuantity(), |
||
| 143 | 'price' => $item->getTotal() / 100, |
||
| 144 | ]; |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($isNew) { |
||
| 148 | $event = new GenericEvent($order, ['data' => $data]); |
||
| 149 | $this->eventDispatcher->dispatch($event, 'mailchimp.cart.pre_add'); |
||
| 150 | $data = $event->getArgument('data'); |
||
| 151 | |||
| 152 | $response = $this->ecommerceApi->addCart($storeId, $data); |
||
| 153 | } else { |
||
| 154 | $event = new GenericEvent($order, ['data' => $data]); |
||
| 155 | $this->eventDispatcher->dispatch($event, 'mailchimp.cart.pre_update'); |
||
| 156 | $data = $event->getArgument('data'); |
||
| 157 | |||
| 158 | $response = $this->ecommerceApi->updateCart($storeId, $cartId, $data); |
||
| 159 | } |
||
| 160 | |||
| 161 | return $response; |
||
| 162 | } |
||
| 195 |