| Conditions | 17 |
| Paths | 28 |
| Total Lines | 105 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 56 | public function register(OrderInterface $order, bool $createOnly = false) |
||
| 57 | { |
||
| 58 | if (!$this->enabled) { |
||
| 59 | return false; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** @var CustomerInterface $customer */ |
||
| 63 | $customer = $order->getCustomer(); |
||
| 64 | $channel = $order->getChannel(); |
||
| 65 | |||
| 66 | if ( |
||
| 67 | null == $customer || |
||
| 68 | null == $channel || |
||
| 69 | count($order->getItems()) == 0 |
||
| 70 | ) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | $storeId = $channel->getCode(); |
||
| 75 | $orderId = (string) $order->getId(); |
||
| 76 | |||
| 77 | $response = $this->ecommerceApi->getOrder($storeId, $orderId); |
||
|
|
|||
| 78 | $isNew = !isset($response['id']); |
||
| 79 | |||
| 80 | // Do nothing if the order exists |
||
| 81 | if (false === $isNew && true === $createOnly) { |
||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | // Registering the customer to ensure that exist on Mailchimp |
||
| 86 | $response = $this->customerRegisterHandler->register($customer, $channel, false, $createOnly); |
||
| 87 | |||
| 88 | if (!isset($response['id']) && $response !== false) { |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | |||
| 92 | // Creating order show url |
||
| 93 | $context = $this->router->getContext(); |
||
| 94 | $context->setHost($channel->getHostname()); |
||
| 95 | $orderShowUrl = $this->router->generate('sylius_shop_order_show', [ |
||
| 96 | '_locale' => $order->getLocaleCode() ?: 'en', |
||
| 97 | 'tokenValue' => $order->getTokenValue(), |
||
| 98 | ], RouterInterface::ABSOLUTE_URL); |
||
| 99 | |||
| 100 | $lastCompletedPayment = $order->getLastPayment(PaymentInterface::STATE_COMPLETED); |
||
| 101 | /** @var \DateTime $orderCompletedDate */ |
||
| 102 | $orderCompletedDate = $lastCompletedPayment?$lastCompletedPayment->getUpdatedAt():$order->getUpdatedAt(); |
||
| 103 | |||
| 104 | $data = [ |
||
| 105 | 'id' => $orderId, |
||
| 106 | 'customer' => [ |
||
| 107 | 'id' => (string) $customer->getId(), |
||
| 108 | ], |
||
| 109 | 'financial_status' => 'paid', |
||
| 110 | 'currency_code' => $order->getCurrencyCode() ?: 'USD', |
||
| 111 | 'order_total' => $order->getTotal() / 100, |
||
| 112 | 'order_url' => $orderShowUrl, |
||
| 113 | 'discount_total' => $order->getOrderPromotionTotal() / 100, |
||
| 114 | 'tax_total' => $order->getTaxTotal() / 100, |
||
| 115 | 'shipping_total' => $order->getShippingTotal() / 100, |
||
| 116 | 'processed_at_foreign' => $orderCompletedDate->format('c'), |
||
| 117 | 'shipping_address' => $this->getAddressData($order->getShippingAddress()), |
||
| 118 | 'billing_address' => $this->getAddressData($order->getBillingAddress()), |
||
| 119 | 'lines' => [], |
||
| 120 | ]; |
||
| 121 | |||
| 122 | if ($this->session->has('campaingId')) { |
||
| 123 | $data['campaign_id'] = $this->session->get('campaingId'); |
||
| 124 | } |
||
| 125 | |||
| 126 | foreach ($order->getItems() as $item) { |
||
| 127 | $product = $item->getProduct(); |
||
| 128 | $variant = $item->getVariant(); |
||
| 129 | |||
| 130 | if (null == $product || null == $variant) { |
||
| 131 | continue; |
||
| 132 | } |
||
| 133 | |||
| 134 | $data['lines'][] = [ |
||
| 135 | 'id' => (string) $item->getId(), |
||
| 136 | 'product_id' => (string) $product->getId(), |
||
| 137 | 'product_variant_id' => (string) $variant->getId(), |
||
| 138 | 'quantity' => $item->getQuantity(), |
||
| 139 | 'price' => $item->getTotal() / 100, |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($isNew) { |
||
| 144 | $event = new GenericEvent($order, ['data' => $data]); |
||
| 145 | $this->eventDispatcher->dispatch($event, 'mailchimp.order.pre_add'); |
||
| 146 | $data = $event->getArgument('data'); |
||
| 147 | |||
| 148 | $response = $this->ecommerceApi->addOrder($storeId, $data); |
||
| 149 | |||
| 150 | // Unregister abandoned cart after order create |
||
| 151 | $this->removeCart($order); |
||
| 152 | } else { |
||
| 153 | $event = new GenericEvent($order, ['data' => $data]); |
||
| 154 | $this->eventDispatcher->dispatch($event, 'mailchimp.order.pre_update'); |
||
| 155 | $data = $event->getArgument('data'); |
||
| 156 | |||
| 157 | $response = $this->ecommerceApi->updateOrder($storeId, $orderId, $data); |
||
| 158 | } |
||
| 159 | |||
| 160 | return $response; |
||
| 161 | } |
||
| 229 |