FrontControllerModule::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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;
13
14
use JsonException;
15
use Psr\Container\ContainerExceptionInterface;
16
use Psr\Container\NotFoundExceptionInterface;
17
use Slick\Di\ContainerInterface;
18
use Slick\Di\Definition\ObjectDefinition;
19
use Slick\Http\Message\Response;
20
use Slick\Http\Session\Driver\ServerDriver;
21
use Slick\Http\Session\SessionDriverInterface;
22
use Slick\ModuleApi\Infrastructure\AbstractModule;
23
use Slick\ModuleApi\Infrastructure\Console\ConsoleModuleInterface;
24
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandler;
25
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandlerInterface;
26
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewarePosition;
0 ignored issues
show
Bug introduced by
The type Slick\ModuleApi\Infrastr...ller\MiddlewarePosition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Slick\ModuleApi\Infrastructure\FrontController\Position;
28
use Slick\ModuleApi\Infrastructure\FrontController\WebModuleInterface;
29
use Slick\ModuleApi\Infrastructure\SlickModuleInterface;
30
use Slick\WebStack\Infrastructure\ComposerParser;
31
use Slick\WebStack\Infrastructure\DependencyContainerFactory;
32
use Slick\WebStack\Infrastructure\Http\FlashMessageStorage;
33
use Slick\WebStack\UserInterface\Console\StackDisplayCommand;
34
use Symfony\Component\Console\Application;
35
use function Slick\ModuleApi\importSettingsFile;
36
37
/**
38
 * FrontControllerModule
39
 *
40
 * @package Slick\WebStack
41
 * @SuppressWarnings(PHPMD)
42
 */
43
final class FrontControllerModule extends AbstractModule implements
44
    SlickModuleInterface,
45
    WebModuleInterface,
46
    ConsoleModuleInterface
47
{
48
49
    private ComposerParser $composerParser;
50
51
    /**
52
     * Creates a FrontControllerSlickModule
53
     *
54
     * @throws JsonException
55
     */
56
    public function __construct()
57
    {
58
        $this->composerParser = new ComposerParser(APP_ROOT.'/composer.json');
59
    }
60
61
    public function description(): string
62
    {
63
        return "Core module that initializes a web application using the front controller pattern.";
64
    }
65
66
    /**
67
     * @inheritDoc
68
     * @return array<string, mixed>
69
     */
70
    public function services(): array
71
    {
72
        $default = [
73
            FlashMessageStorage::class => ObjectDefinition::
74
                create(FlashMessageStorage::class)
75
                ->with('@session'),
76
            SessionDriverInterface::class => '@session',
77
            'session' => ObjectDefinition::create(ServerDriver::class),
78
            'default.middleware' => function () {
79
                return fn() => new Response(
80
                    200,
81
                    $this->createDefaultContent(),
82
                    ['Content-Type' => 'text/html']
83
                );
84
            }
85
        ];
86
        return importSettingsFile(dirname(__DIR__).'/config/logging.php', $default);
87
    }
88
89
    /**
90
     * Returns an array of middleware handlers.
91
     *
92
     * @return array<MiddlewareHandlerInterface> The middleware handlers.
93
     * @throws ContainerExceptionInterface
94
     * @throws NotFoundExceptionInterface
95
     */
96
    public function middlewareHandlers(): array
97
    {
98
        return [
99
            new MiddlewareHandler(
100
                'default-response',
101
                new MiddlewarePosition(Position::Bottom),
102
                DependencyContainerFactory::instance()->container()->get('default.middleware')
103
            )
104
        ];
105
    }
106
107
    public function configureConsole(Application $cli, ContainerInterface $container): void
108
    {
109
        $args = [null];
110
        $cli->add($container->make(StackDisplayCommand::class, ...$args));
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Console\Application::add() has been deprecated: since Symfony 7.4, use Application::addCommand() instead ( Ignorable by Annotation )

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

110
        /** @scrutinizer ignore-deprecated */ $cli->add($container->make(StackDisplayCommand::class, ...$args));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
111
    }
112
113
114
    /**
115
     * Creates the default content for the HTML body.
116
     *
117
     * @return string The default content for the HTML body.
118
     */
119
    private function createDefaultContent(): string
120
    {
121
        $head = "<html lang=\"en\"><title>%s</title><body>%s</body></html>";
122
        $body = sprintf(
123
            "<h1>%s <span class=\"small\">%s</span></h1><p>%s</p>",
124
            $this->composerParser->appName(),
125
            $this->composerParser->version(),
126
            $this->composerParser->description()
127
        );
128
        return sprintf($head, $this->composerParser->appName(), $body);
129
    }
130
}
131