Completed
Push — master ( c37102...86711a )
by Dominik
18:10
created

SendNotificationsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
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
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
use Symfony\Component\Filesystem\LockHandler;
9
10
/**
11
 * Aggregate and send pending notifications or newsletters via email.
12
 *
13
 * @author dominik
14
 */
15
class SendNotificationsCommand extends ContainerAwareCommand
16
{
17
    /**
18
     * (non-PHPdoc).
19
     *
20
     * @see Symfony\Component\Console\Command.Command::configure()
21
     */
22 3
    protected function configure()
23
    {
24 3
        $this->setName('emails:sendNotifications')
25 3
                ->setDescription('Aggregate and send pending notifications via email.')
26 3
                ->setHelp(<<<EOF
27 3
The <info>emails:sendNotifications</info> command sends emails for all pending notifications.
28
29
Depending on you Swiftmailer-Configuration the email will be send directly or will be written to the spool.
30
31
If you configured Swiftmailer to spool email, then you need to run the <info>swiftmailer:spool:send</info>
32
command to actually send the emails from the spool.
33
34
EOF
35
            )
36
        ;
37 3
    }
38
39
    /**
40
     * (non-PHPdoc).
41
     *
42
     * @see Symfony\Component\Console\Command.Command::execute()
43
     */
44 2
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        // create the lock
47 2
        $lock = new LockHandler($this->getName());
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Filesystem\LockHandler has been deprecated with message: since version 3.4, to be removed in 4.0. Use Symfony\Component\Lock\Store\SemaphoreStore or Symfony\Component\Lock\Store\FlockStore instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
48 2
        if (!$lock->lock()) {
49
            $output->writeln('The command is already running in another process.');
50
51
            return 0;
52
        }
53
54 2
        $failedAddresses = array();
55 2
        $sentMails = $this->getContainer()->get('azine_email_notifier_service')->sendNotifications($failedAddresses);
56
57 2
        $output->writeln(date(\DateTime::RFC2822).' : '.str_pad($sentMails, 4, ' ', STR_PAD_LEFT).' emails have been processed.');
58 2
        if (sizeof($failedAddresses) > 0) {
59 1
            $output->writeln(date(\DateTime::RFC2822).' : '.'The following email-addresses failed:');
60 1
            foreach ($failedAddresses as $address) {
61 1
                $output->writeln('    '.$address);
62
            }
63
        }
64
65
        // (optional) release the lock (otherwise, PHP will do it for you automatically)
66 2
        $lock->release();
67 2
    }
68
}
69