SingleStateConfigurator::configureSingleState()   C
last analyzed

Complexity

Conditions 7
Paths 1

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 24
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 1
nop 1
crap 56
1
<?php
2
3
  namespace Funivan\Console\SingleState;
4
5
  use Funivan\Console\ConsoleCommand;
6
  use Symfony\Component\Console\ConsoleEvents;
7
  use Symfony\Component\Console\Event\ConsoleCommandEvent;
8
  use Symfony\Component\Console\Event\ConsoleTerminateEvent;
9
  use Symfony\Component\EventDispatcher\EventDispatcher;
10
11
  /**
12
   * @author Ivan Shcherbak <[email protected]>
13
   */
14
  class SingleStateConfigurator implements ConsoleSingleStateConfiguratorInterface {
15
16
    /**
17
     * @inheritdoc
18
     */
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)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
45
          return;
46
        }
47
48
        if ($event->getExitCode() === 0) {
49
          (new CommandState($command))->unlink();
50
        }
51
52
      });
53
      return $dispatcher;
54
    }
55
  }