Passed
Branch v2-dev (0ddf7c)
by Henri
10:02
created

Router::set()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 4
Metric Value
cc 3
eloc 17
c 9
b 0
f 4
nc 4
nop 3
dl 0
loc 25
rs 9.7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Router::current() 0 4 1
A Router::currentAction() 0 4 1
A Router::currentName() 0 4 1
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
6
class Router implements RouterInterface
7
{
8
    use DefinitionsTrait, 
9
        RunInTrait, 
10
        CheckTrait, 
11
        OwnerTrait, 
12
        MiddlewareTrait, 
13
        WhereTrait,
14
        PrioritizeTrait;
15
16
    private array $currentRoute = [];
17
    private \Closure $beforeAll;
0 ignored issues
show
introduced by
The private property $beforeAll is not used, and could be removed.
Loading history...
18
    private \Closure $afterAll;
0 ignored issues
show
introduced by
The private property $afterAll is not used, and could be removed.
Loading history...
19
    private string $host = '';
20
    private string $prefix = '';
21
    private bool $loaded = false;
22
23
    public static function defineHost(string $host): Router
24
    {
25
        self::getInstance()->host = $host;
26
        return self::getInstance();
27
    }
28
29
    public static function name(string $name): Router
30
    {
31
        self::getInstance()->isInNameGroup();
32
        self::getInstance()->existRouteName($name);
33
        $route = self::getInstance()->inSave();
34
        $route['name'] = $name;
35
        self::getInstance()->routesNames[$name] = $name;
0 ignored issues
show
Bug Best Practice introduced by
The property routesNames does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
        self::getInstance()->unsetRoute(count(self::getInstance()->routes)-1)->updateRoute($route,$name);
37
        return self::getInstance();
38
    }
39
40
    public static function group(string $prefix, \Closure $closure): Router
41
    {
42
        self::getInstance()->prefix = $prefix;
43
        self::getInstance()->group = sha1(date('d/m/Y h:m:i'));
44
45
        $closure();
46
47
        self::getInstance()->group = null;
48
        self::getInstance()->prefix = null;
49
        return self::getInstance();
50
    }
51
52
    public static function current(): array
53
    {
54
        self::getInstance()->hasCurrentRoute();
55
        return self::getInstance()->currentRoute;
56
    }
57
58
    public static function currentName(): string
59
    {
60
        self::getInstance()->hasCurrentRoute();
61
        return self::getInstance()->currentRoute['name'];
62
    }
63
64
    public static function currentAction()
65
    {
66
        self::getInstance()->hasCurrentRoute();
67
        return self::getInstance()->currentRoute['action'];
68
    }
69
70
    public static function load(): RouterInterface
71
    {
72
        self::getInstance()->loaded = true;
73
74
        foreach(self::getInstance()->routes as $r => $route){
75
            self::getInstance()->currentRoute = $route;
76
            self::getInstance()->currentRoute['name'] = $r;
77
78
            try{
79
                self::getInstance()->checkMethod($route, $_SERVER['REQUEST_METHOD']);
80
                self::getInstance()->checkData($route['uri']->getPath(), $_SERVER['REQUEST_URI']);
81
                self::getInstance()->sortRoutes();                
82
                return self::getInstance();
83
            }catch(\Exception $er){
84
                continue;
85
            }
86
        }
87
        
88
        self::getInstance()->currentRoute = [];
89
        throw new \Exception('Page not found', 404);
90
    }
91
92
    public static function run(): RouterInterface
93
    {
94
        if(!self::getInstance()->loaded){
95
            self::getInstance()->load();
96
        }
97
98
        //echo '<pre>';
99
        //var_dump(self::getInstance()->currentRoute['uri']->getPath());
100
        // ...
101
        return self::getInstance();
102
    }
103
   
104
}