GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 435100...78fe11 )
by
unknown
04:57
created

OrderRegisterHandler::removeCart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
     * @param EcommerceInterface $ecommerceApi
33
     * @param CustomerRegisterHandlerInterface $customerRegisterHandler
34
     * @param RouterInterface $router
35
     */
36
    public function __construct(
37
        EcommerceInterface $ecommerceApi,
38
        CustomerRegisterHandlerInterface $customerRegisterHandler,
39
        RouterInterface $router
40
    ) {
41
        $this->ecommerceApi = $ecommerceApi;
42
        $this->customerRegisterHandler = $customerRegisterHandler;
43
        $this->router = $router;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function register(OrderInterface $order)
50
    {
51
        /** @var CustomerInterface $customer */
52
        if ((null === $customer = $order->getCustomer()) || (count($order->getItems()) == 0)) {
53
            return false;
0 ignored issues
show
Bug Best Practice introduced by
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...
54
        }
55
56
        $channel = $order->getChannel();
57
        $storeId = $channel->getCode();
58
        $orderId = (string) $order->getId();
59
60
        // Registering the customer to ensure that exist on Mailchimp
61
        $response = $this->customerRegisterHandler->register($customer, $channel);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

61
        $response = $this->customerRegisterHandler->register($customer, /** @scrutinizer ignore-type */ $channel);
Loading history...
62
63
        if (!isset($response['id']) && $response !== false) {
0 ignored issues
show
introduced by
The condition $response !== false is always false.
Loading history...
64
            return false;
65
        }
66
67
        // Creating order show url
68
        $context = $this->router->getContext();
69
        $context->setHost($channel->getHostname());
70
        $orderShowUrl = $this->router->generate('sylius_shop_order_show', [
71
            '_locale' => $order->getLocaleCode() ?: 'en',
72
            'tokenValue' => $order->getTokenValue(),
73
        ], RouterInterface::ABSOLUTE_URL);
74
75
        $response = $this->ecommerceApi->getOrder($storeId, $orderId);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...ceInterface::getOrder() does only seem to accept string, 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 ignore-type  annotation

75
        $response = $this->ecommerceApi->getOrder(/** @scrutinizer ignore-type */ $storeId, $orderId);
Loading history...
76
        $isNew = !isset($response['id']);
77
78
        $lastCompletedPayment = $order->getLastPayment(PaymentInterface::STATE_COMPLETED);
79
        $orderCompletedDate = $lastCompletedPayment?$lastCompletedPayment->getUpdatedAt():$order->getUpdatedAt();
80
81
        $data = [
82
            'id' => $orderId,
83
            'customer' => [
84
                'id' => (string) $customer->getId(),
85
            ],
86
            'financial_status' => 'paid',
87
            'currency_code' => $order->getCurrencyCode() ?: 'USD',
88
            'order_total' => $order->getTotal() / 100,
89
            'order_url' => $orderShowUrl,
90
            'discount_total' => $order->getOrderPromotionTotal() / 100,
91
            'tax_total' => $order->getTaxTotal() / 100,
92
            'shipping_total' => $order->getShippingTotal() / 100,
93
            'processed_at_foreign' => $orderCompletedDate->format('c'),
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

93
            'processed_at_foreign' => $orderCompletedDate->/** @scrutinizer ignore-call */ format('c'),

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...
94
            'shipping_address' => $this->getAddressData($order->getShippingAddress()),
0 ignored issues
show
Bug introduced by
It seems like $order->getShippingAddress() can also be of type null; however, parameter $address of Odiseo\SyliusMailchimpPl...ndler::getAddressData() does only seem to accept Sylius\Component\Core\Model\AddressInterface, 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 ignore-type  annotation

94
            'shipping_address' => $this->getAddressData(/** @scrutinizer ignore-type */ $order->getShippingAddress()),
Loading history...
95
            'billing_address' => $this->getAddressData($order->getBillingAddress()),
96
            'lines' => [],
97
        ];
98
99
        foreach ($order->getItems() as $item) {
100
            $data['lines'][] = [
101
                'id' => (string) $item->getId(),
102
                'product_id' => (string) $item->getProduct()->getId(),
103
                'product_variant_id' => (string) $item->getVariant()->getId(),
104
                'quantity' => $item->getQuantity(),
105
                'price' => $item->getTotal() / 100,
106
            ];
107
        }
108
109
        if ($isNew) {
110
            $response = $this->ecommerceApi->addOrder($storeId, $data);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...ceInterface::addOrder() does only seem to accept string, 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 ignore-type  annotation

110
            $response = $this->ecommerceApi->addOrder(/** @scrutinizer ignore-type */ $storeId, $data);
Loading history...
111
112
            // Unregister abandoned cart after order create
113
            $this->removeCart($order);
114
        } else {
115
            $response = $this->ecommerceApi->updateOrder($storeId, $orderId, $data);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...nterface::updateOrder() does only seem to accept string, 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 ignore-type  annotation

115
            $response = $this->ecommerceApi->updateOrder(/** @scrutinizer ignore-type */ $storeId, $orderId, $data);
Loading history...
116
        }
117
118
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response also could return the type false which is incompatible with the return type mandated by Odiseo\SyliusMailchimpPl...erInterface::register() of array|null.
Loading history...
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function unregister(OrderInterface $order)
125
    {
126
        $orderId = (string) $order->getId();
127
        $storeId = $order->getChannel()->getCode();
128
129
        $response = $this->ecommerceApi->getOrder($storeId, $orderId);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...ceInterface::getOrder() does only seem to accept string, 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 ignore-type  annotation

129
        $response = $this->ecommerceApi->getOrder(/** @scrutinizer ignore-type */ $storeId, $orderId);
Loading history...
130
        $isNew = !isset($response['id']);
131
132
        if (!$isNew) {
133
            return $this->ecommerceApi->removeOrder($storeId, $orderId);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...nterface::removeOrder() does only seem to accept string, 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 ignore-type  annotation

133
            return $this->ecommerceApi->removeOrder(/** @scrutinizer ignore-type */ $storeId, $orderId);
Loading history...
Bug Best Practice introduced by
The expression return $this->ecommerceA...der($storeId, $orderId) also could return the type false which is incompatible with the return type mandated by Odiseo\SyliusMailchimpPl...Interface::unregister() of array|null.
Loading history...
134
        }
135
136
        return false;
0 ignored issues
show
Bug Best Practice introduced by
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...
137
    }
138
139
    /**
140
     * @param AddressInterface $address
141
     *
142
     * @return array
143
     */
144
    private function getAddressData(AddressInterface $address): array
145
    {
146
        return [
147
            'company' => $address->getCompany() ?: '',
148
            'address1' => $address->getStreet() ?: '',
149
            'city' => $address->getCity() ?: '',
150
            'province' => $address->getProvinceName() ?: '',
151
            'province_code' => $address->getProvinceCode() ?: '',
152
            'postal_code' => $address->getPostcode() ?: '',
153
            'country_code' => $address->getCountryCode() ?: '',
154
            'phone' => $address->getPhoneNumber() ?: '',
155
        ];
156
    }
157
158
    /**
159
     * @param OrderInterface $order
160
     */
161
    private function removeCart(OrderInterface $order): void
162
    {
163
        $cartId = (string) $order->getId();
164
        $storeId = $order->getChannel()->getCode();
165
166
        $this->ecommerceApi->removeCart($storeId, $cartId);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...Interface::removeCart() does only seem to accept string, 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 ignore-type  annotation

166
        $this->ecommerceApi->removeCart(/** @scrutinizer ignore-type */ $storeId, $cartId);
Loading history...
167
    }
168
}
169