Passed
Branch master (6a9c31)
by Henri
01:14
created

CheckTrait::hasRouteName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
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){
16
        if(!array_key_exists($routeName,$this->routers)){
17
            throw new Exception('Page not found.', 404);
18
        }
19
    }
20
21
    protected function checkTypeRole($role){
22
        if(!is_string($role) && @get_class($role) !== 'Closure' ){
23
            throw new Exception("Improperly defined route track.");
24
        }
25
    }
26
27
    protected function checkConfig()
28
    {
29
        if(!defined('ROUTER_CONFIG')){
30
            throw new Exception("Information for loading routes has not been defined.");
31
        }
32
    }
33
34
    protected function checkNumparams(array $routeLoop, array $routeRequest)
35
    {
36
        return (count($routeLoop) !== count($routeRequest));
37
    }
38
39
    protected function checkParameters(array $routeLoop, array $routeRequest): bool
40
    {
41
        foreach($routeLoop as $rr => $param){
42
            if( (substr($param,0,1) === '{') ){
43
                $_GET[ substr($param,1,strlen($param)-2) ] = $routeRequest[$rr];    
44
            }
45
    
46
            if($this->checkParameter($param, $routeRequest[$rr])){
47
                return false;
48
            }
49
        }
50
        return true;
51
    }
52
53
    protected function checkParameter(string $routeLoop, string $routeRequest)
54
    {
55
        return !( substr($routeLoop,0,1) === '{' ) and $routeLoop !== $routeRequest;
56
    }
57
58
    protected function checkRole()
59
    {
60
        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

60
        if(!array_key_exists('role', $this->/** @scrutinizer ignore-call */ getData()['POST'])){
Loading history...
61
            throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
62
        }
63
    }
64
65
    protected function hasProtocol(array $route, string $currentProtocol)
66
    {
67
        $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ];
68
69
        foreach($protocols as $protocol){
70
            if(strtoupper($protocol) !== strtoupper($currentProtocol)){
71
                continue;
72
            }
73
        }
74
    }
75
76
    protected function checkToHiking($route, $routeRequest, $routeLoop): bool
77
    {
78
        if($this->checkNumparams($routeLoop, $routeRequest) || 
79
            !$this->checkParameters($routeLoop, $routeRequest) ||
80
            !$this->checkWhere($route, $routeRequest)){
81
                return false;
82
        }
83
        return true;
84
    }
85
86
    protected function hasRouteName(string $name): void
87
    {
88
        if(array_key_exists($name, $this->routers)){
89
            throw new Exception("There is already a route with the name {$name} configured.");
90
        }
91
    }
92
93
}
94