Conditions | 7 |
Paths | 1 |
Total Lines | 36 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 56 |
Changes | 0 |
1 | <?php |
||
19 | public function configureSingleState(EventDispatcher $dispatcher) { |
||
20 | $dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) { |
||
21 | |||
22 | /** @var ConsoleCommand $command */ |
||
23 | $command = $event->getCommand(); |
||
24 | |||
25 | if (!($command instanceof ConsoleCommand) or ($command->isSingleInstance() == false)) { |
||
|
|||
26 | return; |
||
27 | } |
||
28 | |||
29 | |||
30 | $commandState = (new CommandState($command)); |
||
31 | if ($commandState->isAlive()) { |
||
32 | $event->getOutput()->writeln('Already running: ' . $command->getName() . ' Pid:' . $commandState->getPid()); |
||
33 | $event->disableCommand(); |
||
34 | } else { |
||
35 | $commandState->setPid(getmypid()); |
||
36 | } |
||
37 | |||
38 | }); |
||
39 | |||
40 | $dispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) { |
||
41 | /** @var ConsoleCommand $command */ |
||
42 | $command = $event->getCommand(); |
||
43 | |||
44 | if (!($command instanceof ConsoleCommand) or ($command->isSingleInstance() == false)) { |
||
45 | return; |
||
46 | } |
||
47 | |||
48 | if ($event->getExitCode() === 0) { |
||
49 | (new CommandState($command))->unlink(); |
||
50 | } |
||
51 | |||
52 | }); |
||
53 | return $dispatcher; |
||
54 | } |
||
55 | } |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.