Completed
Push — master ( 8e2e97...8fb2e6 )
by Shcherbak
01:59
created

ConsoleCommand::getInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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