SingleStateConfigurator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 6
dl 0
loc 42
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C configureSingleState() 0 36 7
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
  }