|
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\Command; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Newscoop\PaywallBundle\Notifications\Emails; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Console command responsible for sending email |
|
19
|
|
|
* notifications for expiring subscriptions. |
|
20
|
|
|
*/ |
|
21
|
|
|
class NotifierCommand extends ContainerAwareCommand |
|
22
|
|
|
{ |
|
23
|
|
|
private $input; |
|
24
|
|
|
private $output; |
|
25
|
|
|
|
|
26
|
|
|
protected function configure() |
|
27
|
|
|
{ |
|
28
|
|
|
$this |
|
29
|
|
|
->setName('paywall:notifier:expiring') |
|
30
|
|
|
->addArgument('alias', InputArgument::REQUIRED, 'Publication alias') |
|
31
|
|
|
->setDescription('Sends email notifications for expiring subscriptions') |
|
32
|
|
|
; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->input = $input; |
|
38
|
|
|
$this->output = $output; |
|
39
|
|
|
|
|
40
|
|
|
$this->setCurrentPublication(); |
|
41
|
|
|
|
|
42
|
|
|
try { |
|
43
|
|
|
$now = new \DateTime(); |
|
44
|
|
|
$this->runProcessing($now, 7, Emails::NOTIFY_LEVEL_ONE); |
|
45
|
|
|
$this->runProcessing($now, 3, Emails::NOTIFY_LEVEL_TWO); |
|
46
|
|
|
} catch (\Exception $e) { |
|
47
|
|
|
$this->output->writeln('<error>Error occured: '.$e->getMessage().'</error>'); |
|
48
|
|
|
|
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function setCurrentPublication() |
|
54
|
|
|
{ |
|
55
|
|
|
$entityManager = $this->getContainer()->getService('em'); |
|
56
|
|
|
$queryBuilder = $entityManager->getRepository('Newscoop\Entity\Publication') |
|
57
|
|
|
->createQueryBuilder('p') |
|
58
|
|
|
->leftJoin('p.defaultAlias', 'a') |
|
59
|
|
|
->where('a.name = :alias') |
|
60
|
|
|
->setParameter('alias', $this->input->getArgument('alias')); |
|
61
|
|
|
|
|
62
|
|
|
$publication = $queryBuilder->getQuery()->getOneOrNullResult(); |
|
63
|
|
|
|
|
64
|
|
|
if (null === $publication) { |
|
65
|
|
|
throw new \RuntimeException('Publication does not exist for given alias!'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$publicationService = $this->getContainer()->getService('newscoop.publication_service'); |
|
69
|
|
|
$publicationService->setPublication($publication); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function runProcessing($now, $daysBefore, $level) |
|
73
|
|
|
{ |
|
74
|
|
|
$notificationService = $this->getContainer()->getService('newscoop_paywall.notifications_service'); |
|
75
|
|
|
$subscriptionsCount = $notificationService->getExpiringSubscriptionsCount( |
|
76
|
|
|
$now, |
|
77
|
|
|
$level, |
|
78
|
|
|
$daysBefore |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
if ($subscriptionsCount !== 0) { |
|
82
|
|
|
$notificationService->processExpiringSubscriptions( |
|
83
|
|
|
$now, |
|
84
|
|
|
$level, |
|
85
|
|
|
$subscriptionsCount, |
|
86
|
|
|
$daysBefore |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
if ($this->input->getOption('verbose')) { |
|
90
|
|
|
$this->output->writeln('<info>'.$subscriptionsCount.' notifications sent... (which expire in '.$daysBefore.' days)</info>'); |
|
91
|
|
|
} |
|
92
|
|
|
} else { |
|
93
|
|
|
if ($this->input->getOption('verbose')) { |
|
94
|
|
|
$this->output->writeln('<info>There are no subscriptions expiring within '.$daysBefore.' day(s).<info>'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|