PhpViewMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 3
eloc 15
dl 0
loc 45
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A process() 0 20 2
1
<?php
2
3
namespace App\Application\Middleware;
4
5
use App\Infrastructure\JsCacheBusting\JsImportCacheBuster;
6
use App\Infrastructure\Settings\Settings;
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Slim\App;
13
use Slim\Interfaces\RouteParserInterface;
14
use Slim\Routing\RouteContext;
15
use Slim\Views\PhpRenderer;
16
17
/**
18
 * Adds attributes to the PhpRenderer and updates js imports with version number.
19
 * Documentation: https://samuel-gfeller.ch/docs/Template-Rendering.
20
 */
21
final class PhpViewMiddleware implements MiddlewareInterface
22
{
23
    /** @var array<string, mixed> */
24
    private array $publicSettings;
25
    /** @var array<string, mixed> */
26
    private array $deploymentSettings;
27
28
    /**
29
     * @param App<ContainerInterface> $app
30
     * @param PhpRenderer $phpRenderer
31
     * @param JsImportCacheBuster $jsImportCacheBuster
32
     * @param Settings $settings
33
     * @param RouteParserInterface $routeParser
34
     */
35 17
    public function __construct(
36
        private readonly App $app,
37
        private readonly PhpRenderer $phpRenderer,
38
        private readonly JsImportCacheBuster $jsImportCacheBuster,
39
        Settings $settings,
40
        private readonly RouteParserInterface $routeParser,
41
    ) {
42 17
        $this->publicSettings = $settings->get('public');
43 17
        $this->deploymentSettings = $settings->get('deployment');
44
    }
45
46 17
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
47
    {
48
        // The following has to work even with no connection to mysql to display the error page (layout needs those attr)
49 17
        $this->phpRenderer->setAttributes([
50 17
            'version' => $this->deploymentSettings['version'],
51 17
            'uri' => $request->getUri(),
52 17
            'basePath' => $this->app->getBasePath(),
53 17
            'route' => $this->routeParser,
54 17
            'currRouteName' => RouteContext::fromRequest($request)->getRoute()?->getName(),
55
            // Used for public values (e.g. app name) used by templates or layout
56 17
            'config' => $this->publicSettings,
57 17
        ]);
58
59
        // Add version number to js imports in development mode
60
        // https://samuel-gfeller.ch/docs/Template-Rendering#js-import-cache-busting
61 17
        if ($this->deploymentSettings['update_js_imports_version'] === true) {
62
            $this->jsImportCacheBuster->addVersionToJsImports();
63
        }
64
65 17
        return $handler->handle($request);
66
    }
67
}
68