Application   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 7

Test Coverage

Coverage 80.38%

Importance

Changes 0
Metric Value
wmc 18
lcom 3
cbo 7
dl 0
loc 147
ccs 41
cts 51
cp 0.8038
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultInputDefinition() 0 10 1
A getDefaultCommands() 0 10 2
A selfUpdateCommand() 0 7 1
A getDefaultHelperSet() 0 9 2
A getUserDefinition() 0 8 2
A addUserArgumentsAndOptions() 0 5 1
A isPharArchive() 0 4 1
B doRunCommand() 0 27 6
A catchIO() 0 4 1
A afterRun() 0 4 1
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Console;
9
10
use Deployer\Component\PharUpdate\Console\Command as PharUpdateCommand;
11
use Deployer\Component\PharUpdate\Console\Helper as PharUpdateHelper;
12
use Deployer\Deployer;
13
use Symfony\Component\Console\Application as Console;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputDefinition;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class Application extends Console
21
{
22
    /**
23
     * Input definition for user specific arguments and options.
24
     *
25
     * @var InputDefinition
26
     */
27
    private $userDefinition;
28
29
    /**
30
     * @var callable
31
     */
32
    private $catchIO;
33
34
    /**
35
     * @var callable
36
     */
37
    private $after;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 17
    protected function getDefaultInputDefinition()
43
    {
44 17
        $inputDefinition = parent::getDefaultInputDefinition();
45
46 17
        $inputDefinition->addOption(
47 17
            new InputOption('--file', '-f', InputOption::VALUE_OPTIONAL, 'Specify Deployer file')
48
        );
49
50 17
        return $inputDefinition;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 17
    protected function getDefaultCommands()
57
    {
58 17
        $commands = parent::getDefaultCommands();
59
60 17
        if ($this->isPharArchive()) {
61
            $commands[] = $this->selfUpdateCommand();
62
        }
63
64 17
        return $commands;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    private function selfUpdateCommand()
71
    {
72
        $selfUpdate = new PharUpdateCommand('self-update');
73
        $selfUpdate->setDescription('Updates deployer.phar to the latest version');
74
        $selfUpdate->setManifestUri('https://deployer.org/manifest.json');
75
        return $selfUpdate;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 17
    protected function getDefaultHelperSet()
82
    {
83 17
        $helperSet = parent::getDefaultHelperSet();
84
85 17
        if ($this->isPharArchive()) {
86
            $helperSet->set(new PharUpdateHelper());
87
        }
88 17
        return $helperSet;
89
    }
90
91
    /**
92
     * @return InputDefinition
93
     */
94 17
    public function getUserDefinition()
95
    {
96 17
        if (null === $this->userDefinition) {
97 17
            $this->userDefinition = new InputDefinition();
98
        }
99
100 17
        return $this->userDefinition;
101
    }
102
103
    /**
104
     * Add user definition arguments and options to definition.
105
     */
106 17
    public function addUserArgumentsAndOptions()
107
    {
108 17
        $this->getDefinition()->addArguments($this->getUserDefinition()->getArguments());
109 17
        $this->getDefinition()->addOptions($this->getUserDefinition()->getOptions());
110 17
    }
111
112
    /**
113
     * @return bool
114
     */
115 17
    public function isPharArchive()
116
    {
117 17
        return 'phar:' === substr(__FILE__, 0, 5);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 16
    protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
124
    {
125 16
        $exception = null;
126 16
        $exitCode = 0;
127
128 16
        if (!empty($this->catchIO)) {
129 16
            list($input, $output) = call_user_func($this->catchIO, $input, $output);
130
        }
131
132
        try {
133 16
            $exitCode = parent::doRunCommand($command, $input, $output);
134 2
        } catch (\Exception $x) {
135 2
            $exception = $x;
136
        } catch (\Throwable $x) {
137
            $exception = $x;
138
        }
139
140 16
        if (!empty($this->after)) {
141
            call_user_func($this->after, new CommandEvent($command, $input, $output, $exception, $exitCode));
142
        }
143
144 16
        if ($exception !== null) {
145 2
            throw $exception;
146
        }
147
148 14
        return $exitCode;
149
    }
150
151
    /**
152
     * @param $callable
153
     */
154 17
    public function catchIO($callable)
155
    {
156 17
        $this->catchIO = $callable;
157 17
    }
158
159
    /**
160
     * @param $callable
161
     */
162 17
    public function afterRun($callable)
163
    {
164 17
        $this->after = $callable;
165 17
    }
166
}
167