1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Odiseo\SyliusMailchimpPlugin\Handler; |
||
6 | |||
7 | use Doctrine\ORM\EntityManagerInterface; |
||
8 | use Odiseo\SyliusMailchimpPlugin\Api\EcommerceInterface; |
||
9 | use Sylius\Component\Core\Model\CustomerInterface; |
||
10 | use Sylius\Component\Core\Model\OrderInterface; |
||
11 | use Sylius\Component\Core\TokenAssigner\OrderTokenAssignerInterface; |
||
12 | use Symfony\Component\Routing\RouterInterface; |
||
13 | |||
14 | final class CartRegisterHandler implements CartRegisterHandlerInterface |
||
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 OrderTokenAssignerInterface |
||
33 | */ |
||
34 | private $orderTokenAssigner; |
||
35 | |||
36 | /** |
||
37 | * @var EntityManagerInterface |
||
38 | */ |
||
39 | private $entityManager; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | private $enabled; |
||
45 | |||
46 | /** |
||
47 | * @param EcommerceInterface $ecommerceApi |
||
48 | * @param RouterInterface $router |
||
49 | * @param OrderTokenAssignerInterface $orderTokenAssigner |
||
50 | * @param EntityManagerInterface $entityManager |
||
51 | * @param CustomerRegisterHandlerInterface $customerRegisterHandler |
||
52 | * @param bool $enabled |
||
53 | */ |
||
54 | public function __construct( |
||
55 | EcommerceInterface $ecommerceApi, |
||
56 | CustomerRegisterHandlerInterface $customerRegisterHandler, |
||
57 | RouterInterface $router, |
||
58 | OrderTokenAssignerInterface $orderTokenAssigner, |
||
59 | EntityManagerInterface $entityManager, |
||
60 | bool $enabled |
||
61 | ) { |
||
62 | $this->ecommerceApi = $ecommerceApi; |
||
63 | $this->router = $router; |
||
64 | $this->orderTokenAssigner = $orderTokenAssigner; |
||
65 | $this->entityManager = $entityManager; |
||
66 | $this->customerRegisterHandler = $customerRegisterHandler; |
||
67 | $this->enabled = $enabled; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function register(OrderInterface $order) |
||
74 | { |
||
75 | if (!$this->enabled) { |
||
76 | return false; |
||
77 | } |
||
78 | |||
79 | /** @var CustomerInterface $customer */ |
||
80 | if ((null === $customer = $order->getCustomer()) || (count($order->getItems()) == 0)) { |
||
81 | return false; |
||
82 | } |
||
83 | |||
84 | $channel = $order->getChannel(); |
||
85 | $storeId = $channel->getCode(); |
||
86 | $cartId = (string) $order->getId(); |
||
87 | |||
88 | // Registering the customer to ensure that exist on Mailchimp |
||
89 | $response = $this->customerRegisterHandler->register($customer, $channel); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
90 | |||
91 | if (!isset($response['id']) && $response !== false) { |
||
92 | return false; |
||
93 | } |
||
94 | |||
95 | // Assigning the token value to the order |
||
96 | $this->orderTokenAssigner->assignTokenValueIfNotSet($order); |
||
97 | $this->entityManager->flush(); |
||
98 | |||
99 | // Creating continue purchase url |
||
100 | $context = $this->router->getContext(); |
||
101 | $context->setHost($channel->getHostname()); |
||
102 | $continuePurchaseUrl = $this->router->generate('odiseo_sylius_mailchimp_plugin_continue_cart_purchase', [ |
||
103 | '_locale' => $order->getLocaleCode() ?: 'en', |
||
104 | 'tokenValue' => $order->getTokenValue(), |
||
105 | ], RouterInterface::ABSOLUTE_URL); |
||
106 | |||
107 | $response = $this->ecommerceApi->getCart($storeId, $cartId); |
||
108 | $isNew = !isset($response['id']); |
||
109 | |||
110 | $data = [ |
||
111 | 'id' => $cartId, |
||
112 | 'customer' => [ |
||
113 | 'id' => (string) $customer->getId(), |
||
114 | ], |
||
115 | 'checkout_url' => $continuePurchaseUrl, |
||
116 | 'currency_code' => $order->getCurrencyCode() ?: 'USD', |
||
117 | 'order_total' => $order->getTotal() / 100, |
||
118 | 'tax_total' => $order->getTaxTotal() / 100, |
||
119 | 'lines' => [], |
||
120 | ]; |
||
121 | |||
122 | foreach ($order->getItems() as $item) { |
||
123 | $data['lines'][] = [ |
||
124 | 'id' => (string) $item->getId(), |
||
125 | 'product_id' => (string) $item->getProduct()->getId(), |
||
126 | 'product_variant_id' => (string) $item->getVariant()->getId(), |
||
127 | 'quantity' => $item->getQuantity(), |
||
128 | 'price' => $item->getTotal() / 100, |
||
129 | ]; |
||
130 | } |
||
131 | |||
132 | if ($isNew) { |
||
133 | $response = $this->ecommerceApi->addCart($storeId, $data); |
||
134 | } else { |
||
135 | $response = $this->ecommerceApi->updateCart($storeId, $cartId, $data); |
||
136 | } |
||
137 | |||
138 | return $response; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function unregister(OrderInterface $order) |
||
145 | { |
||
146 | if (!$this->enabled) { |
||
147 | return false; |
||
148 | } |
||
149 | |||
150 | $orderId = (string) $order->getId(); |
||
151 | $storeId = $order->getChannel()->getCode(); |
||
152 | |||
153 | $response = $this->ecommerceApi->getCart($storeId, $orderId); |
||
154 | $isNew = !isset($response['id']); |
||
155 | |||
156 | if (!$isNew) { |
||
157 | return $this->ecommerceApi->removeCart($storeId, $orderId); |
||
158 | } |
||
159 | |||
160 | return false; |
||
161 | } |
||
162 | } |
||
163 |