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; |
||||
0 ignored issues
–
show
|
|||||
61 | } |
||||
62 | |||||
63 | /** @var CustomerInterface $customer */ |
||||
64 | if ((null === $customer = $order->getCustomer()) || (count($order->getItems()) == 0)) { |
||||
65 | return false; |
||||
0 ignored issues
–
show
The expression
return false returns the type false which is incompatible with the return type mandated by Odiseo\SyliusMailchimpPl...erInterface::register() of array|null .
In the issue above, the returned value is violating the contract defined by the mentioned interface. Let's take a look at an example: interface HasName {
/** @return string */
public function getName();
}
class Name {
public $name;
}
class User implements HasName {
/** @return string|Name */
public function getName() {
return new Name('foo'); // This is a violation of the ``HasName`` interface
// which only allows a string value to be returned.
}
}
Loading history...
|
|||||
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
It seems like
$channel can also be of type null ; however, parameter $channel of Odiseo\SyliusMailchimpPl...erInterface::register() does only seem to accept Sylius\Component\Core\Model\ChannelInterface , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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'), |
||||
0 ignored issues
–
show
The method
format() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||||
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; |
||||
0 ignored issues
–
show
|
|||||
131 | } |
||||
132 | |||||
133 | /** |
||||
134 | * {@inheritdoc} |
||||
135 | */ |
||||
136 | public function unregister(OrderInterface $order) |
||||
137 | { |
||||
138 | if (!$this->enabled) { |
||||
139 | return false; |
||||
0 ignored issues
–
show
The expression
return false returns the type false which is incompatible with the return type mandated by Odiseo\SyliusMailchimpPl...Interface::unregister() of array|null .
In the issue above, the returned value is violating the contract defined by the mentioned interface. Let's take a look at an example: interface HasName {
/** @return string */
public function getName();
}
class Name {
public $name;
}
class User implements HasName {
/** @return string|Name */
public function getName() {
return new Name('foo'); // This is a violation of the ``HasName`` interface
// which only allows a string value to be returned.
}
}
Loading history...
|
|||||
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); |
||||
0 ignored issues
–
show
|
|||||
150 | } |
||||
151 | |||||
152 | return false; |
||||
0 ignored issues
–
show
The expression
return false returns the type false which is incompatible with the return type mandated by Odiseo\SyliusMailchimpPl...Interface::unregister() of array|null .
In the issue above, the returned value is violating the contract defined by the mentioned interface. Let's take a look at an example: interface HasName {
/** @return string */
public function getName();
}
class Name {
public $name;
}
class User implements HasName {
/** @return string|Name */
public function getName() {
return new Name('foo'); // This is a violation of the ``HasName`` interface
// which only allows a string value to be returned.
}
}
Loading history...
|
|||||
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 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: