1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Azine\EmailBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Aggregate and send pending notifications or newsletters via email. |
11
|
|
|
* |
12
|
|
|
* @author dominik |
13
|
|
|
*/ |
14
|
|
|
class SendNotificationsCommand extends ContainerAwareCommand |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* (non-PHPdoc). |
18
|
|
|
* |
19
|
|
|
* @see Symfony\Component\Console\Command.Command::configure() |
20
|
|
|
*/ |
21
|
|
|
protected function configure() |
22
|
3 |
|
{ |
23
|
|
|
$this->setName('emails:sendNotifications') |
24
|
3 |
|
->setDescription('Aggregate and send pending notifications via email.') |
25
|
3 |
|
->setHelp(<<<EOF |
26
|
3 |
|
The <info>emails:sendNotifications</info> command sends emails for all pending notifications. |
27
|
3 |
|
|
28
|
|
|
Depending on you Swiftmailer-Configuration the email will be send directly or will be written to the spool. |
29
|
|
|
|
30
|
|
|
If you configured Swiftmailer to spool email, then you need to run the <info>swiftmailer:spool:send</info> |
31
|
|
|
command to actually send the emails from the spool. |
32
|
|
|
|
33
|
|
|
EOF |
34
|
|
|
) |
35
|
|
|
; |
36
|
|
|
} |
37
|
3 |
|
|
38
|
|
|
/** |
39
|
|
|
* (non-PHPdoc). |
40
|
|
|
* |
41
|
|
|
* @see Symfony\Component\Console\Command.Command::execute() |
42
|
|
|
*/ |
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
44
|
2 |
|
{ |
45
|
|
|
if (\Symfony\Component\HttpKernel\Kernel::VERSION_ID < 30400) { |
46
|
|
|
$lock = new \Symfony\Component\Filesystem\LockHandler($this->getName()); |
|
|
|
|
47
|
2 |
|
$unlockedCommand = $lock->lock(); |
48
|
2 |
|
} else { |
49
|
|
|
$store = new \Symfony\Component\Lock\Store\SemaphoreStore(); |
50
|
|
|
$factory = new \Symfony\Component\Lock\Factory($store); |
51
|
|
|
|
52
|
|
|
$lock = $factory->createLock($this->getName()); |
53
|
|
|
$unlockedCommand = $lock->acquire(); |
54
|
2 |
|
} |
55
|
2 |
|
|
56
|
|
|
if (!$unlockedCommand) { |
57
|
2 |
|
$output->writeln('The command is already running in another process.'); |
58
|
2 |
|
|
59
|
1 |
|
return 0; |
60
|
1 |
|
} |
61
|
1 |
|
|
62
|
|
|
$failedAddresses = array(); |
63
|
|
|
$sentMails = $this->getContainer()->get('azine_email_notifier_service')->sendNotifications($failedAddresses); |
64
|
|
|
|
65
|
|
|
$output->writeln(date(\DateTime::RFC2822).' : '.str_pad($sentMails, 4, ' ', STR_PAD_LEFT).' emails have been processed.'); |
66
|
2 |
|
if (sizeof($failedAddresses) > 0) { |
67
|
2 |
|
$output->writeln(date(\DateTime::RFC2822).' : '.'The following email-addresses failed:'); |
68
|
|
|
foreach ($failedAddresses as $address) { |
69
|
|
|
$output->writeln(' '.$address); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// (optional) release the lock (otherwise, PHP will do it for you automatically) |
74
|
|
|
$lock->release(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|