Completed
Pull Request — master (#42)
by guillaume
03:43
created

isLockedChannelInfomation()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 4
nc 2
nop 3
crap 30
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;
0 ignored issues
show
Unused Code introduced by
The property $timeLimit is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
43
    /**
44
     * @var string
45
     */
46
    private $channelName;
0 ignored issues
show
Unused Code introduced by
The property $channelName is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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
                    if ($this->isLockedChannelInfomation($key, $lock, $channel, $output)) {
0 ignored issues
show
Unused Code introduced by
The call to LoggerStateChannelComman...ckedChannelInfomation() has too many arguments starting with $output.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
111
112
                        $dateWithMaxTime = $lock->getLockedAt();
113
114
                        $time = $channel['duration_limit_per_run'] + $this->extraTime;
115
                        $dateWithMaxTime->add(
116
                            new DateInterval('PT' . $time . 'S')
117
                        );
118
                        if (new \DateTime() > ($dateWithMaxTime)) {
119
                            $this->notifier->process($key);
120
                        }
121
                    }
122
                }
123
124
125
            }
126
        }
127
    }
128
129
    /**
130
     * @param      $key
131
     * @param Lock $lock
132
     * @param      $channel
133
     * @return bool
134
     */
135
    private function isLockedChannelInfomation($key, Lock $lock, $channel)
136
    {
137
138
        if (preg_match(sprintf('/^%s/', $key), $lock->getChannel()) && true === $channel['dynamic'] || $key == $lock->getChannel() && false === $channel['dynamic']) {
139
140
            return true;
141
        }
142
143
        return false;
144
    }
145
}
146