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 ( 314332...aa4b5e )
by Odiseo
05:08
created

CartRegisterHandler::register()   C

Complexity

Conditions 15
Paths 10

Size

Total Lines 84
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 51
nc 10
nop 2
dl 0
loc 84
rs 5.9166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

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, 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);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...rceInterface::getCart() 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

94
        $response = $this->ecommerceApi->getCart(/** @scrutinizer ignore-type */ $storeId, $cartId);
Loading history...
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) {
0 ignored issues
show
introduced by
The condition $response !== false is always false.
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...rceInterface::addCart() 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

151
            $response = $this->ecommerceApi->addCart(/** @scrutinizer ignore-type */ $storeId, $data);
Loading history...
152
        } else {
153
            $response = $this->ecommerceApi->updateCart($storeId, $cartId, $data);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...Interface::updateCart() 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

153
            $response = $this->ecommerceApi->updateCart(/** @scrutinizer ignore-type */ $storeId, $cartId, $data);
Loading history...
154
        }
155
156
        return $response;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function unregister(OrderInterface $order)
163
    {
164
        if (!$this->enabled) {
165
            return false;
166
        }
167
168
        $orderId = (string) $order->getId();
169
        $channel = $order->getChannel();
170
171
        if (null == $channel) {
172
            return false;
173
        }
174
175
        $storeId = $channel->getCode();
176
177
        $response = $this->ecommerceApi->getCart($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...rceInterface::getCart() 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

177
        $response = $this->ecommerceApi->getCart(/** @scrutinizer ignore-type */ $storeId, $orderId);
Loading history...
178
        $isNew = !isset($response['id']);
179
180
        if (!$isNew) {
181
            return $this->ecommerceApi->removeCart($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...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

181
            return $this->ecommerceApi->removeCart(/** @scrutinizer ignore-type */ $storeId, $orderId);
Loading history...
182
        }
183
184
        return false;
185
    }
186
}
187