Passed
Push — master ( e0eeef...ff46e0 )
by Henri
01:44
created

CheckTrait::checkInGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
use Exception;
6
7
trait CheckTrait{
8
    use FilterTrait, CheckWhere;
9
10
    protected function checkProtocol(string $expected, string $current): bool
11
    {
12
        return (strtoupper($expected) === strtoupper($current));
13
    }
14
15
    protected function checkName(string $routeName): parent
16
    {
17
        if(!array_key_exists($routeName,$this->routers)){
18
            throw new Exception('Page not found.', 404);
19
        }
20
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\CheckTrait which is incompatible with the type-hinted return parent.
Loading history...
21
    }
22
23
    protected function checkTypeRole($role): parent
24
    {
25
        if(!is_string($role) && @get_class($role) !== 'Closure' ){
26
            throw new Exception("Improperly defined route track.");
27
        }
28
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\CheckTrait which is incompatible with the type-hinted return parent.
Loading history...
29
    }
30
31
    protected function checkNumparams(array $routeLoop, array $routeRequest): bool
32
    {
33
        return (count($routeLoop) !== count($routeRequest));
34
    }
35
36
    protected function checkParameters(array $routeLoop, array $routeRequest): bool
37
    {
38
        foreach($routeLoop as $rr => $param){
39
            if( (substr($param,0,1) === '{') ){
40
                $_GET[ substr($param,1,strlen($param)-2) ] = $routeRequest[$rr];    
41
            }
42
    
43
            if($this->checkParameter($param, $routeRequest[$rr])){
44
                return false;
45
            }
46
        }
47
48
        return true;
49
    }
50
51
    protected function checkParameter(string $routeLoop, string $routeRequest): bool
52
    {
53
        return !( substr($routeLoop,0,1) === '{' ) and $routeLoop !== $routeRequest;
54
    }
55
56
    protected function checkRole(): parent
57
    {
58
        if(!array_key_exists('role', $this->getData()['POST'])){
0 ignored issues
show
Bug introduced by
It seems like getData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        if(!array_key_exists('role', $this->/** @scrutinizer ignore-call */ getData()['POST'])){
Loading history...
59
            throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
60
        }
61
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\CheckTrait which is incompatible with the type-hinted return parent.
Loading history...
62
    }
63
64
    protected function hasProtocol(array $route, string $currentProtocol): parent
65
    {
66
        $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ];
67
68
        foreach($protocols as $protocol){
69
            if(strtoupper($protocol) !== strtoupper($currentProtocol)){
70
                continue;
71
            }
72
        }
73
74
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\CheckTrait which is incompatible with the type-hinted return parent.
Loading history...
75
    }
76
77
    protected function checkToHiking($route, $routeRequest, $routeLoop): bool
78
    {
79
        if($this->checkNumparams($routeLoop, $routeRequest) || 
80
            !$this->checkParameters($routeLoop, $routeRequest) ||
81
            !$this->checkWhere($route, $routeRequest)){
82
                return false;
83
        }
84
        return true;
85
    }
86
87
    protected function hasRouteName(string $name): parent
88
    {
89
        if(array_key_exists($name, $this->routers)){
90
            throw new Exception("There is already a route with the name {$name} configured.");
91
        }
92
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\CheckTrait which is incompatible with the type-hinted return parent.
Loading history...
93
    }
94
95
    protected function checkExistence(string $url, string $protocol): parent
96
    {
97
        foreach($this->routers as $key => $value){
98
    		if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
99
                throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured.");
100
            }
101
        }
102
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\CheckTrait which is incompatible with the type-hinted return parent.
Loading history...
103
    }
104
105
    protected function checkInGroup(): parent
106
    {
107
        if($this->lastReturn){
108
            throw new Exception("At the moment it is not allowed to assign names or tests of parameters in groups..");
109
        }
110
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\CheckTrait which is incompatible with the type-hinted return parent.
Loading history...
111
    }
112
113
}
114