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
|
|
|
* Send Newsletter via email |
11
|
|
|
* @author dominik |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class SendNewsLetterCommand extends ContainerAwareCommand |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* (non-PHPdoc) |
17
|
|
|
* @see Symfony\Component\Console\Command.Command::configure() |
18
|
|
|
*/ |
19
|
4 |
|
protected function configure() |
20
|
|
|
{ |
21
|
4 |
|
$this ->setName('emails:sendNewsletter') |
22
|
4 |
|
->setDescription('Send Newsletter via email to all subscribers.') |
23
|
4 |
|
->setHelp(<<<EOF |
24
|
|
|
The <info>emails:sendNewsletter</info> command sends the newsletter email to all recipients who |
25
|
|
|
indicate that they would like to recieve the newsletter (see Azine\EmailBundle\Entity\RecipientInterface.getNewsletter). |
26
|
|
|
|
27
|
|
|
Depending on you Swiftmailer-Configuration the email will be send directly or will be written to the spool. |
28
|
|
|
|
29
|
|
|
If you configured Swiftmailer to spool email, then you need to run the <info>swiftmailer:spool:send</info> |
30
|
|
|
command to actually send the emails from the spool. |
31
|
|
|
|
32
|
|
|
EOF |
33
|
4 |
|
) |
34
|
|
|
; |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* (non-PHPdoc) |
39
|
|
|
* @see Symfony\Component\Console\Command.Command::execute() |
40
|
|
|
*/ |
41
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
42
|
|
|
{ |
43
|
|
|
// create the lock |
44
|
2 |
|
$lock = new LockHandler($this->getName()); |
45
|
2 |
|
if (!$lock->lock()) { |
46
|
|
|
$output->writeln('The command is already running in another process.'); |
47
|
|
|
return 0; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
$failedAddresses = array(); |
51
|
2 |
|
$output->writeln(date(\DateTime::RFC2822)." : starting to send newsletter emails."); |
52
|
|
|
|
53
|
2 |
|
$sentMails = $this->getContainer()->get('azine_email_notifier_service')->sendNewsletter($failedAddresses); |
54
|
|
|
|
55
|
2 |
|
$output->writeln(date(\DateTime::RFC2822)." : ".str_pad($sentMails, 4, " ", STR_PAD_LEFT)." newsletter emails have been sent."); |
56
|
2 |
|
if (sizeof($failedAddresses) > 0) { |
57
|
1 |
|
$output->writeln(date(\DateTime::RFC2822)." : "."The following email-addresses failed:"); |
58
|
1 |
|
foreach ($failedAddresses as $address) { |
59
|
1 |
|
$output->writeln(" ".$address); |
60
|
1 |
|
} |
61
|
1 |
|
} |
62
|
2 |
|
} |
63
|
|
|
} |
64
|
|
|
|
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.