Passed
Push — master ( 3dfc64...76adab )
by Evgeniy
02:21
created

Application   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 15
eloc 28
c 4
b 0
f 0
dl 0
loc 72
ccs 39
cts 39
cp 1
rs 10

8 Methods

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