1 | <?php |
||||
2 | |||||
3 | namespace MailMotor\Bundle\MailMotorBundle\Helper; |
||||
4 | |||||
5 | use MailMotor\Bundle\MailMotorBundle\Event\MailMotorSubscribedEvent; |
||||
6 | use MailMotor\Bundle\MailMotorBundle\Event\MailMotorUnsubscribedEvent; |
||||
7 | use MailMotor\Bundle\MailMotorBundle\Gateway\SubscriberGateway; |
||||
8 | use MailMotor\Bundle\MailMotorBundle\MailMotor; |
||||
9 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||||
10 | |||||
11 | /** |
||||
12 | * Subscriber - this is the class that will be used in the web app. |
||||
13 | * |
||||
14 | * @author Jeroen Desloovere <[email protected]> |
||||
15 | */ |
||||
16 | final class Subscriber extends MailMotor |
||||
17 | { |
||||
18 | public const MEMBER_STATUS_SUBSCRIBED = 'subscribed'; |
||||
19 | public const MEMBER_STATUS_UNSUBSCRIBED = 'unsubscribed'; |
||||
20 | |||||
21 | /** @var EventDispatcherInterface */ |
||||
22 | protected $eventDispatcher; |
||||
23 | |||||
24 | /** @var SubscriberGateway */ |
||||
25 | protected $subscriberGateway; |
||||
26 | |||||
27 | public function __construct( |
||||
28 | SubscriberGateway $subscriberGateway, |
||||
29 | EventDispatcherInterface $eventDispatcher, |
||||
30 | string $listId = null |
||||
31 | ) { |
||||
32 | parent::__construct($listId); |
||||
33 | $this->subscriberGateway = $subscriberGateway; |
||||
34 | $this->eventDispatcher = $eventDispatcher; |
||||
35 | } |
||||
36 | |||||
37 | public function exists(string $email, string $listId = null): bool |
||||
38 | { |
||||
39 | return (bool) $this->subscriberGateway->exists( |
||||
40 | $email, |
||||
41 | $this->getListId($listId) |
||||
42 | ); |
||||
43 | } |
||||
44 | |||||
45 | public function getInterests(string $listId = null): array |
||||
46 | { |
||||
47 | return (array) $this->subscriberGateway->getInterests( |
||||
48 | $this->getListId($listId) |
||||
49 | ); |
||||
50 | } |
||||
51 | |||||
52 | public function isSubscribed(string $email, string $listId = null): bool |
||||
53 | { |
||||
54 | return $this->subscriberGateway->hasStatus( |
||||
55 | $email, |
||||
56 | $this->getListId($listId), |
||||
57 | self::MEMBER_STATUS_SUBSCRIBED |
||||
58 | ); |
||||
59 | } |
||||
60 | |||||
61 | public function isUnsubscribed(string $email, string $listId = null): bool |
||||
62 | { |
||||
63 | return $this->subscriberGateway->hasStatus( |
||||
64 | $email, |
||||
65 | $this->getListId($listId), |
||||
66 | self::MEMBER_STATUS_UNSUBSCRIBED |
||||
67 | ); |
||||
68 | } |
||||
69 | |||||
70 | public function ping(): bool |
||||
71 | { |
||||
72 | return $this->subscriberGateway->ping($this->getListId()); |
||||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * Subscribe |
||||
77 | * |
||||
78 | * @param string $email |
||||
79 | * @param string $language |
||||
80 | * @param array $mergeFields |
||||
81 | * @param array $interests The array is like: ['9AS489SQF' => true, '4SDF8S9DF1' => false] |
||||
82 | * @param bool $doubleOptin Members need to validate their emailAddress before they get added to the list |
||||
83 | * @param string $listId |
||||
84 | * @return boolean |
||||
85 | */ |
||||
86 | public function subscribe( |
||||
87 | string $email, |
||||
88 | string $language = null, |
||||
89 | array $mergeFields = array(), |
||||
90 | array $interests = array(), |
||||
91 | bool $doubleOptin = true, |
||||
92 | string $listId = null |
||||
93 | ): bool { |
||||
94 | $subscribed = $this->subscriberGateway->subscribe( |
||||
95 | $email, |
||||
96 | $this->getListId($listId), |
||||
97 | $language, |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
98 | $mergeFields, |
||||
99 | $interests, |
||||
100 | $doubleOptin |
||||
101 | ); |
||||
102 | |||||
103 | if ($subscribed) { |
||||
104 | // dispatch subscribed event |
||||
105 | $this->eventDispatcher->dispatch( |
||||
106 | new MailMotorSubscribedEvent( |
||||
107 | $email, |
||||
108 | $this->getListId($listId), |
||||
109 | $language, |
||||
0 ignored issues
–
show
It seems like
$language can also be of type null ; however, parameter $language of MailMotor\Bundle\MailMot...bedEvent::__construct() 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
![]() |
|||||
110 | $mergeFields, |
||||
111 | $interests, |
||||
112 | $doubleOptin |
||||
113 | ), |
||||
114 | MailMotorSubscribedEvent::EVENT_NAME |
||||
115 | ); |
||||
116 | } |
||||
117 | |||||
118 | return $subscribed; |
||||
119 | } |
||||
120 | |||||
121 | public function unsubscribe(string $email, string $listId = null): bool |
||||
122 | { |
||||
123 | $unsubscribed = $this->subscriberGateway->unsubscribe( |
||||
124 | $email, |
||||
125 | $this->getListId($listId) |
||||
126 | ); |
||||
127 | |||||
128 | if ($unsubscribed) { |
||||
129 | // dispatch unsubscribed event |
||||
130 | $this->eventDispatcher->dispatch( |
||||
131 | new MailMotorUnsubscribedEvent( |
||||
132 | $email, |
||||
133 | $this->getListId($listId) |
||||
134 | ), |
||||
135 | MailMotorUnsubscribedEvent::EVENT_NAME |
||||
136 | ); |
||||
137 | } |
||||
138 | |||||
139 | return $unsubscribed; |
||||
140 | } |
||||
141 | } |
||||
142 |