Completed
Push — master ( eec44f...f37f32 )
by Julien
05:06 queued 01:29
created

ProcessEventCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 9.4285
cc 1
eloc 11
nc 1
nop 5
crap 2
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
                continue;
103
            }
104
105
            if ($this->lockHandler->isLocked($channel)) {
106
                $output->writeln(
107
                    sprintf(
108
                        'Command is locked by another process for channel <info>%s</info>.',
109
                        $channel
110
                    )
111
                );
112
113
                continue;
114
            }
115
116
            $output->writeln(
117
                sprintf(
118
                    'Process events for channel <info>%s</info>',
119
                    $channel
120
                )
121
            );
122
123
            $this->lockHandler->lock($channel);
124
125
            $processedEventsCount = 0;
126
            $event = null;
127
            try {
128
                while ($this->channels[$channel]['events_limit_per_run'] === null
129
                    || $processedEventsCount < $this->channels[$channel]['events_limit_per_run']
130
                ) {
131
                    if (!$event = $this->eventRepository->findFirstTodoEvent(false, $this->channels[$channel]['include'], $this->channels[$channel]['exclude'])) {
132
                        break;
133
                    }
134
                    $event->setDelayed(false);
135
                    $this->eventProcessor->process($event);
136
                    $processedEventsCount++;
137
                }
138
            } catch (\Exception $e) {
139
                if ($event instanceof Event) {
140
                    $output->writeln(sprintf(
141
                        '<info>[%s]</info> <error> An error occurred while processing event "%s".</error>',
142
                        $channel,
143
                        $event->getOriginalName()
144
                    ));
145
                }
146
147
                $output->writeln(sprintf('<info>[%s]</info> <error>%s</error>',$channel, $e->getMessage()));
148
                $output->writeln($e->getTraceAsString());
149
            }
150
151
            $this->lockHandler->release($channel);
152
        }
153
    }
154
}
155