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

DefinitionsTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 28
c 2
b 0
f 1
dl 0
loc 73
rs 10
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A any() 0 3 1
A patch() 0 3 1
A put() 0 3 1
A ajax() 0 3 1
A post() 0 3 1
A delete() 0 3 1
A match() 0 4 1
A checkDuplicity() 0 5 3
A set() 0 19 3
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
use HnrAzevedo\Http\Uri;
6
7
trait DefinitionsTrait
8
{
9
    use Helper;
10
    
11
    protected array $routes = [];
12
    
13
    public static function get(string $uri, $closure): RouterInterface
14
    {
15
        return self::set('get',$uri,$closure);
16
    }
17
18
    public static function post(string $uri, $closure): RouterInterface
19
    {
20
        return self::set('post',$uri,$closure);
21
    }
22
23
    public static function ajax(string $uri, $closure): RouterInterface
24
    {
25
        return self::set('ajax',$uri,$closure);
26
    }
27
28
    public static function delete(string $uri, $closure): RouterInterface
29
    {
30
        return self::set('delete',$uri,$closure);
31
    }
32
33
    public static function put(string $uri, $closure): RouterInterface
34
    {
35
        return self::set('put',$uri,$closure);
36
    }
37
38
    public static function patch(string $uri, $closure): RouterInterface
39
    {
40
        return self::set('patch',$uri,$closure);
41
    }
42
43
    public static function match(string $method, string $uri, $closure): RouterInterface
44
    {
45
        self::set($method, $uri, $closure);
46
        return self::getInstance();
47
    }
48
49
    public static function any(string $uri, $closure): RouterInterface
50
    {
51
        return self::set('*',$uri,$closure);
52
    }
53
54
    private static function set(string $method, string $uri, $closure): RouterInterface
55
    {   
56
        $uri = (substr($uri,0,1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri;
57
        
58
        self::checkDuplicity($uri,$method);
59
        
60
		self::getInstance()->routes[] = [
0 ignored issues
show
Bug introduced by
The property routes is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
61
			'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri),
0 ignored issues
show
Bug introduced by
The property prefix is declared private in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
Bug introduced by
The property host is declared private in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
62
			'action' => $closure,
63
			'method' => strtoupper($method),
64
            'middlewares' => null,
65
            'where' => null,
66
            'before' => null,
67
            'after' => null,
68
            'group' => self::getInstance()->group,
0 ignored issues
show
Bug introduced by
The property group is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
69
            'response' => null
70
        ];
71
        	
72
        return self::getInstance();
73
    }
74
75
    private static function checkDuplicity(string $uri, string $method): void
76
    {
77
        foreach(self::getInstance()->routes as $route){
0 ignored issues
show
Bug introduced by
The property routes is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
78
    		if( md5($route['uri'].$route['method']) === md5($uri.$method) ){
79
                throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured.");
80
            }
81
        }
82
    }
83
84
}
85