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

PageViewRouter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 88
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 27 6
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
            case BasePageView::REPEATER_TYPE:
52
                $rawFM = $pageView->getRawFrontMatter();
53
                $permalinkFM = \__::get($rawFM, 'permalink');
54
55
                $permalink = is_array($permalinkFM) ? $permalinkFM[0] : $permalinkFM;
56
                $permalink = preg_replace(FrontMatterParser::VARIABLE_DEF, '{$1}', $permalink);
57
                $permalink = preg_replace(FrontMatterParser::ARRAY_DEF, '{$1}', $permalink);
58
59
                $this->mapping[$permalink] = $pageView;
60
                break;
61
        }
62
    }
63
64
    /**
65
     * Get the PageView used for a specified route.
66
     *
67
     * @param string $route
68
     *
69
     * @return StaticPageView|DynamicPageView|RepeaterPageView|null
70
     */
71
    public function getRoute($route)
72
    {
73
        return __::get($this->mapping, $route);
74
    }
75
76
    /**
77
     * Retrieve all of the URL routes registered.
78
     *
79
     * @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...
80
     */
81
    public function getRoutes()
82
    {
83
        return array_keys($this->mapping);
84
    }
85
86
    public function &getRouteMapping()
87
    {
88
        return $this->mapping;
89
    }
90
91
    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...
92
    {
93
        $matches = [];
94
95
        preg_match_all(FrontMatterParser::ANY_VARIABLE, $permalink, $matches);
96
97
        return $matches[1];
98
    }
99
}
100