Passed
Branch master (ae28f9)
by Alexey
03:06
created

Routes::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1.008
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Framework\Commands;
4
5
use Symfony\Component\Console\Helper\Table;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Venta\Console\Command;
9
use Venta\Contracts\Routing\RouteCollector;
10
11
/**
12
 * Class Route
13
 *
14
 * @package Venta\Commands
15
 */
16
class Routes extends Command
17
{
18
19
    /**
20
     * @var RouteCollector
21
     */
22
    protected $collector;
23
24
    /**
25
     * Routes constructor.
26
     *
27
     * @param RouteCollector $collector
28
     */
29 1
    public function __construct(RouteCollector $collector)
30
    {
31 1
        parent::__construct();
32 1
        $this->collector = $collector;
33 1
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38 1
    public function description(): string
39
    {
40 1
        return 'Lists application routes';
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 1
    public function handle(InputInterface $input, OutputInterface $output)
47
    {
48 1
        $routes = $this->collector->getRoutes();
49 1
        if (count($routes) == 0) {
50 1
            $this->writeln('<error>Application has no routes.</error>');
51
        } else {
52
            $table = new Table($output);
53
            $table->setHeaders(['Methods', 'Path', 'Action', 'Name', 'Host', 'Scheme', 'Middlewares']);
54
            foreach ($routes as $route) {
55
                $table->addRow([
56
                    join(',', $route->getMethods()),
57
                    $route->getPath(),
58
                    is_string($route->getCallable()) ? $route->getCallable() : get_class($route->getCallable()),
59
                    $route->getName(),
60
                    $route->getHost(),
61
                    $route->getScheme(),
62
                    join(',', array_keys($route->getMiddlewares())),
63
                ]);
64
            }
65
            $table->render();
66
        }
67 1
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72 1
    public function signature(): string
73
    {
74 1
        return 'route:list';
75
    }
76
77
}