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.

unsubscribe()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 10
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 19
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusMailchimpPlugin\Handler;
6
7
use Odiseo\SyliusMailchimpPlugin\Api\ListsInterface;
8
use Sylius\Component\Core\Model\CustomerInterface;
9
use Symfony\Component\EventDispatcher\GenericEvent;
10
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
11
12
final class CustomerNewsletterSubscriptionHandler implements CustomerNewsletterSubscriptionHandlerInterface
13
{
14
    /** @var ListsInterface */
15
    private $listsApi;
16
17
    /** @var bool */
18
    private $enabled;
19
20
    /** @var EventDispatcherInterface */
21
    private $eventDispatcher;
22
23
    public function __construct(
24
        ListsInterface $listsApi,
25
        EventDispatcherInterface $eventDispatcher,
26
        bool $enabled
27
    ) {
28
        $this->listsApi = $listsApi;
29
        $this->eventDispatcher = $eventDispatcher;
30
        $this->enabled = $enabled;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function subscribe(CustomerInterface $customer, string $listId)
37
    {
38
        if (!$this->enabled) {
39
            return false;
40
        }
41
42
        $subscriberHash = md5(strtolower($customer->getEmail()));
0 ignored issues
show
Bug introduced by
It seems like $customer->getEmail() can also be of type null; however, parameter $string of strtolower() 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

42
        $subscriberHash = md5(strtolower(/** @scrutinizer ignore-type */ $customer->getEmail()));
Loading history...
43
44
        $getMemberResponse = $this->listsApi->getMember($listId, $subscriberHash);
45
        $isNew = !isset($getMemberResponse['id']);
46
47
        $data = [
48
            'email_address' => $customer->getEmail(),
49
            'status' => 'subscribed',
50
        ];
51
52
        if ($isNew) {
53
            $event = new GenericEvent($customer, ['data' => $data]);
54
            $this->eventDispatcher->dispatch($event, 'mailchimp.customer_newsletter.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_newsletter.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

54
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
55
                                    dispatch($event, 'mailchimp.customer_newsletter.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...
55
            $data = $event->getArgument('data');
56
57
            $response = $this->listsApi->addMember($listId, $data);
58
        } else {
59
            $event = new GenericEvent($customer, ['data' => $data, 'existing_mailchimp_member_data' => $getMemberResponse]);
60
            $this->eventDispatcher->dispatch($event, 'mailchimp.customer_newsletter.pre_update');
61
            $data = $event->getArgument('data');
62
63
            $response = $this->listsApi->updateMember($listId, $subscriberHash, $data);
64
        }
65
66
        return $response;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function unsubscribe(CustomerInterface $customer, string $listId)
73
    {
74
        if (!$this->enabled) {
75
            return false;
76
        }
77
78
        $subscriberHash = md5(strtolower($customer->getEmail()));
0 ignored issues
show
Bug introduced by
It seems like $customer->getEmail() can also be of type null; however, parameter $string of strtolower() 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

78
        $subscriberHash = md5(strtolower(/** @scrutinizer ignore-type */ $customer->getEmail()));
Loading history...
79
80
        $response = $this->listsApi->getMember($listId, $subscriberHash);
81
        $isNew = !isset($response['id']);
82
83
        if (!$isNew) {
84
            $event = new GenericEvent($customer, ['listId' => $listId]);
85
            $this->eventDispatcher->dispatch($event, 'mailchimp.customer_newsletter.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_newsletter.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

85
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
86
                                    dispatch($event, 'mailchimp.customer_newsletter.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...
86
87
            return $this->listsApi->removeMember($listId, $subscriberHash);
88
        }
89
90
        return false;
91
    }
92
}
93