Passed
Pull Request — master (#95)
by Wilmer
02:57
created

Application::getDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Console;
6
7
use Exception;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\StyleInterface;
12
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
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
    public function doRenderException(Exception $e, OutputInterface $output): void
51
    {
52
        // Friendly Exception support
53 1
        if ($e instanceof FriendlyExceptionInterface) {
54 1
            if ($output instanceof StyleInterface) {
55 1
                $output->title($e->getName());
56 1
                $output->note((string) $e->getSolution());
57 1
                $output->newLine();
58
            } else {
59 1
                $output->writeln([
60 1
                    '<fg=red>' . $e->getName() . '</>',
61 1
                    '<fg=yellow>' . (string) $e->getSolution() . '</>',
62 1
                    '',
63
                ]);
64
            }
65
        }
66 1
    }
67
68 29
    public function addOptions(InputOption $options): void
69
    {
70 29
        $this->getDefinition()->addOption($options);
71 29
    }
72
}
73