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