Completed
Pull Request — master (#41)
by guillaume
20:22
created

LoggerStateChannelCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 5
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 NotifierInterface $notifier
57
     * @param array             $channels
58
     * @param null              $name
59
     */
60
    public function __construct(
61
        LockRepository $lockRepository,
62
        NotifierInterface $notifier,
63
        array $channels = [],
64
        $name = null
65
    ) {
66
        $this->lockRepository = $lockRepository;
67
        $this->notifier = $notifier;
68
        $this->channels = $channels;
69
70
        parent::__construct($name);
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    protected function configure()
77
    {
78
        $this
79
            ->setName('itkg_delay_event:logger')
80
            ->setDescription('Log state channel')
81
            ->addArgument(
82
                'time',
83
                InputArgument::REQUIRED,
84
                'extra time for detect lock'
85
            );
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    protected function execute(InputInterface $input, OutputInterface $output)
92
    {
93
        $this->extraTime = $input->getArgument('time');
94
95
        if (!isset($this->extraTime)) {
96
            $output->writeln(
97
                sprintf(
98
                    '<info>Argument time %s is required.</info>',
99
                    $this->extraTime
100
                )
101
            );
102
        }
103
104
        $locks = $this->lockRepository->findAll();
105
106
        /** @var Lock $lock */
107
        foreach ($locks as $lock) {
108
            if ($lock->isCommandLocked()) {
109
                foreach ($this->channels as $key => $channel) {
110
                    $this->loadChannelInfomation($key, $lock, $channel);
111
                }
112
113
                $dateWithMaxTime = $lock->getLockedAt();
114
                $dateWithMaxTime->add(new DateInterval('PT' . $this->timeLimit . 'S'));
115
                if (new \DateTime() > ($dateWithMaxTime)) {
116
                    $this->notifier->process($this->channelName);
117
                }
118
            }
119
        }
120
    }
121
122
    /**
123
     * @param string $key
124
     * @param Lock   $lock
125
     * @param array  $channel
126
     */
127
    private function loadChannelInfomation($key, Lock $lock, $channel)
128
    {
129
        $this->channelName = $key;
130
        $this->timeLimit = $channel['duration_limit_per_run'] + $this->extraTime;
131
132
        if (preg_match(sprintf('/^%s/', $key), $lock->getChannel()) && true === $channel['dynamic']) {
133
            $this->channelName = $lock->getChannel();
134
            $this->timeLimit = $channel['duration_limit_per_run'] + $this->extraTime;
135
        }
136
    }
137
}
138