|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Rafał Muszyński <[email protected]> |
|
5
|
|
|
* @copyright 2014 Sourcefabric z.ú. |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Newscoop\PaywallBundle\EventListener; |
|
10
|
|
|
|
|
11
|
|
|
use Newscoop\EventDispatcher\Events\GenericEvent; |
|
12
|
|
|
use Newscoop\PaywallBundle\Services\NotificationsService; |
|
13
|
|
|
use Newscoop\PaywallBundle\Notifications\Emails; |
|
14
|
|
|
use Newscoop\PaywallBundle\Entity\UserSubscription; |
|
15
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Notifications listener. |
|
19
|
|
|
*/ |
|
20
|
|
|
class NotificationListener |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Notifications service. |
|
24
|
|
|
* |
|
25
|
|
|
* @var NotificationsService |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $notificationsService; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(NotificationsService $notificationsService) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->notificationsService = $notificationsService; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Sends confirmation email to user. |
|
36
|
|
|
* |
|
37
|
|
|
* @param GenericEvent $event |
|
38
|
|
|
*/ |
|
39
|
|
|
public function sendUserNotificationEmail(GenericEvent $event) |
|
40
|
|
|
{ |
|
41
|
|
|
$subscription = $event->getSubject(); |
|
42
|
|
|
$this->sendValidatedNotification(Emails::USER_CONFIRMATION, $subscription); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sends confirmation email with informations |
|
47
|
|
|
* about created subscription for user, by admin |
|
48
|
|
|
* in backend. |
|
49
|
|
|
* |
|
50
|
|
|
* @param GenericEvent $event |
|
51
|
|
|
*/ |
|
52
|
|
|
public function sendAdminCreatedNotification(GenericEvent $event) |
|
53
|
|
|
{ |
|
54
|
|
|
$subscription = $event->getSubject(); |
|
55
|
|
|
$this->sendValidatedNotification(Emails::ADMIN_CREATED_CONFIRMATION, $subscription); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Sends notification email to admin. |
|
60
|
|
|
* |
|
61
|
|
|
* @param GenericEvent $event |
|
62
|
|
|
*/ |
|
63
|
|
|
public function sendAdminNotificationEmail(GenericEvent $event) |
|
64
|
|
|
{ |
|
65
|
|
|
$subscriptions = $event->getSubject(); |
|
66
|
|
|
$this->isValid($subscriptions); |
|
67
|
|
|
$user = $this->getUser($subscriptions); |
|
68
|
|
|
$this->notificationsService->sendNotification( |
|
69
|
|
|
Emails::SUBSCRIPTION_CONFIRMATION, |
|
70
|
|
|
array(), |
|
71
|
|
|
array( |
|
72
|
|
|
'subscriptions' => $subscriptions, |
|
73
|
|
|
'user' => $user, |
|
74
|
|
|
) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sends confirmation email to user when subscription |
|
80
|
|
|
* status is changed in paywall admin panel. |
|
81
|
|
|
* |
|
82
|
|
|
* @param GenericEvent $event |
|
83
|
|
|
*/ |
|
84
|
|
|
public function sendUserSubscriptionStatusChangeEmail(GenericEvent $event) |
|
85
|
|
|
{ |
|
86
|
|
|
$subscription = $event->getSubject(); |
|
87
|
|
|
$this->sendValidatedNotification(Emails::SUBSCRIPTION_STATUS, $subscription); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Sends an emails with the informations about expiring |
|
92
|
|
|
* subscription. |
|
93
|
|
|
* |
|
94
|
|
|
* @param GenericEvent $event |
|
95
|
|
|
*/ |
|
96
|
|
|
public function sendSubscriptionExpirationEmail(GenericEvent $event) |
|
97
|
|
|
{ |
|
98
|
|
|
$subscription = $event->getSubject(); |
|
99
|
|
|
$this->sendValidatedNotification(Emails::SUBSCRIPTION_EXPIRATION, $subscription); |
|
100
|
|
|
if (!$subscription->getNotifySentLevelOne()) { |
|
101
|
|
|
$this->notificationsService->setSentDateTimeOnLevelOne($subscription); |
|
102
|
|
|
} else { |
|
103
|
|
|
$this->notificationsService->setSentDateTimeOnLevelTwo($subscription); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function sendValidatedNotification($code, $subscription) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->isValid($subscription); |
|
110
|
|
|
$user = $this->getUser($subscription); |
|
111
|
|
|
$this->notificationsService->sendNotification( |
|
112
|
|
|
$code, |
|
113
|
|
|
array($user->getEmail()), |
|
114
|
|
|
array( |
|
115
|
|
|
'subscriptions' => $subscription, |
|
116
|
|
|
'user' => $user, |
|
117
|
|
|
) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function isValid($subscriptions) |
|
122
|
|
|
{ |
|
123
|
|
|
if (is_array($subscriptions)) { |
|
124
|
|
|
foreach ($subscriptions as $key => $subscription) { |
|
125
|
|
|
$this->isSupported($subscription); |
|
126
|
|
|
} |
|
127
|
|
|
} else { |
|
128
|
|
|
$this->isSupported($subscriptions); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function isSupported($subscription) |
|
133
|
|
|
{ |
|
134
|
|
|
if (!$subscription instanceof UserSubscription) { |
|
135
|
|
|
throw new UnexpectedTypeException( |
|
136
|
|
|
$subscription, |
|
137
|
|
|
'PaywallBundle\Entity\UserSubscription' |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private function getUser($subscription) |
|
143
|
|
|
{ |
|
144
|
|
|
$user = null; |
|
|
|
|
|
|
145
|
|
|
if (is_array($subscription)) { |
|
146
|
|
|
$user = $subscription[0]->getUser(); |
|
147
|
|
|
} else { |
|
148
|
|
|
$user = $subscription->getUser(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $user; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.