Completed
Pull Request — master (#75)
by Vladimir
02:24
created

RouteDispatcher::staticPageViewController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace allejo\stakx\Server;
4
5
use allejo\stakx\Compiler;
6
use allejo\stakx\Document\BasePageView;
7
use allejo\stakx\Document\CollectableItem;
8
use allejo\stakx\Document\DynamicPageView;
9
use allejo\stakx\Document\ReadableDocument;
10
use allejo\stakx\Document\RepeaterPageView;
11
use allejo\stakx\Document\StaticPageView;
12
use allejo\stakx\Document\TemplateReadyDocument;
13
use allejo\stakx\Filesystem\FilesystemLoader as fs;
14
use FastRoute\RouteCollector;
15
use Psr\Http\Message\ServerRequestInterface;
16
use React\Http\Response;
17
18
class RouteDispatcher
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
19
{
20
    /**
21
     * @internal
22
     */
23
    private function __construct()
24
    {
25
    }
26
27
    /**
28
     * Build a controller for handling a Static PageView's URL.
29
     *
30
     * @param StaticPageView $pageView
31
     * @param Compiler       $compiler
32
     *
33
     * @return \Closure
34
     */
35
    private function staticPageViewController(StaticPageView $pageView, Compiler $compiler)
36
    {
37
        return function () use ($pageView, $compiler) {
38
            $pageView->readContent();
39
            $mimeType = MimeDetector::getMimeType(fs::getExtension($pageView->getTargetFile()));
0 ignored issues
show
Documentation introduced by
$pageView->getTargetFile() is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
41
            return new Response(
42
                200,
43
                ['Content-Type' => $mimeType],
44
                $compiler->renderStaticPageView($pageView)
45
            );
46
        };
47
    }
48
49
    /**
50
     * Build a controller for handling a Dynamic PageView's URL.
51
     *
52
     * @param DynamicPageView $pageView
53
     * @param Compiler        $compiler
54
     *
55
     * @return \Closure
56
     */
57
    private function dynamicPageViewController(DynamicPageView $pageView, Compiler $compiler)
58
    {
59
        return function (ServerRequestInterface $request) use ($pageView, $compiler) {
60
            $contentItem = self::getContentItem($pageView, $request->getUri()->getPath());
61
62
            if ($contentItem === null)
63
            {
64
                return DevServer::return404();
65
            }
66
67
            $pageView->readContent();
68
            $contentItem->readContent();
69
70
            return DevServer::return200($compiler->renderDynamicPageView($pageView, $contentItem));
0 ignored issues
show
Documentation introduced by
$contentItem is of type object<allejo\stakx\Document\CollectableItem>, but the function expects a object<allejo\stakx\Docu...\TemplateReadyDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
        };
72
    }
73
74
    /**
75
     * Return the appropriate controller based on a PageView's type.
76
     *
77
     * @param BasePageView|DynamicPageView|RepeaterPageView|StaticPageView $pageView
78
     * @param Compiler     $compiler
79
     *
80
     * @return \Closure
81
     */
82
    private function createController(BasePageView $pageView, Compiler $compiler)
83
    {
84
        switch ($pageView->getType())
85
        {
86
            case BasePageView::STATIC_TYPE:
87
                return $this->staticPageViewController($pageView, $compiler);
0 ignored issues
show
Compatibility introduced by
$pageView of type object<allejo\stakx\Document\BasePageView> is not a sub-type of object<allejo\stakx\Document\StaticPageView>. It seems like you assume a child class of the class allejo\stakx\Document\BasePageView to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
88
89
            case BasePageView::DYNAMIC_TYPE:
90
                return $this->dynamicPageViewController($pageView, $compiler);
0 ignored issues
show
Compatibility introduced by
$pageView of type object<allejo\stakx\Document\BasePageView> is not a sub-type of object<allejo\stakx\Document\DynamicPageView>. It seems like you assume a child class of the class allejo\stakx\Document\BasePageView to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
91
92
            default:
93
                return function () {
94
                    $errMsg = 'This URL type has not yet been implemented.';
95
96
                    return new Response(501, ['Content-Type' => 'text/plain'], $errMsg);
97
                };
98
        }
99
    }
100
101
    /**
102
     * Create a FastRoute Dispatcher.
103
     *
104
     * @param PageViewRouter $router
105
     * @param Compiler       $compiler
106
     *
107
     * @return \FastRoute\Dispatcher
108
     */
109
    public static function create(PageViewRouter $router, Compiler $compiler)
110
    {
111
        return \FastRoute\simpleDispatcher(function (RouteCollector $r) use ($router, $compiler) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
112
            $dispatcher = new RouteDispatcher();
113
114
            foreach ($router->getRouteMapping() as $route => $pageView)
115
            {
116
                $r->get($route, $dispatcher->createController($pageView, $compiler));
117
            }
118
        });
119
    }
120
121
    /**
122
     * Find a ContentItem from a Dynamic PageView or null if it doesn't exist.
123
     *
124
     * @param DynamicPageView $pageView
125
     * @param                 $permalink
126
     *
127
     * @return CollectableItem|ReadableDocument|TemplateReadyDocument|null
128
     */
129
    private static function getContentItem(DynamicPageView $pageView, $permalink)
130
    {
131
        foreach ($pageView->getCollectableItems() as $collectableItem)
132
        {
133
            if ($collectableItem['permalink'] === $permalink)
134
            {
135
                return $collectableItem;
136
            }
137
        }
138
139
        return null;
140
    }
141
}
142