DefinitionsTrait::set()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 1
b 0
f 0
nc 16
nop 3
dl 0
loc 31
rs 9.2888
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace HnrAzevedo\Router;
6
7
use HnrAzevedo\Http\Uri;
8
use Opis\Closure\SerializableClosure;
9
10
trait DefinitionsTrait
11
{
12
    use Helper;
13
    
14
    public static function get(string $uri, $closure): RouterInterface
15
    {
16
        return self::set('get', $uri, $closure);
17
    }
18
19
    public static function post(string $uri, $closure): RouterInterface
20
    {
21
        return self::set('post', $uri, $closure);
22
    }
23
24
    public static function ajax(string $uri, $closure): RouterInterface
25
    {
26
        return self::set('ajax', $uri, $closure);
27
    }
28
29
    public static function delete(string $uri, $closure): RouterInterface
30
    {
31
        return self::set('delete', $uri, $closure);
32
    }
33
34
    public static function put(string $uri, $closure): RouterInterface
35
    {
36
        return self::set('put', $uri, $closure);
37
    }
38
39
    public static function patch(string $uri, $closure): RouterInterface
40
    {
41
        return self::set('patch', $uri, $closure);
42
    }
43
44
    public static function match(string $method, string $uri, $closure): RouterInterface
45
    {
46
        self::set($method, $uri, $closure);
47
        return self::getInstance();
48
    }
49
50
    public static function any(string $uri, $closure): RouterInterface
51
    {
52
        return self::set('*', $uri, $closure);
53
    }
54
55
    private static $count = 0;
56
57
    private static function set(string $method, string $uri, $closure): RouterInterface
58
    {   
59
        $uri = (substr($uri, 0, 1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri;
60
        
61
        self::checkDuplicity($uri, $method);
62
        
63
        $routes = self::getInstance()->getRoutes();
64
65
        $index = count($routes);
66
67
        while(array_key_exists($index, $routes)){
68
            $index++;
69
        }
70
71
        $routes[strval($index)] = [
72
        'uri' => serialize(new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri)),
73
        'action' => (is_callable($closure)) ? serialize(new SerializableClosure($closure)) : serialize($closure),
74
        'method' => strtoupper($method),
75
            'middlewares' => [],
76
            'where' => [],
77
            'before' => [],
78
            'after' => [],
79
            'attributes' => [],
80
            'group' => self::getInstance()->getGroup(),
81
            'response' => null,
82
            'name' => $index
83
        ];
84
85
        self::getInstance()->setRoutes($routes);
86
            
87
        return self::getInstance();
88
    }
89
90
    private static function checkDuplicity(string $uri, string $method): void
91
    {
92
        foreach(self::getInstance()->getRoutes() as $route){
93
            if(md5(strtoupper(unserialize($route['uri'])->getPath().$route['method'])) === md5(strtoupper($uri.$method)) ) {
94
                throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured.");
95
            }
96
        }
97
    }
98
99
}
100