Completed
Push — master ( cbca24...4306f5 )
by Clément
51s
created

ProcessEventCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 10.08 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 2 Features 2
Metric Value
wmc 10
c 8
b 2
f 2
lcom 1
cbo 8
dl 13
loc 129
ccs 0
cts 76
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A configure() 13 13 1
B execute() 0 57 8

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
3
namespace Itkg\DelayEventBundle\Command;
4
5
use Itkg\DelayEventBundle\DomainManager\EventManager;
6
use Itkg\DelayEventBundle\Exception\LockException;
7
use Itkg\DelayEventBundle\Handler\LockHandlerInterface;
8
use Itkg\DelayEventBundle\Model\Event;
9
use Itkg\DelayEventBundle\Processor\EventProcessor;
10
use Itkg\DelayEventBundle\Repository\EventRepository;
11
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Class ProcessEventCommand
19
 */
20
class ProcessEventCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * @var EventRepository
24
     */
25
    private $eventRepository;
26
27
    /**
28
     * @var EventProcessor
29
     */
30
    private $eventProcessor;
31
32
    /**
33
     * @var LockHandlerInterface
34
     */
35
    private $lockHandler;
36
37
    /**
38
     * @var array
39
     */
40
    private $channels;
41
42
    /**
43
     * @param EventRepository      $eventRepository
44
     * @param EventProcessor       $eventProcessor
45
     * @param LockHandlerInterface $lockHandler
46
     * @param array                $channels
47
     * @param null|string          $name
48
     */
49
    public function __construct(
50
        EventRepository $eventRepository,
51
        EventProcessor $eventProcessor,
52
        LockHandlerInterface $lockHandler,
53
        array $channels = [],
54
        $name = null
55
    )
56
    {
57
        $this->eventRepository = $eventRepository;
58
        $this->eventProcessor = $eventProcessor;
59
        $this->lockHandler = $lockHandler;
60
        $this->channels = $channels;
61
62
        parent::__construct($name);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70
        $this
71
            ->setName('itkg_delay_event:process')
72
            ->setDescription('Process async events')
73
            ->addOption(
74
                'channel',
75
                'c',
76
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
77
                'Specify the channels to process (default: [\'default\'])',
78
                ['default']
79
            );
80
    }
81
82
    /**
83
     * @param InputInterface  $input
84
     * @param OutputInterface $output
85
     *
86
     * @throws LockException
87
     * @throws \Exception
88
     *
89
     * @return int|null|void
90
     */
91
    protected function execute(InputInterface $input, OutputInterface $output)
92
    {
93
        $channels = $input->getOption('channel');
94
95
        foreach ($channels as $channel) {
96
            if (!isset($this->channels[$channel])) {
97
                $output->writeln(sprintf(
98
                    '<error>Channel <info>%s</info> is not configured.</error>',
99
                    $channel
100
                ));
101
            }
102
103
            if ($this->lockHandler->isLocked($channel)) {
104
                $output->writeln(
105
                    sprintf(
106
                        'Command is locked by another process for channel <info>%s</info>.',
107
                        $channel
108
                    )
109
                );
110
111
                continue;
112
            }
113
114
            $output->writeln(
115
                sprintf(
116
                    'Process events for channel <info>%s</info>',
117
                    $channel
118
                )
119
            );
120
121
            $this->lockHandler->lock($channel);
122
123
            $event = null;
124
            try {
125
                while (true) {
126
                    if (!$event = $this->eventRepository->findFirstTodoEvent(false, $this->channels[$channel]['include'], $this->channels[$channel]['exclude'])) {
127
                        break;
128
                    }
129
                    $event->setDelayed(false);
130
                    $this->eventProcessor->process($event);
131
                }
132
            } catch (\Exception $e) {
133
                if ($event instanceof Event) {
134
                    $output->writeln(sprintf(
135
                        '<info>[%s]</info> <error> An error occurred while processing event "%s".</error>',
136
                        $channel,
137
                        $event->getOriginalName()
138
                    ));
139
                }
140
141
                $output->writeln(sprintf('<info>[%s]</info> <error>%s</error>',$channel, $e->getMessage()));
142
                $output->writeln($e->getTraceAsString());
143
            }
144
145
            $this->lockHandler->release($channel);
146
        }
147
    }
148
}
149