Passed
Push — master ( bf7b4e...f97996 )
by Dmitriy
03:45 queued 01:22
created

Application::start()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
cc 3
nc 2
nop 1
crap 3.3332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Console;
6
7
use Symfony\Component\Console\Input\ArgvInput;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\StyleInterface;
11
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
12
use Throwable;
13
use Yiisoft\FriendlyException\FriendlyExceptionInterface;
14
use Yiisoft\Yii\Console\Event\ApplicationShutdown;
15
use Yiisoft\Yii\Console\Event\ApplicationStartup;
16
17
use function array_slice;
18
use function explode;
19
use function implode;
20
21
final class Application extends \Symfony\Component\Console\Application
22
{
23
    public const NAME = 'Yii Console';
24
    public const VERSION = '1.0';
25
26
    private ?EventDispatcherInterface $dispatcher = null;
27
28 46
    public function __construct(string $name = self::NAME, string $version = self::VERSION)
29
    {
30 46
        parent::__construct($name, $version);
31
    }
32
33 46
    public function setDispatcher(EventDispatcherInterface $dispatcher): void
34
    {
35 46
        $this->dispatcher = $dispatcher;
36 46
        parent::setDispatcher($dispatcher);
37
    }
38
39 3
    public function start(?ArgvInput $input = null): void
40
    {
41 3
        if ($this->dispatcher !== null && $input !== null) {
42
            $this->dispatcher->dispatch(new ApplicationStartup($input->getFirstArgument()));
43
        }
44
    }
45
46 3
    public function shutdown(int $exitCode): void
47
    {
48 3
        if ($this->dispatcher !== null) {
49 3
            $this->dispatcher->dispatch(new ApplicationShutdown($exitCode));
50
        }
51
    }
52
53 3
    public function renderThrowable(Throwable $e, OutputInterface $output): void
54
    {
55 3
        $output->writeln('', OutputInterface::VERBOSITY_QUIET);
56
57 3
        $this->doRenderThrowable($e, $output);
58
    }
59
60 3
    protected function doRenderThrowable(Throwable $e, OutputInterface $output): void
61
    {
62 3
        parent::doRenderThrowable($e, $output);
63
64
        // Friendly Exception support
65 3
        if ($e instanceof FriendlyExceptionInterface) {
66 3
            if ($output instanceof StyleInterface) {
67 1
                $output->title($e->getName());
68 1
                if (($solution = $e->getSolution()) !== null) {
69 1
                    $output->note($solution);
70
                }
71 1
                $output->newLine();
72
            } else {
73 2
                $output->writeln('<fg=red>' . $e->getName() . '</>');
74 2
                if (($solution = $e->getSolution()) !== null) {
75 2
                    $output->writeln('<fg=yellow>' . $solution . '</>');
76
                }
77 2
                $output->writeln('');
78
            }
79
        }
80
81 3
        $output->writeln($e->getTraceAsString());
82
    }
83
84 46
    public function addOptions(InputOption $options): void
85
    {
86
        $this
87 46
            ->getDefinition()
88 46
            ->addOption($options);
89
    }
90
91 5
    public function extractNamespace(string $name, int $limit = null): string
92
    {
93 5
        $parts = explode('/', $name, -1);
94
95 5
        return implode('/', null === $limit ? $parts : array_slice($parts, 0, $limit));
96
    }
97
}
98