Completed
Pull Request — master (#24)
by Rafał
03:09
created

LifecycleSubscriber::setCronJobs()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 31
rs 8.8571
cc 2
eloc 19
nc 2
nop 0
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
namespace Newscoop\PaywallBundle\EventListener;
9
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
use Newscoop\EventDispatcher\Events\GenericEvent;
12
use Newscoop\PaywallBundle\Entity\Gateway;
13
14
/**
15
 * Event lifecycle management.
16
 */
17
class LifecycleSubscriber implements EventSubscriberInterface
18
{
19
    const PLUGIN_NAME = 'newscoop/newscoop-paywall-bundle';
20
21
    private $em;
22
    private $dispatcher;
23
    private $scheduler;
24
    private $systemPreferences;
25
    private $classDir;
26
    private $pluginDir = '/../../../../';
27
28
    public function __construct($em, $dispatcher, $scheduler, $systemPreferences)
29
    {
30
        $this->em = $em;
31
        $this->dispatcher = $dispatcher;
32
        $this->scheduler = $scheduler;
33
        $this->systemPreferences = $systemPreferences;
34
        $reflection = new \ReflectionClass($this);
35
        $this->classDir = $reflection->getFileName();
36
        $this->pluginDir = dirname($this->classDir).$this->pluginDir;
37
    }
38
39
    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...
40
    {
41
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
42
        $tool->updateSchema($this->getClasses(), true);
43
        $this->em->getProxyFactory()->generateProxyClasses(
44
            $this->getClasses(),
45
            $this->pluginDir.'library/Proxy'
46
        );
47
        $adapter = new Gateway();
48
        $adapter->setName('PayPal_Express');
49
        $adapter->setValue('PayPal_Express');
50
        $this->em->persist($adapter);
51
        $this->em->flush();
52
53
        $this->dispatcher->dispatch('newscoop_paywall.adapters.register', new GenericEvent());
54
55
        $this->addJobs();
56
        $this->systemPreferences->PaywallMembershipNotifyEmail = $this->systemPreferences->EmailFromAddress;
57
        $this->systemPreferences->PaywallMembershipNotifyFromEmail = $this->systemPreferences->EmailFromAddress;
58
        $this->systemPreferences->PaywallEmailNotifyEnabled = 0;
59
    }
60
61
    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...
62
    {
63
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
64
        $tool->updateSchema($this->getClasses(), true);
65
66
        $this->dispatcher->dispatch('newscoop_paywall.adapters.register', new GenericEvent());
67
68
        // Generate proxies for entities
69
        $this->em->getProxyFactory()->generateProxyClasses(
70
            $this->getClasses(),
71
            $this->pluginDir.'library/Proxy'
72
        );
73
    }
74
75
    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...
76
    {
77
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
78
        $tool->dropSchema($this->getClasses(), true);
79
        $this->removeJobs();
80
        $this->removeSettings();
81
    }
82
83
    /**
84
     * Clean up system preferences.
85
     */
86
    private function removeSettings()
87
    {
88
        $this->systemPreferences->delete('PaywallMembershipNotifyEmail');
89
        $this->systemPreferences->delete('PaywallEmailNotifyEnabled');
90
        $this->systemPreferences->delete('PaywallMembershipNotifyFromEmail');
91
    }
92
93
    /**
94
     * Add plugin cron jobs.
95
     */
96
    private function addJobs()
97
    {
98
        $this->setCronJobs();
99
        foreach ($this->cronjobs as $jobName => $jobConfig) {
0 ignored issues
show
Bug introduced by
The property cronjobs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
100
            $this->scheduler->registerJob($jobName, $jobConfig);
101
        }
102
    }
103
104
    /**
105
     * Remove plugin cron jobs.
106
     */
107
    private function removeJobs()
108
    {
109
        $this->setCronJobs();
110
        foreach ($this->cronjobs as $jobName => $jobConfig) {
111
            $this->scheduler->removeJob($jobName, $jobConfig);
112
        }
113
    }
114
115
    private function setCronJobs()
116
    {
117
        $qb = $this->em->getRepository('Newscoop\Entity\Aliases')
118
            ->createQueryBuilder('a');
119
120
        $qb->select(
121
                'a.id as aliasId',
122
                'p.id as publicationId'
123
            )
124
            ->leftJoin('a.publication', 'p')
125
            ->setMaxResults(1);
126
127
        $alias = $qb->getQuery()->getArrayResult();
128
129
        if (empty($alias)) {
130
            throw new \RuntimeException('There is no alias defined! At least one alias needs to be defined.');
131
        }
132
133
        $this->cronjobs = array(
134
            'Sends email notifications for expiring subscriptions' => array(
135
                'command' => sprintf(
136
                    "%s paywall:notifier:expiring %s",
137
                    realpath($this->pluginDir.'application/console'),
138
                    $alias[0]['publicationId']
139
                ),
140
                'schedule' => '0 2 * * *',
141
            ),
142
        );
143
144
        return $alias[0]['publicationId'];
145
    }
146
147
    public static function getSubscribedEvents()
148
    {
149
        return array(
150
            'plugin.install.newscoop_newscoop_paywall_bundle' => array('install', 1),
151
            'plugin.update.newscoop_newscoop_paywall_bundle' => array('update', 1),
152
            'plugin.remove.newscoop_newscoop_paywall_bundle' => array('remove', 1),
153
        );
154
    }
155
156
    private function getClasses()
157
    {
158
        return array(
159
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Subscription'),
160
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\SubscriptionSpecification'),
161
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Gateway'),
162
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\UserSubscription'),
163
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Trial'),
164
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Discount'),
165
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Duration'),
166
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Order'),
167
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Modification'),
168
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Currency'),
169
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\SubscriptionTranslation'),
170
            $this->em->getClassMetadata('Newscoop\PaywallBundle\Entity\Payment'),
171
        );
172
    }
173
}
174