Passed
Pull Request — master (#79)
by Wilmer
08:11
created

Application::setDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
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\Contracts\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\StyleInterface;
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
    private EventDispatcherInterface $dispatcher;
22
23 2
    public function __construct(string $name = 'Yii Console', string $version = self::VERSION)
24
    {
25 2
        parent::__construct($name, $version);
26 2
    }
27
28 2
    public function setDispatcher(SymfonyEventDispatcherInterface $dispatcher): void
29
    {
30 2
        $this->dispatcher = $dispatcher;
31 2
        parent::setDispatcher($dispatcher);
32 2
    }
33
34
    public function getDispatcher(): EventDispatcherInterface
35
    {
36
        return $this->dispatcher;
37
    }
38
39
    public function start(): void
40
    {
41
        $this->dispatcher->dispatch(new ApplicationStartup());
42
    }
43
44
    public function shutdown(int $exitCode): void
45
    {
46
        $this->dispatcher->dispatch(new ApplicationShutdown($exitCode));
47
    }
48
49
    public function doRenderException(Exception $e, OutputInterface $output): void
50
    {
51
        parent::doRenderException($e, $output);
0 ignored issues
show
introduced by
The method doRenderException() does not exist on Symfony\Component\Console\Application. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        parent::/** @scrutinizer ignore-call */ 
52
                doRenderException($e, $output);
Loading history...
52
        // Friendly Exception support
53
        if ($e instanceof FriendlyExceptionInterface) {
54
            if ($output instanceof StyleInterface) {
55
                $output->title($e->getName());
56
                $output->note($e->getSolution());
57
                $output->newLine();
58
            } else {
59
                $output->writeln([
60
                    '<fg=red>' . $e->getName() . '</>',
61
                    '<fg=yellow>' . $e->getSolution() . '</>',
62
                    '',
63
                ]);
64
            }
65
        }
66
    }
67
68 2
    public function addOptions(InputOption $options): void
69
    {
70 2
        $this->getDefinition()->addOption($options);
71 2
    }
72
}
73