Completed
Pull Request — master (#41)
by guillaume
03:05
created

LoggerStateChannelCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 118
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A configure() 0 11 1
B execute() 0 30 6
A loadChannelInfomation() 0 10 3
1
<?php
2
3
namespace Itkg\DelayEventBundle\Command;
4
5
use DateInterval;
6
use Itkg\DelayEventBundle\Document\Lock;
7
use Itkg\DelayEventBundle\Handler\LockHandlerInterface;
8
use Itkg\DelayEventBundle\Notifier\NotifierInterface;
9
use Itkg\DelayEventBundle\Repository\EventRepository;
10
use Itkg\DelayEventBundle\Repository\LockRepository;
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\Output\OutputInterface;
15
use Symfony\Component\Filesystem\LockHandler;
16
17
/**
18
 * Class LoggerStateChannelCommand
19
 */
20
class LoggerStateChannelCommand extends ContainerAwareCommand
21
{
22
23
    /**
24
     * @var array
25
     */
26
    private $channels;
27
28
    /**
29
     * @var LockRepository
30
     */
31
    private $lockRepository;
32
33
    /**
34
     * @var NotifierInterface
35
     */
36
    private $notifier;
37
38
    /**
39
     * @var integer
40
     */
41
    private $timeLimit;
42
43
    /**
44
     * @var string
45
     */
46
    private $channelName;
47
48
    /**
49
     * @var integer
50
     */
51
    private $extraTime;
52
53
54
    /**
55
     * @param LockRepository $lockRepository
56
     * @param array          $channels
57
     * @param null           $name
58
     */
59
    public function __construct(
60
        LockRepository $lockRepository,
61
        NotifierInterface $notifier,
62
        array $channels = [],
63
        $name = null
64
    ) {
65
        $this->lockRepository = $lockRepository;
66
        $this->notifier = $notifier;
67
        $this->channels = $channels;
68
69
        parent::__construct($name);
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    protected function configure()
76
    {
77
        $this
78
            ->setName('itkg_delay_event:logger')
79
            ->setDescription('Log state channel')
80
            ->addArgument(
81
                'time',
82
                InputArgument::REQUIRED,
83
                'extra time for detect lock'
84
            );
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function execute(InputInterface $input, OutputInterface $output)
91
    {
92
        $this->extraTime = $input->getArgument('time');
93
94
        if (!isset($this->extraTime)) {
95
            $output->writeln(
96
                sprintf(
97
                    '<info>Argument time %s is required.</info>',
98
                    $this->extraTime
99
                )
100
            );
101
        }
102
103
        $locks = $this->lockRepository->findAll();
104
105
        /** @var Lock $lock */
106
        foreach ($locks as $lock) {
107
            if ($lock->isCommandLocked()) {
108
                foreach ($this->channels as $key => $channel) {
109
                    $this->loadChannelInfomation($key, $lock, $channel);
110
                }
111
112
                $dateWithMaxTime = $lock->getLockedAt();
113
                $dateWithMaxTime->add(new DateInterval('PT' . $this->timeLimit . 'S'));
114
                if (new \DateTime() > ($dateWithMaxTime)) {
115
                    $this->notifier->process($this->channelName);
116
                }
117
            }
118
        }
119
    }
120
121
    /**
122
     * @param string $key
123
     * @param Lock   $lock
124
     * @param array  $channel
125
     */
126
    private function loadChannelInfomation($key, Lock $lock, $channel)
127
    {
128
        $this->channelName = $key;
129
        $this->timeLimit = $channel['duration_limit_per_run'] + $this->extraTime;
130
131
        if (preg_match(sprintf('/^%s/', $key), $lock->getChannel()) && true === $channel['dynamic']) {
132
            $this->channelName = $lock->getChannel();
133
            $this->timeLimit = $channel['duration_limit_per_run'] + $this->extraTime;
134
        }
135
    }
136
137
}
138