Passed
Branch master (783ca0)
by Henri
05:36 queued 04:17
created

Helper::currentRouteAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 import(string $path)
44
    {
45
        foreach (scandir($path) as $routeFile) {
46
            if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){
47
                require_once($path. DIRECTORY_SEPARATOR .$routeFile);
48
            }
49
        }
50
    }
51
52
    protected function ControllerForm($controller, string $method, array $values){
53
		$this->checkRole();
54
        $method = ($method !== 'method') ? $method : $this->getData()['POST']['role'];
55
        $data = (!is_null($values)) ? json_decode($values['data'], true) : null;
0 ignored issues
show
introduced by
The condition is_null($values) is always false.
Loading history...
56
57
        call_user_func_array([$controller,$method],  $data);
58
    }
59
60
    protected function Controller(string $controll): void
61
    {
62
        $data = $this->getData();
63
64
        foreach ($data['GET'] as $name => $value) {
65
            $controll = str_replace('{'.$name.'}',$value,$controll);
66
        }
67
68
        $this->checkControllsettable($controll);
69
70
        $this->checkControllexist($controll);
71
72
        $this->checkControllmethod($controll);
73
74
        $controller = ROUTER_CONFIG['controller.namespace'].'\\'. ucfirst(explode(':',$controll)[0]);
75
        $controller = new $controller();
76
        $method = explode(':',$controll)[1];
77
78
        if( ($this->getProtocol() == 'form') ){
79
            $this->ControllerForm($controller, $method, $data['POST']);
80
        }else {
81
            $data = (array_key_exists('data',$data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET'];
82
            call_user_func_array([$controller,$method],  $data);
83
        }
84
       
85
    }    
86
87
    protected function explodeRoute(bool $bar, string $url): array
88
    {   
89
        return explode( '/', $bar ? substr($url, 0, -1) : $url );
90
    }
91
92
    protected function toHiking($walking)
93
    {
94
        if(is_string($walking)){
95
            $this->Controller($walking);
96
            return true;
97
        }
98
99
        call_user_func_array($walking,$this->getData()['GET']);
100
    }
101
102
}
103