Passed
Pull Request — master (#98)
by Rustam
01:59
created

Application::doRenderThrowable()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.5555
cc 5
nc 5
nop 2
crap 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 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
    protected function doRenderThrowable(Throwable $e, OutputInterface $output): void
51
    {
52 2
        parent::doRenderThrowable($e, $output);
53
        // Friendly Exception support
54 2
        if ($e instanceof FriendlyExceptionInterface) {
55 2
            if ($output instanceof StyleInterface) {
56 1
                $output->title($e->getName());
57 1
                if ($e->getSolution() !== null) {
58 1
                    $output->note((string)$e->getSolution());
59
                }
60 1
                $output->newLine();
61
            } else {
62 1
                $output->writeln('<fg=red>' . $e->getName() . '</>');
63 1
                if ($e->getSolution() !== null) {
64 1
                    $output->writeln('<fg=yellow>' . $e->getSolution() . '</>');
65
                }
66 1
                $output->writeln('');
67
            }
68
        }
69 2
    }
70
71 30
    public function addOptions(InputOption $options): void
72
    {
73 30
        $this->getDefinition()->addOption($options);
74 30
    }
75
}
76