Passed
Pull Request — master (#129)
by Evgeniy
11:18
created

Application   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
eloc 25
c 4
b 0
f 0
dl 0
loc 66
ccs 34
cts 34
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A extractNamespace() 0 5 2
A renderThrowable() 0 5 1
A shutdown() 0 3 1
A doRenderThrowable() 0 21 5
A start() 0 3 1
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;
26
27 43
    public function __construct(
28
        EventDispatcherInterface $dispatcher,
29
        string $name = self::NAME,
30
        string $version = self::VERSION
31
    ) {
32 43
        $this->dispatcher = $dispatcher;
33 43
        parent::__construct($name, $version);
34 43
    }
35
36 3
    public function start(): void
37
    {
38 3
        $this->dispatcher->dispatch(new ApplicationStartup());
39 3
    }
40
41 3
    public function shutdown(int $exitCode): void
42
    {
43 3
        $this->dispatcher->dispatch(new ApplicationShutdown($exitCode));
44 3
    }
45
46 3
    public function renderThrowable(Throwable $e, OutputInterface $output): void
47
    {
48 3
        $output->writeln('', OutputInterface::VERBOSITY_QUIET);
49
50 3
        $this->doRenderThrowable($e, $output);
51 3
    }
52
53 3
    protected function doRenderThrowable(Throwable $e, OutputInterface $output): void
54
    {
55 3
        parent::doRenderThrowable($e, $output);
56
        // Friendly Exception support
57 3
        if ($e instanceof FriendlyExceptionInterface) {
58 3
            if ($output instanceof StyleInterface) {
59 1
                $output->title($e->getName());
60 1
                if (($solution = $e->getSolution()) !== null) {
61 1
                    $output->note($solution);
62
                }
63 1
                $output->newLine();
64
            } else {
65 2
                $output->writeln('<fg=red>' . $e->getName() . '</>');
66 2
                if (($solution = $e->getSolution()) !== null) {
67 2
                    $output->writeln('<fg=yellow>' . $solution . '</>');
68
                }
69 2
                $output->writeln('');
70
            }
71
        }
72
73 3
        $output->writeln($e->getTraceAsString());
74 3
    }
75
76 43
    public function addOptions(InputOption $options): void
77
    {
78 43
        $this->getDefinition()->addOption($options);
79 43
    }
80
81 5
    public function extractNamespace(string $name, int $limit = null): string
82
    {
83 5
        $parts = explode('/', $name, -1);
84
85 5
        return implode('/', null === $limit ? $parts : array_slice($parts, 0, $limit));
86
    }
87
}
88