1
|
|
|
<?php |
2
|
|
|
namespace Azine\EmailBundle\Command; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Filesystem\LockHandler; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Aggregate and send pending notifications or newsletters via email |
11
|
|
|
* @author dominik |
12
|
|
|
*/ |
13
|
|
|
class SendNotificationsCommand extends ContainerAwareCommand |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* (non-PHPdoc) |
17
|
|
|
* @see Symfony\Component\Console\Command.Command::configure() |
18
|
|
|
*/ |
19
|
3 |
|
protected function configure() |
20
|
|
|
{ |
21
|
3 |
|
$this ->setName('emails:sendNotifications') |
22
|
3 |
|
->setDescription('Aggregate and send pending notifications via email.') |
23
|
3 |
|
->setHelp(<<<EOF |
24
|
|
|
The <info>emails:sendNotifications</info> command sends emails for all pending notifications. |
25
|
|
|
|
26
|
|
|
Depending on you Swiftmailer-Configuration the email will be send directly or will be written to the spool. |
27
|
|
|
|
28
|
|
|
If you configured Swiftmailer to spool email, then you need to run the <info>swiftmailer:spool:send</info> |
29
|
|
|
command to actually send the emails from the spool. |
30
|
|
|
|
31
|
|
|
EOF |
32
|
3 |
|
) |
33
|
|
|
; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* (non-PHPdoc) |
38
|
|
|
* @see Symfony\Component\Console\Command.Command::execute() |
39
|
|
|
*/ |
40
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
41
|
|
|
{ |
42
|
|
|
// create the lock |
43
|
2 |
|
$lock = new LockHandler($this->getName()); |
44
|
2 |
|
if (!$lock->lock()) { |
45
|
|
|
$output->writeln('The command is already running in another process.'); |
46
|
|
|
|
47
|
|
|
return 0; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
$failedAddresses = array(); |
51
|
2 |
|
$sentMails = $this->getContainer()->get('azine_email_notifier_service')->sendNotifications($failedAddresses); |
52
|
|
|
|
53
|
2 |
|
$output->writeln(date(\DateTime::RFC2822)." : ".str_pad($sentMails, 4, " ", STR_PAD_LEFT)." emails have been processed."); |
54
|
2 |
View Code Duplication |
if (sizeof($failedAddresses) > 0) { |
|
|
|
|
55
|
1 |
|
$output->writeln(date(\DateTime::RFC2822)." : "."The following email-addresses failed:"); |
56
|
1 |
|
foreach ($failedAddresses as $address) { |
57
|
1 |
|
$output->writeln(" ".$address); |
58
|
1 |
|
} |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
// (optional) release the lock (otherwise, PHP will do it for you automatically) |
62
|
2 |
|
$lock->release(); |
63
|
2 |
|
} |
64
|
|
|
} |
65
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.