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

SendNotificationsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 52
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
B execute() 0 24 4
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 3
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
            )
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
        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
            }
59
        }
60
61
        // (optional) release the lock (otherwise, PHP will do it for you automatically)
62 2
        $lock->release();
63 2
    }
64
}
65