ConsoleCommand   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 111
ccs 17
cts 17
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
B getProgressPanel() 0 13 5
B initialize() 0 20 5
A setCode() 0 3 1
A getOutput() 0 3 1
A getInput() 0 3 1
A isSingleInstance() 0 3 1
1
<?php
2
3
  namespace Funivan\Console;
4
5
  use Funivan\Console\ProgressPanel\InlineProgressPanel;
6
  use Funivan\Console\ProgressPanel\ProgressPanel;
7
  use Symfony\Component\Console\Command\Command;
8
  use Symfony\Component\Console\Input\ArgvInput;
9
  use Symfony\Component\Console\Input\ArrayInput;
10
  use Symfony\Component\Console\Input\InputInterface;
11
  use Symfony\Component\Console\Input\InputOption;
12
  use Symfony\Component\Console\Output\OutputInterface;
13
14
  /**
15
   * @author Ivan Shcherbak <[email protected]>
16
   */
17
  class ConsoleCommand extends Command {
18
19
    /**
20
     * @var null|InputInterface
21
     */
22
    private $input;
23
24
    /**
25
     * @var null|OutputInterface
26
     */
27
    private $output;
28
29
30
    /**
31
     * @inheritdoc
32
     */
33 5
    protected function configure() {
34 5
      $this->addOption('no-cache', null, InputOption::VALUE_NONE, 'Disable application cache');
35 5
      $this->addOption('run-from-cron', null, InputOption::VALUE_NONE, 'Flag for custom verbosity');
36 5
      parent::configure();
37 5
    }
38
39
40
    /**
41
     * Return progress panel according to application options
42
     *
43
     * @param int $max
44
     * @return InlineProgressPanel|ProgressPanel
45
     * @throws \Exception
46
     */
47
    protected function getProgressPanel($max = 0) {
48
      if ($this->getOutput() === null) {
49
        throw new \Exception('Cant initialize progress panel. Output is undefined');
50
      }
51
52
      $input = $this->getInput();
53
54
      if ($input !== null and $input->hasOption('run-from-cron') and $input->getOption('run-from-cron')) {
55
        return new InlineProgressPanel($this->getOutput(), $max);
56
      }
57
58
      return new ProgressPanel($this->getOutput(), $max);
59
    }
60
61
62
    /**
63
     * @inheritdoc
64
     */
65 1
    protected function initialize(InputInterface $input, OutputInterface $output) {
66 1
      $this->input = $input;
67 1
      $this->output = $output;
68
69 1
      parent::initialize($input, $output);
70
71
72 1
      if ($input !== null and $input->getOption('run-from-cron')) {
73
        $message = '[run-from-cron] [' . (new \DateTime())->format('Y-m-d H:i:s') . ']';
74
75
        if ($input instanceof ArgvInput or $input instanceof ArrayInput) {
76
          $message = $message . ' ' . $input;
77
        } else {
78
          $message = $message . ' ' . $this->getName();
79
        }
80
81
        $output->writeln($message);
82
      }
83
84 1
    }
85
86
87
    /**
88
     * We use execute method for command logic.
89
     *
90
     * @see ConsoleCommand::execute
91
     * @deprecated
92
     *
93
     * @param callable $code
94
     * @return Command|void
95
     * @throws \Exception
96
     */
97 1
    public function setCode(callable $code) {
98 1
      throw new \Exception('Command setCode is deprecated');
99
    }
100
101
102
    /**
103
     * @return null|OutputInterface
104
     */
105 1
    public function getOutput() {
106 1
      return $this->output;
107
    }
108
109
110
    /**
111
     * @return null|InputInterface
112
     */
113
    public function getInput() {
114
      return $this->input;
115
    }
116
117
118
    /**
119
     * Run command only with single instance
120
     *
121
     * @return bool
122
     */
123 1
    public function isSingleInstance() {
124 1
      return false;
125
    }
126
127
  }