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

Helper::import()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
trait Helper{
6
    use CheckTrait, ControllerTrait;
7
    
8
    private $currentRoute = null;
9
10
    public static function current(): ?array
11
    {
12
        return self::getInstance()->currentRoute;
13
    }
14
15
    public static function currentRouteName(): ?string
16
    {
17
        return self::getInstance()->currentRoute['name'];
18
    }
19
20
    public static function currentRouteAction()
21
    {
22
        return self::getInstance()->currentRoute['role'];
23
    }
24
    
25
    protected function getProtocol(): string
26
    {
27
        $protocol = ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? 'ajax' : 'get';
28
        $protocol = (array_key_existS('HTTP_REQUESTED_METHOD',$_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol;
29
            
30
        return $protocol;
31
    }
32
33
    protected function getData(): ?array
34
    {
35
        return [
36
            'POST' => $_POST,
37
            'GET' => $_GET,
38
            'FILES' => $_FILES,
39
            'PROTOCOL' => $this->getProtocol()
40
        ];
41
    }
42
43
    protected function ControllerForm($controller, string $method, array $values){
44
		$this->checkRole();
45
        $method = ($method !== 'method') ? $method : $this->getData()['POST']['role'];
46
        $data = (array_key_exists('data',$values)) ? json_decode($values['data'], true) : null;
47
48
        call_user_func_array([$controller,$method],  $data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type null; however, parameter $param_arr of call_user_func_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

48
        call_user_func_array([$controller,$method],  /** @scrutinizer ignore-type */ $data);
Loading history...
49
    }
50
51
    protected function Controller(string $controll): void
52
    {
53
        $data = $this->getData();
54
55
        foreach ($data['GET'] as $name => $value) {
56
            $controll = str_replace('{'.$name.'}',$value,$controll);
57
        }
58
59
        $this->checkControllsettable($controll);
60
61
        $this->checkControllexist($controll);
62
63
        $this->checkControllmethod($controll);
64
65
        $controller = ucfirst(explode(':',$controll)[0]);
66
        $controller = new $controller();
67
        $method = explode(':',$controll)[1];
68
69
        if( ($this->getProtocol() == 'form') ){
70
            $this->ControllerForm($controller, $method, $data['POST']);
71
        }else {
72
            $data = (array_key_exists('data',$data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET'];
73
            call_user_func_array([$controller,$method],  $data);
74
        }
75
       
76
    }    
77
78
    protected function explodeRoutes(bool $bar, string $url ,bool $bar_, string $url_): array
79
    {   
80
        $url = $bar ? substr($url, 0, -1) : $url ;
81
        $url = explode('/',$url);
82
83
        $url_ = $bar_ ? substr($url_, 0, -1) : $url_ ;
84
        $url_ = explode('/',$url_);
85
86
        foreach($url as $ur => $u){
87
            if(substr($u,0,2) === '{?'){
88
                if(!array_key_exists($ur,$url_)){
89
                    $url_[$ur] = '';
90
                };
91
            }
92
        }
93
94
        return ['routeLoop' => $url, 'routeRequest' => $url_];
95
    }
96
97
    protected function toHiking(array $route)
98
    {
99
        $this->callOnRoute($route,'beforeAll');
100
        $this->callOnRoute($route,'before');
101
102
        if(is_string($route['role'])){
103
            $this->Controller($route['role']);
104
            $this->callOnRoute($route,'after');
105
            $this->callOnRoute($route,'afterAll');
106
            return true;
107
        }
108
109
        call_user_func_array($route['role'],$this->getData()['GET']);
110
111
        $this->callOnRoute($route,'after');
112
        $this->callOnRoute($route,'afterAll');
113
    }
114
115
    protected function callOnRoute(array $route,string $state)
116
    {
117
        if($route[$state] !== null){
118
            if(is_string($route[$state])){
119
                $this->Controller($route[$state]);
120
            }else{
121
                $route[$state]();
122
            }
123
        }
124
    }
125
126
}
127