Checks if the types of the passed arguments in a function/method call are compatible.
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Odiseo\SyliusMailchimpPlugin\Handler; |
||
6 | |||
7 | use Odiseo\SyliusMailchimpPlugin\Api\EcommerceInterface; |
||
8 | use Sylius\Component\Core\Model\AddressInterface; |
||
9 | use Sylius\Component\Core\Model\CustomerInterface; |
||
10 | use Sylius\Component\Core\Model\OrderInterface; |
||
11 | use Sylius\Component\Core\Model\PaymentInterface; |
||
12 | use Symfony\Component\Routing\RouterInterface; |
||
13 | |||
14 | final class OrderRegisterHandler implements OrderRegisterHandlerInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var EcommerceInterface |
||
18 | */ |
||
19 | private $ecommerceApi; |
||
20 | |||
21 | /** |
||
22 | * @var CustomerRegisterHandlerInterface |
||
23 | */ |
||
24 | private $customerRegisterHandler; |
||
25 | |||
26 | /** |
||
27 | * @var RouterInterface |
||
28 | */ |
||
29 | private $router; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $enabled; |
||
35 | |||
36 | /** |
||
37 | * @param EcommerceInterface $ecommerceApi |
||
38 | * @param CustomerRegisterHandlerInterface $customerRegisterHandler |
||
39 | * @param RouterInterface $router |
||
40 | * @param bool $enabled |
||
41 | */ |
||
42 | public function __construct( |
||
43 | EcommerceInterface $ecommerceApi, |
||
44 | CustomerRegisterHandlerInterface $customerRegisterHandler, |
||
45 | RouterInterface $router, |
||
46 | bool $enabled |
||
47 | ) { |
||
48 | $this->ecommerceApi = $ecommerceApi; |
||
49 | $this->customerRegisterHandler = $customerRegisterHandler; |
||
50 | $this->router = $router; |
||
51 | $this->enabled = $enabled; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function register(OrderInterface $order) |
||
58 | { |
||
59 | if (!$this->enabled) { |
||
60 | return false; |
||
61 | } |
||
62 | |||
63 | /** @var CustomerInterface $customer */ |
||
64 | if ((null === $customer = $order->getCustomer()) || (count($order->getItems()) == 0)) { |
||
65 | return false; |
||
66 | } |
||
67 | |||
68 | $channel = $order->getChannel(); |
||
69 | $storeId = $channel->getCode(); |
||
70 | $orderId = (string) $order->getId(); |
||
71 | |||
72 | // Registering the customer to ensure that exist on Mailchimp |
||
73 | $response = $this->customerRegisterHandler->register($customer, $channel); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
74 | |||
75 | if (!isset($response['id']) && $response !== false) { |
||
76 | return false; |
||
77 | } |
||
78 | |||
79 | // Creating order show url |
||
80 | $context = $this->router->getContext(); |
||
81 | $context->setHost($channel->getHostname()); |
||
82 | $orderShowUrl = $this->router->generate('sylius_shop_order_show', [ |
||
83 | '_locale' => $order->getLocaleCode() ?: 'en', |
||
84 | 'tokenValue' => $order->getTokenValue(), |
||
85 | ], RouterInterface::ABSOLUTE_URL); |
||
86 | |||
87 | $response = $this->ecommerceApi->getOrder($storeId, $orderId); |
||
88 | $isNew = !isset($response['id']); |
||
89 | |||
90 | $lastCompletedPayment = $order->getLastPayment(PaymentInterface::STATE_COMPLETED); |
||
91 | $orderCompletedDate = $lastCompletedPayment?$lastCompletedPayment->getUpdatedAt():$order->getUpdatedAt(); |
||
92 | |||
93 | $data = [ |
||
94 | 'id' => $orderId, |
||
95 | 'customer' => [ |
||
96 | 'id' => (string) $customer->getId(), |
||
97 | ], |
||
98 | 'financial_status' => 'paid', |
||
99 | 'currency_code' => $order->getCurrencyCode() ?: 'USD', |
||
100 | 'order_total' => $order->getTotal() / 100, |
||
101 | 'order_url' => $orderShowUrl, |
||
102 | 'discount_total' => $order->getOrderPromotionTotal() / 100, |
||
103 | 'tax_total' => $order->getTaxTotal() / 100, |
||
104 | 'shipping_total' => $order->getShippingTotal() / 100, |
||
105 | 'processed_at_foreign' => $orderCompletedDate->format('c'), |
||
106 | 'shipping_address' => $this->getAddressData($order->getShippingAddress()), |
||
107 | 'billing_address' => $this->getAddressData($order->getBillingAddress()), |
||
108 | 'lines' => [], |
||
109 | ]; |
||
110 | |||
111 | foreach ($order->getItems() as $item) { |
||
112 | $data['lines'][] = [ |
||
113 | 'id' => (string) $item->getId(), |
||
114 | 'product_id' => (string) $item->getProduct()->getId(), |
||
115 | 'product_variant_id' => (string) $item->getVariant()->getId(), |
||
116 | 'quantity' => $item->getQuantity(), |
||
117 | 'price' => $item->getTotal() / 100, |
||
118 | ]; |
||
119 | } |
||
120 | |||
121 | if ($isNew) { |
||
122 | $response = $this->ecommerceApi->addOrder($storeId, $data); |
||
123 | |||
124 | // Unregister abandoned cart after order create |
||
125 | $this->removeCart($order); |
||
126 | } else { |
||
127 | $response = $this->ecommerceApi->updateOrder($storeId, $orderId, $data); |
||
128 | } |
||
129 | |||
130 | return $response; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function unregister(OrderInterface $order) |
||
137 | { |
||
138 | if (!$this->enabled) { |
||
139 | return false; |
||
140 | } |
||
141 | |||
142 | $orderId = (string) $order->getId(); |
||
143 | $storeId = $order->getChannel()->getCode(); |
||
144 | |||
145 | $response = $this->ecommerceApi->getOrder($storeId, $orderId); |
||
146 | $isNew = !isset($response['id']); |
||
147 | |||
148 | if (!$isNew) { |
||
149 | return $this->ecommerceApi->removeOrder($storeId, $orderId); |
||
150 | } |
||
151 | |||
152 | return false; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param AddressInterface $address |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | private function getAddressData(AddressInterface $address): array |
||
161 | { |
||
162 | return [ |
||
163 | 'company' => $address->getCompany() ?: '', |
||
164 | 'address1' => $address->getStreet() ?: '', |
||
165 | 'city' => $address->getCity() ?: '', |
||
166 | 'province' => $address->getProvinceName() ?: '', |
||
167 | 'province_code' => $address->getProvinceCode() ?: '', |
||
168 | 'postal_code' => $address->getPostcode() ?: '', |
||
169 | 'country_code' => $address->getCountryCode() ?: '', |
||
170 | 'phone' => $address->getPhoneNumber() ?: '', |
||
171 | ]; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param OrderInterface $order |
||
176 | */ |
||
177 | private function removeCart(OrderInterface $order): void |
||
178 | { |
||
179 | $cartId = (string) $order->getId(); |
||
180 | $storeId = $order->getChannel()->getCode(); |
||
181 | |||
182 | $this->ecommerceApi->removeCart($storeId, $cartId); |
||
183 | } |
||
184 | } |
||
185 |