LoggerStateChannelCommand   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 5
dl 0
loc 128
ccs 0
cts 59
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
C execute() 0 39 7
B isLockedChannelInfomation() 0 10 5
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
                        break;
123
                    }
124
                }
125
126
127
            }
128
        }
129
    }
130
131
    /**
132
     * @param      $key
133
     * @param Lock $lock
134
     * @param      $channel
135
     * @return bool
136
     */
137
    private function isLockedChannelInfomation($key, Lock $lock, $channel)
138
    {
139
140
        if (preg_match(sprintf('/^%s/', $key), $lock->getChannel()) && true === $channel['dynamic'] || $key == $lock->getChannel() && false === $channel['dynamic']) {
141
142
            return true;
143
        }
144
145
        return false;
146
    }
147
}
148