StackDisplayCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 34
c 1
b 0
f 0
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parsePosition() 0 10 2
A parseMiddlewareClass() 0 11 3
A execute() 0 22 2
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\UserInterface\Console;
13
14
use Slick\Http\Message\Server\Request;
15
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandlerInterface;
16
use Slick\ModuleApi\Infrastructure\FrontController\Position;
17
use Slick\WebStack\Infrastructure\FrontController\WebApplication;
18
use Symfony\Component\Console\Attribute\AsCommand;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
24
/**
25
 * StackDisplayCommand
26
 *
27
 * @package Slick\WebStack\UserInterface\Console
28
 */
29
#[AsCommand(
30
    name: "app:http-stack",
31
    description: "Displays the list of the middlewares configured on the HTTP stack.",
32
    aliases: ["stack"]
33
)]
34
final class StackDisplayCommand extends Command
35
{
36
    /**
37
     * Executes the command.
38
     *
39
     * @param InputInterface $input The input object.
40
     * @param OutputInterface $output The output object.
41
     *
42
     * @return int The exit code of the command.
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output): int
45
    {
46
        $style = new SymfonyStyle($input, $output);
47
        $app = new WebApplication(new Request("GET", "/"), APP_ROOT);
48
        $app->loadModules();
49
        $app->loadMiddlewares();
50
        $list = $app->middlewareList();
51
        $table = $style->createTable();
52
        $table->setHeaderTitle("HTTP Stack Middlewares");
53
        $table->setHeaders(["Name", "Middleware", "Position"]);
54
        /** @var MiddlewareHandlerInterface $middleware */
55
        foreach ($list as $middleware) {
56
            $table->addRow([
57
                $middleware->name(),
58
                $this->parseMiddlewareClass($middleware),
59
                $this->parsePosition($middleware)
60
            ]);
61
        }
62
        $style->writeln('');
63
        $table->render();
64
        $style->write("  <info>Modules file: </info>{$app->rootPath()}".$app::MODULES_PATH."\n");
65
        return Command::SUCCESS;
66
    }
67
68
    private function parseMiddlewareClass(MiddlewareHandlerInterface $middleware): string
69
    {
70
        if (is_string($middleware->handler())) {
71
            return $middleware->handler();
72
        }
73
74
        if (is_callable($middleware->handler())) {
75
            return "--Callable--";
76
        }
77
78
        return get_class($middleware->handler());
0 ignored issues
show
Bug introduced by
It seems like $middleware->handler() can also be of type callable; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

78
        return get_class(/** @scrutinizer ignore-type */ $middleware->handler());
Loading history...
79
    }
80
81
    private function parsePosition(MiddlewareHandlerInterface $middleware): string
82
    {
83
        $reference = $middleware->middlewarePosition()->reference();
84
        $refCheck = fn (?string $ref) => $ref ? " of $ref" : "";
85
86
        return match ($middleware->middlewarePosition()->position()->value) {
87
            Position::Top->value => "On top{$refCheck($reference)}",
88
            Position::Bottom->value => "On bottom{$refCheck($reference)}",
89
            Position::After->value => "After $reference",
90
            Position::Before->value => "Before $reference",
91
        };
92
    }
93
}
94