Completed
Pull Request — master (#32)
by Rafał
03:20
created

LifecycleSubscriber   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 20
Bugs 5 Features 5
Metric Value
wmc 15
c 20
b 5
f 5
lcom 1
cbo 1
dl 0
loc 176
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A install() 0 23 1
A update() 0 15 1
A remove() 0 8 1
A removeSettings() 0 6 1
A addJobs() 0 7 2
A removeJobs() 0 7 2
B setCronJobs() 0 25 2
A getSubscribedEvents() 0 8 1
A removePermissions() 0 4 1
A setPermissions() 0 4 1
A getClasses() 0 17 1
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
    private $pluginsService;
30
    private $translator;
31
32
    public function __construct($em, $dispatcher, $scheduler, $systemPreferences, $pluginsService, $translator)
33
    {
34
        $this->em = $em;
35
        $this->dispatcher = $dispatcher;
36
        $this->scheduler = $scheduler;
37
        $this->systemPreferences = $systemPreferences;
38
        $reflection = new \ReflectionClass($this);
39
        $this->classDir = $reflection->getFileName();
40
        $this->pluginDir = dirname($this->classDir).$this->pluginDir;
41
        $this->pluginsService = $pluginsService;
42
        $this->translator = $translator;
43
    }
44
45
    public function install(GenericEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
48
        $tool->updateSchema($this->getClasses(), true);
49
        $this->em->getProxyFactory()->generateProxyClasses(
50
            $this->getClasses(),
51
            $this->pluginDir.'library/Proxy'
52
        );
53
54
        $adapter = new Gateway();
55
        $adapter->setName('Offline');
56
        $adapter->setValue('offline');
57
        $this->em->persist($adapter);
58
        $this->em->flush();
59
60
        $this->dispatcher->dispatch('newscoop_paywall.adapters.register', new GenericEvent());
61
62
        $this->addJobs();
63
        $this->systemPreferences->PaywallMembershipNotifyEmail = $this->systemPreferences->EmailFromAddress;
64
        $this->systemPreferences->PaywallMembershipNotifyFromEmail = $this->systemPreferences->EmailFromAddress;
65
        $this->systemPreferences->PaywallEmailNotifyEnabled = 0;
66
        $this->setPermissions();
67
    }
68
69
    public function update(GenericEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
72
        $tool->updateSchema($this->getClasses(), true);
73
74
        $this->dispatcher->dispatch('newscoop_paywall.adapters.register', new GenericEvent());
75
76
        // Generate proxies for entities
77
        $this->em->getProxyFactory()->generateProxyClasses(
78
            $this->getClasses(),
79
            $this->pluginDir.'library/Proxy'
80
        );
81
82
        $this->setPermissions();
83
    }
84
85
    public function remove(GenericEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
88
        $tool->dropSchema($this->getClasses(), true);
89
        $this->removeJobs();
90
        $this->removeSettings();
91
        $this->removePermissions();
92
    }
93
94
    /**
95
     * Clean up system preferences.
96
     */
97
    private function removeSettings()
98
    {
99
        $this->systemPreferences->delete('PaywallMembershipNotifyEmail');
100
        $this->systemPreferences->delete('PaywallEmailNotifyEnabled');
101
        $this->systemPreferences->delete('PaywallMembershipNotifyFromEmail');
102
    }
103
104
    /**
105
     * Add plugin cron jobs.
106
     */
107
    private function addJobs()
108
    {
109
        $this->setCronJobs();
110
        foreach ($this->cronjobs as $jobName => $jobConfig) {
111
            $this->scheduler->registerJob($jobName, $jobConfig);
112
        }
113
    }
114
115
    /**
116
     * Remove plugin cron jobs.
117
     */
118
    private function removeJobs()
119
    {
120
        $this->setCronJobs();
121
        foreach ($this->cronjobs as $jobName => $jobConfig) {
122
            $this->scheduler->removeJob($jobName, $jobConfig);
123
        }
124
    }
125
126
    private function setCronJobs()
127
    {
128
        $queryBuilder = $this->em->getRepository('Newscoop\Entity\Publication')
129
            ->createQueryBuilder('p')
130
            ->select('a.name')
131
            ->leftJoin('p.defaultAlias', 'a')
132
            ->setMaxResults(1);
133
134
        $alias = $queryBuilder->getQuery()->getOneOrNullResult();
135
136
        if (!isset($alias['name'])) {
137
            throw new \RuntimeException('There is no alias defined! At least one alias needs to be defined.');
138
        }
139
140
        $this->cronjobs = array(
141
            'Sends email notifications for expiring subscriptions' => array(
142
                'command' => sprintf(
143
                    '%s paywall:notifier:expiring %s',
144
                    realpath($this->pluginDir.'application/console'),
145
                    $alias['name']
146
                ),
147
                'schedule' => '0 2 * * *',
148
            ),
149
        );
150
    }
151
152
    public static function getSubscribedEvents()
153
    {
154
        return array(
155
            'plugin.install.newscoop_newscoop_paywall_bundle' => array('install', 1),
156
            'plugin.update.newscoop_newscoop_paywall_bundle' => array('update', 1),
157
            'plugin.remove.newscoop_newscoop_paywall_bundle' => array('remove', 1),
158
        );
159
    }
160
161
    /**
162
     * Remove plugin permissions.
163
     */
164
    private function removePermissions()
165
    {
166
        $this->pluginsService->removePluginPermissions($this->pluginsService->collectPermissions($this->translator->trans('paywall.title')));
167
    }
168
    /**
169
     * Collect plugin permissions.
170
     */
171
    private function setPermissions()
172
    {
173
        $this->pluginsService->savePluginPermissions($this->pluginsService->collectPermissions($this->translator->trans('paywall.title')));
174
    }
175
176
    private function getClasses()
177
    {
178
        return array(
179
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Subscription'),
180
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\SubscriptionSpecification'),
181
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Gateway'),
182
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\UserSubscription'),
183
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Trial'),
184
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Discount'),
185
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Duration'),
186
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Order'),
187
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Modification'),
188
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Currency'),
189
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\SubscriptionTranslation'),
190
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Payment'),
191
        );
192
    }
193
}
194