Passed
Push — master ( 24256b...8139c8 )
by Henri
06:50
created

CheckTrait::checkConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
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 checkNumparams(array $routeLoop, array $routeRequest)
28
    {
29
        return (count($routeLoop) !== count($routeRequest));
30
    }
31
32
    protected function checkParameters(array $routeLoop, array $routeRequest): bool
33
    {
34
        foreach($routeLoop as $rr => $param){
35
            if( (substr($param,0,1) === '{') ){
36
                $_GET[ substr($param,1,strlen($param)-2) ] = $routeRequest[$rr];    
37
            }
38
    
39
            if($this->checkParameter($param, $routeRequest[$rr])){
40
                return false;
41
            }
42
        }
43
        return true;
44
    }
45
46
    protected function checkParameter(string $routeLoop, string $routeRequest)
47
    {
48
        return !( substr($routeLoop,0,1) === '{' ) and $routeLoop !== $routeRequest;
49
    }
50
51
    protected function checkRole()
52
    {
53
        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

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