Completed
Push — development ( 67765c...7029e6 )
by Andrij
18:12
created

Router::setModules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
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
/**
11
 * Class Router
12
 * @package core\src
13
 * @todo remove PageException and url check
14
 */
15
class Router
16
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
17
18
    private $modules = [];
19
20
    private $deniedSegments = [
21
                               '_install',
22
                               '_deinstall',
23
                               '_install_rules',
24
                               'autoload',
25
                               '__construct',
26
                              ];
27
28
    public function setModules(array $modules) {
29
        $this->modules = $modules;
30
    }
31
32
    public function findRoute($url) {
33
        $this->checkUrl($url);
34
        $url = trim($url, '/');
35
36
        if ($route = $this->isMainPage($url)) {
37
            return $route;
38
        }
39
40
        if ($route = $this->isRoute($url)) {
41
            return $route;
42
        }
43
44
        if ($route = $this->isModule($url)) {
45
            return $route;
46
        }
47
    }
48
49
    private function checkUrl($url) {
50
        if (strpos($url, '//') !== false) {
51
            throw new PageNotFoundException();
52
        }
53
54
        if (count(explode('/_', $url)) > 1) {
55
            throw  new PageNotFoundException();
56
        }
57
58
        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...
59
            if (in_array($segment, $this->deniedSegments) == TRUE) {
60
                throw new PageNotFoundException();
61
            }
62
        }
63
    }
64
65
    private function isMainPage($url) {
66
        if ($url == '') {
67
            $route = new Route();
68
            $route->setType(Route::TYPE_MAIN);
69
            return $route;
70
        }
71
    }
72
73
    private function isRoute($url) {
74
        $routeUrl = explode('/', $url);
75
        $last = array_pop($routeUrl);
76
77
        $route = RouteQuery::create()->filterByUrl($last)
78
            ->findOne();
79
80
        if ($route && $this->checkRoute($route, $url)) {
81
            return $route;
82
        }
83
    }
84
85
    private function checkRoute(Route $route, $url) {
86
        if ($route->getRouteUrl() !== $url) {
87
            throw new PageNotFoundException();
88
        }
89
        return true;
90
91
    }
92
93
    /**
94
     * @param $url
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
95
     * @return Route
0 ignored issues
show
Documentation introduced by
Should the return type not be 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...
96
     */
97
    private function isModule($url) {
98
99
        $moduleSegments = explode('/', $url);
100
        $moduleName = array_shift($moduleSegments);
101
102
        foreach ($this->modules as $module) {
103
            if ($module['identif'] === $moduleName AND $module['enabled'] == 1) {
104
105
                $route = new Route();
106
                $route->setType(Route::TYPE_MODULE);
107
                $route->setUrl($url);
108
                return $route;
109
            }
110
        }
111
    }
112
113
}