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
![]() |
|||||||
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
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
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. ![]() |
|||||||
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
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
![]() |
|||||||
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
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
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. ![]() |
|||||||
86 | |||||||
87 | return $this->listsApi->removeMember($listId, $subscriberHash); |
||||||
88 | } |
||||||
89 | |||||||
90 | return false; |
||||||
91 | } |
||||||
92 | } |
||||||
93 |