Completed
Branch 2.0 (13ec26)
by Anton
05:17
created

ConsoleDispatcher   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A canServe() 0 5 2
A serve() 0 12 2
A handleException() 0 12 2
A mapVerbosity() 0 12 4
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Console;
10
11
use Psr\Container\ContainerExceptionInterface;
12
use Psr\Container\ContainerInterface;
13
use Spiral\Boot\DispatcherInterface;
14
use Spiral\Boot\EnvironmentInterface;
15
use Spiral\Exceptions\ConsoleHandler;
16
use Spiral\Snapshots\SnapshotterInterface;
17
use Symfony\Component\Console\Input\ArgvInput;
18
use Symfony\Component\Console\Output\ConsoleOutput;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * Manages Console commands and exception. Lazy loads console service.
23
 */
24
class ConsoleDispatcher implements DispatcherInterface
25
{
26
    /** @var EnvironmentInterface */
27
    private $environment;
28
29
    /** @var ContainerInterface */
30
    private $container;
31
32
    /**
33
     * @param EnvironmentInterface $environment
34
     * @param ContainerInterface   $container
35
     */
36
    public function __construct(EnvironmentInterface $environment, ContainerInterface $container)
37
    {
38
        $this->environment = $environment;
39
        $this->container = $container;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function canServe(): bool
46
    {
47
        // only run in pure CLI more, ignore under RoadRunner
48
        return (php_sapi_name() == 'cli' && $this->environment->get('RR') === null);
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function serve()
55
    {
56
        /** @var ConsoleCore $core */
57
        $core = $this->container->get(ConsoleCore::class);
58
        $output = new ConsoleOutput();
59
60
        try {
61
            $core->start(new ArgvInput(), $output);
62
        } catch (\Throwable $e) {
63
            $this->handleException($e, $output);
64
        }
65
    }
66
67
    /**
68
     * @param \Throwable      $e
69
     * @param OutputInterface $output
70
     */
71
    protected function handleException(\Throwable $e, OutputInterface $output)
72
    {
73
        try {
74
            $this->container->get(SnapshotterInterface::class)->register($e);
75
        } catch (\Throwable|ContainerExceptionInterface $se) {
76
            // no need to notify when unable to register an exception
77
        }
78
79
        // Explaining exception to the user
80
        $handler = new ConsoleHandler(STDERR);
0 ignored issues
show
Documentation introduced by
STDERR is of type string, but the function expects a boolean|resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
        $output->write($handler->renderException($e, $this->mapVerbosity($output)));
82
    }
83
84
    /**
85
     * @param OutputInterface $output
86
     * @return int
87
     */
88
    private function mapVerbosity(OutputInterface $output): int
89
    {
90
        if ($output->isDebug() || $output->isVeryVerbose()) {
91
            return ConsoleHandler::VERBOSITY_DEBUG;
92
        }
93
94
        if ($output->isVerbose()) {
95
            return ConsoleHandler::VERBOSITY_VERBOSE;
96
        }
97
98
        return ConsoleHandler::VERBOSITY_BASIC;
99
    }
100
}