PageHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chinstrap\Core\Http\Handlers;
6
7
use Chinstrap\Core\Contracts\Sources\Source;
8
use Chinstrap\Core\Contracts\Views\Renderer;
9
use League\Route\Http\Exception\NotFoundException;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
13
final class PageHandler
14
{
15
    private ResponseInterface $response;
16
    private Source $source;
17
    private Renderer $view;
18
19
    public function __construct(
20
        ResponseInterface $response,
21
        Source $source,
22
        Renderer $view
23
    ) {
24
        $this->response = $response;
25
        $this->source = $source;
26
        $this->view = $view;
27
    }
28
29
    /**
30
     * GET request to content page
31
     *
32
     * @param ServerRequestInterface $request
33
     * @param array{name: ?string} $args
34
     */
35
    public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
36
    {
37
        $name = isset($args['name']) ? $args['name'] : 'index';
38
        if (!$document = $this->source->find($name)) {
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Chinstrap\Core\Contracts\Sources\Source::find() does only seem to accept string, 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

38
        if (!$document = $this->source->find(/** @scrutinizer ignore-type */ $name)) {
Loading history...
39
            throw new NotFoundException('Page not found');
40
        }
41
        $data = $document->getFields();
42
        $data['content'] = $document->getContent();
43
        $layout = isset($data['layout']) ? $data['layout'] . '.html' : 'default.html';
44
        $response = $this->response->withAddedHeader(
45
            'Last-Modified',
46
            $document->getUpdatedAt()->format('D, d M Y H:i:s') . ' GMT'
47
        );
48
        return $this->view->render($response, $layout, $data);
49
    }
50
}
51