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

PageViewRouter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 99
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getBaseUrl() 0 4 1
A setBaseUrl() 0 4 1
B registerPageView() 0 38 7
A getRoute() 0 4 1
A getRoutes() 0 4 1
A getRouteMapping() 0 4 1
A extractUrlParameters() 0 8 1
1
<?php
2
3
namespace allejo\stakx\Server;
4
5
use __\__;
6
use allejo\stakx\Document\BasePageView;
7
use allejo\stakx\Document\DynamicPageView;
8
use allejo\stakx\Document\RepeaterPageView;
9
use allejo\stakx\Document\StaticPageView;
10
use allejo\stakx\FrontMatter\FrontMatterParser;
11
12
class PageViewRouter
13
{
14
    private $redirects;
15
    private $baseUrl;
16
    private $mapping;
17
18
    public function __construct()
19
    {
20
        $this->mapping = [];
21
    }
22
23
    public function getBaseUrl()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
24
    {
25
        return $this->baseUrl;
26
    }
27
28
    public function setBaseUrl($baseUrl)
29
    {
30
        $this->baseUrl = $baseUrl;
31
    }
32
33
    /**
34
     * Register a PageView to this router.
35
     */
36
    public function registerPageView(BasePageView $pageView)
37
    {
38
        switch ($pageView->getType())
39
        {
40
            case BasePageView::STATIC_TYPE:
41
                $this->mapping[$pageView->getPermalink()] = $pageView;
42
43
                foreach ($pageView->getRedirects() as $redirect)
44
                {
45
                    $this->redirects[$redirect] = $pageView->getPermalink();
46
                }
47
48
                break;
49
50
            case BasePageView::DYNAMIC_TYPE:
51
                $rawFM = $pageView->getRawFrontMatter();
52
                $permalinkFM = \__::get($rawFM, 'permalink');
53
54
                $permalink = is_array($permalinkFM) ? $permalinkFM[0] : $permalinkFM;
55
                $permalink = preg_replace(FrontMatterParser::VARIABLE_DEF, '{$1}', $permalink);
56
57
                $this->mapping[$permalink] = $pageView;
58
                break;
59
60
            case BasePageView::REPEATER_TYPE:
61
                $rawFM = $pageView->getRawFrontMatter();
62
                $permalinkFM = \__::get($rawFM, 'permalink');
63
64
                $permalink = is_array($permalinkFM) ? $permalinkFM[0] : $permalinkFM;
65
                $permalink = preg_replace(FrontMatterParser::ARRAY_DEF, '{$1}', $permalink);
66
67
                // Replace `.` in complex FM variables because they don't work in routes
68
                $permalink = preg_replace('/\./', '_', $permalink);
69
70
                $this->mapping[$permalink] = $pageView;
71
                break;
72
        }
73
    }
74
75
    /**
76
     * Get the PageView used for a specified route.
77
     *
78
     * @param string $route
79
     *
80
     * @return StaticPageView|DynamicPageView|RepeaterPageView|null
81
     */
82
    public function getRoute($route)
83
    {
84
        return __::get($this->mapping, $route);
85
    }
86
87
    /**
88
     * Retrieve all of the URL routes registered.
89
     *
90
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<integer|string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
91
     */
92
    public function getRoutes()
93
    {
94
        return array_keys($this->mapping);
95
    }
96
97
    public function &getRouteMapping()
98
    {
99
        return $this->mapping;
100
    }
101
102
    public static function extractUrlParameters($permalink)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
103
    {
104
        $matches = [];
105
106
        preg_match_all(FrontMatterParser::ANY_VARIABLE, $permalink, $matches);
107
108
        return $matches[1];
109
    }
110
}
111