Passed
Push — master ( 5c7ba7...439db4 )
by Alexander
02:14
created

Application   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 24
c 2
b 0
f 0
dl 0
loc 64
ccs 34
cts 34
cp 1
rs 10

8 Methods

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