Passed
Pull Request — master (#98)
by Rustam
02:19
created

Application   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 22
c 3
b 0
f 0
dl 0
loc 57
ccs 26
cts 30
cp 0.8667
rs 10

7 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 addOptions() 0 3 1
A doRenderThrowable() 0 17 5
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 29
    public function __construct(string $name = 'Yii Console', string $version = self::VERSION)
25
    {
26 29
        parent::__construct($name, $version);
27 29
    }
28
29 29
    public function setDispatcher(SymfonyEventDispatcherInterface $dispatcher): void
30
    {
31 29
        $this->dispatcher = $dispatcher;
32 29
        parent::setDispatcher($dispatcher);
33 29
    }
34
35 2
    public function getDispatcher(): EventDispatcherInterface
36
    {
37 2
        return $this->dispatcher;
38
    }
39
40 1
    public function start(): void
41
    {
42 1
        $this->dispatcher->dispatch(new ApplicationStartup());
43 1
    }
44
45 1
    public function shutdown(int $exitCode): void
46
    {
47 1
        $this->dispatcher->dispatch(new ApplicationShutdown($exitCode));
48 1
    }
49
50 1
    protected function doRenderThrowable(Throwable $e, OutputInterface $output): void
51
    {
52 1
        parent::doRenderThrowable($e, $output);
53
        // Friendly Exception support
54 1
        if ($e instanceof FriendlyExceptionInterface) {
55 1
            if ($output instanceof StyleInterface) {
56
                $output->title($e->getName());
57
                if ($e->getSolution() !== null) {
58
                    $output->note((string)$e->getSolution());
59
                }
60
                $output->newLine();
61
            } else {
62 1
                $output->write('<fg=red>' . $e->getName() . '</>', true);
63 1
                if ($e->getSolution() !== null) {
64 1
                    $output->write('<fg=yellow>' . $e->getSolution() . '</>', true);
65
                }
66 1
                $output->write('', true);
67
            }
68
        }
69 1
    }
70
71 29
    public function addOptions(InputOption $options): void
72
    {
73 29
        $this->getDefinition()->addOption($options);
74 29
    }
75
}
76