Completed
Pull Request — master (#29)
by Eugene
03:29
created

SendNewsLetterCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1
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
class SendNewsLetterCommand 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:sendNewsletter')
22 3
                ->setDescription('Send Newsletter via email to all subscribers.')
23 3
                ->setHelp(<<<EOF
24 3
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
                )
34
        ;
35 3
    }
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
            }
61
        }
62 2
    }
63
}
64