|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Rafał Muszyński <[email protected]> |
|
5
|
|
|
* @copyright 2013 Sourcefabric o.p.s. |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Newscoop\PaywallBundle\EventListener; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
12
|
|
|
use Newscoop\EventDispatcher\Events\GenericEvent; |
|
13
|
|
|
use Newscoop\PaywallBundle\Entity\Gateway; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Event lifecycle management. |
|
17
|
|
|
*/ |
|
18
|
|
|
class LifecycleSubscriber implements EventSubscriberInterface |
|
19
|
|
|
{ |
|
20
|
|
|
const PLUGIN_NAME = 'newscoop/newscoop-paywall-bundle'; |
|
21
|
|
|
|
|
22
|
|
|
private $em; |
|
23
|
|
|
private $dispatcher; |
|
24
|
|
|
private $scheduler; |
|
25
|
|
|
private $systemPreferences; |
|
26
|
|
|
private $classDir; |
|
27
|
|
|
private $pluginDir = '/../../../../'; |
|
28
|
|
|
private $cronjobs; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($em, $dispatcher, $scheduler, $systemPreferences) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->em = $em; |
|
33
|
|
|
$this->dispatcher = $dispatcher; |
|
34
|
|
|
$this->scheduler = $scheduler; |
|
35
|
|
|
$this->systemPreferences = $systemPreferences; |
|
36
|
|
|
$reflection = new \ReflectionClass($this); |
|
37
|
|
|
$this->classDir = $reflection->getFileName(); |
|
38
|
|
|
$this->pluginDir = dirname($this->classDir).$this->pluginDir; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function install(GenericEvent $event) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->em); |
|
44
|
|
|
$tool->updateSchema($this->getClasses(), true); |
|
45
|
|
|
$this->em->getProxyFactory()->generateProxyClasses( |
|
46
|
|
|
$this->getClasses(), |
|
47
|
|
|
$this->pluginDir.'library/Proxy' |
|
48
|
|
|
); |
|
49
|
|
|
$adapter = new Gateway(); |
|
50
|
|
|
$adapter->setName('PayPal_Express'); |
|
51
|
|
|
$adapter->setValue('PayPal_Express'); |
|
52
|
|
|
$this->em->persist($adapter); |
|
53
|
|
|
$this->em->flush(); |
|
54
|
|
|
|
|
55
|
|
|
$this->dispatcher->dispatch('newscoop_paywall.adapters.register', new GenericEvent()); |
|
56
|
|
|
|
|
57
|
|
|
$this->addJobs(); |
|
58
|
|
|
$this->systemPreferences->PaywallMembershipNotifyEmail = $this->systemPreferences->EmailFromAddress; |
|
59
|
|
|
$this->systemPreferences->PaywallMembershipNotifyFromEmail = $this->systemPreferences->EmailFromAddress; |
|
60
|
|
|
$this->systemPreferences->PaywallEmailNotifyEnabled = 0; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function update(GenericEvent $event) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->em); |
|
66
|
|
|
$tool->updateSchema($this->getClasses(), true); |
|
67
|
|
|
|
|
68
|
|
|
$this->dispatcher->dispatch('newscoop_paywall.adapters.register', new GenericEvent()); |
|
69
|
|
|
|
|
70
|
|
|
// Generate proxies for entities |
|
71
|
|
|
$this->em->getProxyFactory()->generateProxyClasses( |
|
72
|
|
|
$this->getClasses(), |
|
73
|
|
|
$this->pluginDir.'library/Proxy' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function remove(GenericEvent $event) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->em); |
|
80
|
|
|
$tool->dropSchema($this->getClasses(), true); |
|
81
|
|
|
$this->removeJobs(); |
|
82
|
|
|
$this->removeSettings(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Clean up system preferences. |
|
87
|
|
|
*/ |
|
88
|
|
|
private function removeSettings() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->systemPreferences->delete('PaywallMembershipNotifyEmail'); |
|
91
|
|
|
$this->systemPreferences->delete('PaywallEmailNotifyEnabled'); |
|
92
|
|
|
$this->systemPreferences->delete('PaywallMembershipNotifyFromEmail'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Add plugin cron jobs. |
|
97
|
|
|
*/ |
|
98
|
|
|
private function addJobs() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->setCronJobs(); |
|
101
|
|
|
foreach ($this->cronjobs as $jobName => $jobConfig) { |
|
102
|
|
|
$this->scheduler->registerJob($jobName, $jobConfig); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Remove plugin cron jobs. |
|
108
|
|
|
*/ |
|
109
|
|
|
private function removeJobs() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->setCronJobs(); |
|
112
|
|
|
foreach ($this->cronjobs as $jobName => $jobConfig) { |
|
113
|
|
|
$this->scheduler->removeJob($jobName, $jobConfig); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private function setCronJobs() |
|
118
|
|
|
{ |
|
119
|
|
|
$queryBuilder = $this->em->getRepository('Newscoop\Entity\Publication') |
|
120
|
|
|
->createQueryBuilder('p') |
|
121
|
|
|
->select('a.name') |
|
122
|
|
|
->leftJoin('p.defaultAlias', 'a') |
|
123
|
|
|
->setMaxResults(1); |
|
124
|
|
|
|
|
125
|
|
|
$alias = $queryBuilder->getQuery()->getOneOrNullResult(); |
|
126
|
|
|
|
|
127
|
|
|
if (!isset($alias['name'])) { |
|
128
|
|
|
throw new \RuntimeException('There is no alias defined! At least one alias needs to be defined.'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->cronjobs = array( |
|
132
|
|
|
'Sends email notifications for expiring subscriptions' => array( |
|
133
|
|
|
'command' => sprintf( |
|
134
|
|
|
'%s paywall:notifier:expiring %s', |
|
135
|
|
|
realpath($this->pluginDir.'application/console'), |
|
136
|
|
|
$alias['name'] |
|
137
|
|
|
), |
|
138
|
|
|
'schedule' => '0 2 * * *', |
|
139
|
|
|
), |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public static function getSubscribedEvents() |
|
144
|
|
|
{ |
|
145
|
|
|
return array( |
|
146
|
|
|
'plugin.install.newscoop_newscoop_paywall_bundle' => array('install', 1), |
|
147
|
|
|
'plugin.update.newscoop_newscoop_paywall_bundle' => array('update', 1), |
|
148
|
|
|
'plugin.remove.newscoop_newscoop_paywall_bundle' => array('remove', 1), |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
private function getClasses() |
|
153
|
|
|
{ |
|
154
|
|
|
return array( |
|
155
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Subscription'), |
|
156
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\SubscriptionSpecification'), |
|
157
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Gateway'), |
|
158
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\UserSubscription'), |
|
159
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Trial'), |
|
160
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Discount'), |
|
161
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Duration'), |
|
162
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Order'), |
|
163
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Modification'), |
|
164
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Currency'), |
|
165
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\SubscriptionTranslation'), |
|
166
|
|
|
$this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Payment'), |
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.