Completed
Push — master ( fcb684...b715c2 )
by Dominik
15:53
created

SendNotificationsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 11.54 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 90.91%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 5
c 6
b 0
f 1
lcom 0
cbo 5
dl 6
loc 52
ccs 20
cts 22
cp 0.9091
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
B execute() 6 24 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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