Passed
Pull Request — master (#46)
by Théo
02:11
created

Application   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 112
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 15 2
A __construct() 0 14 2
A getDefaultHelperSet() 0 6 1
A getDefaultCommands() 0 20 3
A getHelp() 0 3 1
A getLongVersion() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console;
16
17
use ErrorException;
18
use Humbug\SelfUpdate\Exception\RuntimeException as SelfUpdateRuntimeException;
19
use Humbug\SelfUpdate\Updater;
20
use KevinGH\Box\Console\Command\SelfUpdateCommand;
21
use Symfony\Component\Console\Application as SymfonyApplication;
22
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
23
use Symfony\Component\Console\Helper\HelperSet;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Output\ConsoleOutput;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
final class Application extends SymfonyApplication
29
{
30
    private const LOGO = <<<'ASCII'
31
32
    ____            
33
   / __ )____  _  __
34
  / __  / __ \| |/_/
35
 / /_/ / /_/ />  <  
36
/_____/\____/_/|_|  
37
                    
38
39
40
ASCII;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function __construct(string $name = 'Box', string $version = '@git-version@')
46
    {
47
        // convert errors to exceptions
48
        set_error_handler(
49
            function ($code, $message, $file, $line): void {
50
                if (error_reporting() & $code) {
51
                    throw new ErrorException($message, 0, $code, $file, $line);
52
                }
53
                // @codeCoverageIgnoreStart
54
            }
55
        // @codeCoverageIgnoreEnd
56
        );
57
58
        parent::__construct($name, $version);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getLongVersion()
65
    {
66
        if (('@'.'git-version@') !== $this->getVersion()) {
67
            return sprintf(
68
                '<info>%s</info> version <comment>%s</comment> build <comment>%s</comment>',
69
                $this->getName(),
70
                $this->getVersion(),
71
                '@git-commit@'
72
            );
73
        }
74
75
        return '<info>'.$this->getName().'</info> (repo)';
76
    }
77
78
    /**
79
     * @override
80
     */
81
    public function run(InputInterface $input = null, OutputInterface $output = null)
82
    {
83
        $output = $output ?: new ConsoleOutput();
84
85
        $output->getFormatter()->setStyle(
86
            'error',
87
            new OutputFormatterStyle('red')
88
        );
89
90
        $output->getFormatter()->setStyle(
91
            'question',
92
            new OutputFormatterStyle('cyan')
93
        );
94
95
        return parent::run($input, $output);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getHelp(): string
102
    {
103
        return self::LOGO.parent::getHelp();
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    protected function getDefaultCommands(): array
110
    {
111
        $commands = parent::getDefaultCommands();
112
113
        $commands[] = new Command\Build();
114
        $commands[] = new Command\Info();
115
        $commands[] = new Command\Validate();
116
        $commands[] = new Command\Verify();
117
118
        if ('phar:' === substr(__FILE__, 0, 5)) {
119
            try {
120
                $updater = new Updater();
121
            } catch (SelfUpdateRuntimeException $e) {
122
                // Allow E2E testing of unsigned phar
123
                $updater = new Updater(null, false);
124
            }
125
            $commands[] = new SelfUpdateCommand($updater);
126
        }
127
128
        return $commands;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    protected function getDefaultHelperSet(): HelperSet
135
    {
136
        $helperSet = parent::getDefaultHelperSet();
137
        $helperSet->set(new \KevinGH\Box\Console\ConfigurationHelper());
138
139
        return $helperSet;
140
    }
141
}
142