Completed
Push — development ( cfd391...deed4d )
by Andrij
12:03
created

Router::isModule()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 5
nop 1
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
3
namespace core\src;
4
5
use core\models\Route;
6
use core\models\RouteQuery;
7
use core\src\Exception\PageNotFoundException;
8
9
/**
10
 * Class Router
11
 *
12
 * @package core\src
13
 * @todo    remove PageException and url check
14
 */
15
class Router
16
{
17
18
    private $modules = [];
19
20
    private $deniedSegments
21
                     = [
0 ignored issues
show
introduced by
Multi-line assignment not indented correctly; expected 6 spaces but found 21
Loading history...
Coding Style introduced by
Multi-line assignment not indented correctly; expected 8 spaces but found 21
Loading history...
22
                        '_install',
23
                        '_deinstall',
24
                        '_install_rules',
25
                        'autoload',
26
                        '__construct',
27
                       ];
28
29
    public function setModules(array $modules) {
30
        $this->modules = $modules;
31
    }
32
33
    public function findRoute($url) {
34
        $this->checkUrl($url);
35
        $url = trim($url, '/');
36
37
        if ($route = $this->isMainPage($url)) {
38
            return $route;
39
        }
40
41
        if ($route = $this->isModule($url)) {
42
            return $route;
43
        }
44
45
        if ($route = $this->isRoute($url)) {
46
            return $route;
47
        }
48
49
    }
50
51
    private function checkUrl($url) {
52
        if (strpos($url, '//') !== false) {
53
            throw new PageNotFoundException();
54
        }
55
56
        if (count(explode('/_', $url)) > 1) {
57
            throw  new PageNotFoundException();
58
        }
59
60
        foreach ($this->segments as $segment) {
0 ignored issues
show
Bug introduced by
The property segments does not seem to exist. Did you mean deniedSegments?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
61
            if (in_array($segment, $this->deniedSegments) == true) {
62
                throw new PageNotFoundException();
63
            }
64
        }
65
    }
66
67
    private function isMainPage($url) {
68
        if ($url == '') {
69
            $route = new Route();
70
            $route->setType(Route::TYPE_MAIN);
71
72
            return $route;
73
        }
74
    }
75
76
    private function isRoute($url) {
77
        $routeUrl = explode('/', $url);
78
        $last     = array_pop($routeUrl);
79
80
        $route = RouteQuery::create()->filterByUrl($last)->findOne();
81
82
        if ($route && $this->checkRoute($route, $url)) {
83
            return $route;
84
        }
85
    }
86
87
    private function checkRoute(Route $route, $url) {
88
        if ($route->getRouteUrl() !== $url) {
89
            throw new PageNotFoundException();
90
        }
91
92
        return true;
93
94
    }
95
96
    /**
97
     * @param $url
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
98
     *
99
     * @return bool|Route
0 ignored issues
show
Documentation introduced by
Should the return type not be false|Route|null?

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...
100
     */
101
    private function isModule($url) {
102
        if (strpos(trim($url, '/'), 'shop/category') === 0) {
103
            return false;
104
        }
105
        if (strpos(trim($url, '/'), 'shop/product') === 0) {
106
            return false;
107
        }
108
        $moduleSegments = explode('/', $url);
109
        $moduleName     = array_shift($moduleSegments);
110
111
        foreach ($this->modules as $module) {
112
            if ($module['identif'] === $moduleName AND $module['enabled'] == 1) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected and, but found AND.
Loading history...
113
114
                $route = new Route();
115
                $route->setType(Route::TYPE_MODULE);
116
                $route->setUrl($url);
117
118
                return $route;
119
            }
120
        }
121
    }
122
123
}