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.

CustomerRegisterHandler::register()   C
last analyzed

Complexity

Conditions 15
Paths 6

Size

Total Lines 59
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 15
eloc 37
c 1
b 0
f 1
nc 6
nop 4
dl 0
loc 59
rs 5.9166

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 Odiseo\SyliusMailchimpPlugin\Api\EcommerceInterface;
8
use Sylius\Component\Core\Model\AddressInterface;
9
use Sylius\Component\Core\Model\ChannelInterface;
10
use Sylius\Component\Core\Model\CustomerInterface;
11
use Symfony\Component\EventDispatcher\GenericEvent;
12
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
13
14
final class CustomerRegisterHandler implements CustomerRegisterHandlerInterface
15
{
16
    /** @var EcommerceInterface */
17
    private $ecommerceApi;
18
19
    /** @var EventDispatcherInterface */
20
    private $eventDispatcher;
21
22
    /** @var bool */
23
    private $enabled;
24
25
    public function __construct(
26
        EcommerceInterface $ecommerceApi,
27
        EventDispatcherInterface $eventDispatcher,
28
        bool $enabled
29
    ) {
30
        $this->ecommerceApi = $ecommerceApi;
31
        $this->eventDispatcher = $eventDispatcher;
32
        $this->enabled = $enabled;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function register(
39
        CustomerInterface $customer,
40
        ChannelInterface $channel,
41
        bool $optInStatus = false,
42
        bool $createOnly = false
43
    ) {
44
        if (!$this->enabled) {
45
            return false;
46
        }
47
48
        $customerId = (string) $customer->getId();
49
        $storeId = $channel->getCode();
50
        $customerAddress = $this->getCustomerAddress($customer);
51
        $firstName = $this->getCustomerFirstName($customer, $customerAddress);
52
        $lastName = $this->getCustomerLastName($customer, $customerAddress);
53
54
        $response = $this->ecommerceApi->getCustomer($storeId, $customerId);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...nterface::getCustomer() 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

54
        $response = $this->ecommerceApi->getCustomer(/** @scrutinizer ignore-type */ $storeId, $customerId);
Loading history...
55
        $isNew = !isset($response['id']);
56
57
        // Do nothing if the customer exists
58
        if (false === $isNew && true === $createOnly) {
59
            return false;
60
        }
61
62
        $data = [
63
            'id' => $customerId,
64
            'email_address' => $customer->getEmail(),
65
            'opt_in_status' => $optInStatus,
66
            'first_name' => $firstName ?: '',
67
            'last_name' => $lastName ?: '',
68
        ];
69
70
        if ($customerAddress) {
0 ignored issues
show
introduced by
$customerAddress is of type Sylius\Component\Core\Model\AddressInterface, thus it always evaluated to true.
Loading history...
71
            $data['company'] = $customerAddress->getCompany() ?: '';
72
            $data['address'] = [
73
                'address1' => $customerAddress->getStreet() ?: '',
74
                'city' => $customerAddress->getCity() ?: '',
75
                'province' => $customerAddress->getProvinceName() ?: '',
76
                'province_code' => $customerAddress->getProvinceCode() ?: '',
77
                'postal_code' => $customerAddress->getPostcode() ?: '',
78
                'country_code' => $customerAddress->getCountryCode() ?: '',
79
            ];
80
        }
81
82
        if ($isNew) {
83
            $event = new GenericEvent($customer, ['data' => $data, 'channel' => $channel]);
84
            $this->eventDispatcher->dispatch($event, 'mailchimp.customer.pre_add');
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with 'mailchimp.customer.pre_add'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
85
                                    dispatch($event, 'mailchimp.customer.pre_add');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
85
            $data = $event->getArgument('data');
86
87
            $response = $this->ecommerceApi->addCustomer($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...nterface::addCustomer() 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

87
            $response = $this->ecommerceApi->addCustomer(/** @scrutinizer ignore-type */ $storeId, $data);
Loading history...
88
        } else {
89
            $event = new GenericEvent($customer, ['data' => $data, 'channel' => $channel]);
90
            $this->eventDispatcher->dispatch($event, 'mailchimp.customer.pre_update');
91
            $data = $event->getArgument('data');
92
93
            $response = $this->ecommerceApi->updateCustomer($storeId, $customerId, $data);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...rface::updateCustomer() 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

93
            $response = $this->ecommerceApi->updateCustomer(/** @scrutinizer ignore-type */ $storeId, $customerId, $data);
Loading history...
94
        }
95
96
        return $response;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function unregister(CustomerInterface $customer, ChannelInterface $channel)
103
    {
104
        if (!$this->enabled) {
105
            return false;
106
        }
107
108
        $customerId = (string) $customer->getId();
109
        $storeId = $channel->getCode();
110
111
        $response = $this->ecommerceApi->getCustomer($storeId, $customerId);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...nterface::getCustomer() 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

111
        $response = $this->ecommerceApi->getCustomer(/** @scrutinizer ignore-type */ $storeId, $customerId);
Loading history...
112
        $isNew = !isset($response['id']);
113
114
        if (!$isNew) {
115
            $event = new GenericEvent($customer, ['channel' => $channel]);
116
            $this->eventDispatcher->dispatch($event, 'mailchimp.customer.pre_remove');
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with 'mailchimp.customer.pre_remove'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
117
                                    dispatch($event, 'mailchimp.customer.pre_remove');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
117
118
            return $this->ecommerceApi->removeCustomer($storeId, $customerId);
0 ignored issues
show
Bug introduced by
It seems like $storeId can also be of type null; however, parameter $storeId of Odiseo\SyliusMailchimpPl...rface::removeCustomer() 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

118
            return $this->ecommerceApi->removeCustomer(/** @scrutinizer ignore-type */ $storeId, $customerId);
Loading history...
119
        }
120
121
        return false;
122
    }
123
124
    /**
125
     * @param CustomerInterface $customer
126
     * @return AddressInterface|null
127
     */
128
    private function getCustomerAddress(CustomerInterface $customer): ?AddressInterface
129
    {
130
        $address = $customer->getDefaultAddress();
131
132
        if (!$address && count($customer->getAddresses()) > 0) {
133
            $address = $customer->getAddresses()->first();
134
        }
135
136
        return $address;
137
    }
138
139
    /**
140
     * @param CustomerInterface $customer
141
     * @param AddressInterface|null $address
142
     * @return string|null
143
     */
144
    private function getCustomerFirstName(CustomerInterface $customer, AddressInterface $address = null): ?string
145
    {
146
        $firstName = $customer->getFirstName();
147
148
        if (!$firstName && $address) {
149
            $firstName = $address->getFirstName();
150
        }
151
152
        return $firstName;
153
    }
154
155
    /**
156
     * @param CustomerInterface $customer
157
     * @param AddressInterface|null $address
158
     * @return string|null
159
     */
160
    private function getCustomerLastName(CustomerInterface $customer, AddressInterface $address = null): ?string
161
    {
162
        $lastName = $customer->getLastName();
163
164
        if (!$lastName && $address) {
165
            $lastName = $address->getLastName();
166
        }
167
168
        return $lastName;
169
    }
170
}
171