1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace Odiseo\SyliusMailchimpPlugin\Handler; |
||||||
6 | |||||||
7 | use Odiseo\SyliusMailchimpPlugin\Api\EcommerceInterface; |
||||||
8 | use Odiseo\SyliusMailchimpPlugin\Entity\MailchimpListIdAwareInterface; |
||||||
9 | use Odiseo\SyliusMailchimpPlugin\Provider\ListIdProviderInterface; |
||||||
10 | use Sylius\Component\Core\Model\ChannelInterface; |
||||||
11 | use Symfony\Component\EventDispatcher\GenericEvent; |
||||||
12 | use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
||||||
13 | |||||||
14 | final class StoreRegisterHandler implements StoreRegisterHandlerInterface |
||||||
15 | { |
||||||
16 | /** |
||||||
17 | * @var EcommerceInterface |
||||||
18 | */ |
||||||
19 | private $ecommerceApi; |
||||||
20 | |||||||
21 | /** |
||||||
22 | * @var ListIdProviderInterface |
||||||
23 | */ |
||||||
24 | private $listIdProvider; |
||||||
25 | |||||||
26 | /** @var EventDispatcherInterface */ |
||||||
27 | private $eventDispatcher; |
||||||
28 | |||||||
29 | /** |
||||||
30 | * @var bool |
||||||
31 | */ |
||||||
32 | private $enabled; |
||||||
33 | |||||||
34 | /** |
||||||
35 | * @param EcommerceInterface $ecommerceApi |
||||||
36 | * @param ListIdProviderInterface $listIdProvider |
||||||
37 | * @param EventDispatcherInterface $eventDispatcher |
||||||
38 | * @param bool $enabled |
||||||
39 | */ |
||||||
40 | public function __construct( |
||||||
41 | EcommerceInterface $ecommerceApi, |
||||||
42 | ListIdProviderInterface $listIdProvider, |
||||||
43 | EventDispatcherInterface $eventDispatcher, |
||||||
44 | bool $enabled |
||||||
45 | ) { |
||||||
46 | $this->ecommerceApi = $ecommerceApi; |
||||||
47 | $this->listIdProvider = $listIdProvider; |
||||||
48 | $this->eventDispatcher = $eventDispatcher; |
||||||
49 | $this->enabled = $enabled; |
||||||
50 | } |
||||||
51 | |||||||
52 | /** |
||||||
53 | * {@inheritdoc} |
||||||
54 | */ |
||||||
55 | public function register(ChannelInterface $channel, bool $isSyncing = false) |
||||||
56 | { |
||||||
57 | if (!$this->enabled) { |
||||||
58 | return false; |
||||||
59 | } |
||||||
60 | |||||||
61 | $storeId = $channel->getCode(); |
||||||
62 | |||||||
63 | $response = $this->ecommerceApi->getStore($storeId); |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
64 | $isNew = !isset($response['id']); |
||||||
65 | |||||||
66 | $localeCode = 'en'; |
||||||
67 | $currencyCode = 'USD'; |
||||||
68 | |||||||
69 | if ($defaultLocale = $channel->getDefaultLocale()) { |
||||||
70 | $localeCode = $defaultLocale->getCode(); |
||||||
71 | } |
||||||
72 | |||||||
73 | if ($baseCurrency = $channel->getBaseCurrency()) { |
||||||
74 | $currencyCode = $baseCurrency->getCode(); |
||||||
75 | } |
||||||
76 | |||||||
77 | $data = [ |
||||||
78 | 'id' => $storeId, |
||||||
79 | 'list_id' => $this->getListIdByChannel($channel), |
||||||
80 | 'name' => $channel->getName(), |
||||||
81 | 'platform' => 'Sylius', |
||||||
82 | 'domain' => $channel->getHostname(), |
||||||
83 | 'is_syncing' => $isSyncing, |
||||||
84 | 'email_address' => $channel->getContactEmail(), |
||||||
85 | 'currency_code' => $currencyCode, |
||||||
86 | 'primary_locale' => $localeCode, |
||||||
87 | ]; |
||||||
88 | |||||||
89 | if ($isNew) { |
||||||
90 | $event = new GenericEvent($channel, ['data' => $data]); |
||||||
91 | $this->eventDispatcher->dispatch($event, 'mailchimp.store.pre_add'); |
||||||
0 ignored issues
–
show
The call to
Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with 'mailchimp.store.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.
Loading history...
|
|||||||
92 | $data = $event->getArgument('data'); |
||||||
93 | |||||||
94 | $response = $this->ecommerceApi->addStore($data); |
||||||
95 | } else { |
||||||
96 | $event = new GenericEvent($channel, ['data' => $data]); |
||||||
97 | $this->eventDispatcher->dispatch($event, 'mailchimp.store.pre_update'); |
||||||
98 | $data = $event->getArgument('data'); |
||||||
99 | |||||||
100 | $response = $this->ecommerceApi->updateStore($storeId, $data); |
||||||
0 ignored issues
–
show
It seems like
$storeId can also be of type null ; however, parameter $storeId of Odiseo\SyliusMailchimpPl...nterface::updateStore() 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
Loading history...
|
|||||||
101 | } |
||||||
102 | |||||||
103 | return $response; |
||||||
104 | } |
||||||
105 | |||||||
106 | /** |
||||||
107 | * {@inheritdoc} |
||||||
108 | */ |
||||||
109 | public function unregister(ChannelInterface $channel) |
||||||
110 | { |
||||||
111 | if (!$this->enabled) { |
||||||
112 | return false; |
||||||
113 | } |
||||||
114 | |||||||
115 | $storeId = $channel->getCode(); |
||||||
116 | |||||||
117 | $response = $this->ecommerceApi->getStore($storeId); |
||||||
0 ignored issues
–
show
It seems like
$storeId can also be of type null ; however, parameter $storeId of Odiseo\SyliusMailchimpPl...ceInterface::getStore() 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
Loading history...
|
|||||||
118 | $isNew = !isset($response['id']); |
||||||
119 | |||||||
120 | if (!$isNew) { |
||||||
121 | $event = new GenericEvent($channel); |
||||||
122 | $this->eventDispatcher->dispatch($event, 'mailchimp.store.pre_remove'); |
||||||
0 ignored issues
–
show
The call to
Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with 'mailchimp.store.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.
Loading history...
|
|||||||
123 | |||||||
124 | return $this->ecommerceApi->removeStore($storeId); |
||||||
0 ignored issues
–
show
It seems like
$storeId can also be of type null ; however, parameter $storeId of Odiseo\SyliusMailchimpPl...nterface::removeStore() 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
Loading history...
|
|||||||
125 | } |
||||||
126 | |||||||
127 | return false; |
||||||
128 | } |
||||||
129 | |||||||
130 | /** |
||||||
131 | * @param ChannelInterface $channel |
||||||
132 | * |
||||||
133 | * @return string |
||||||
134 | */ |
||||||
135 | private function getListIdByChannel(ChannelInterface $channel): string |
||||||
136 | { |
||||||
137 | if ($channel instanceof MailchimpListIdAwareInterface) { |
||||||
138 | if ($listId = $channel->getListId()) { |
||||||
139 | return $listId; |
||||||
140 | } |
||||||
141 | } |
||||||
142 | |||||||
143 | return $this->listIdProvider->getListId(); |
||||||
144 | } |
||||||
145 | } |
||||||
146 |