Passed
Push — master ( 6a9c31...8fd4e9 )
by Henri
01:16
created

ExtraJobsTrait::setOnRoute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 17
rs 10
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
trait ExtraJobsTrait{
6
7
    public function before($walking): Router
8
    {
9
        return $this->setOnRoute($walking,'before');
10
    }
11
12
    public static function beforeAll($walking, $except = null): Router
13
    {
14
        $excepts = is_array($except) ? $except : [$except];
15
        self::getInstance()->beforeExcepts = $excepts;
16
        self::getInstance()->beforeAll = $walking;
17
        return self::getInstance()->setOnRoutes($walking,'beforeAll',$excepts);
18
    }
19
20
    public function after($walking): Router
21
    {
22
        return $this->setOnRoute($walking,'after');
23
    }
24
25
    public static function afterAll($walking, $except = null): Router
26
    {
27
        $excepts = is_array($except) ? $except : [$except];
28
        self::getInstance()->afterExcepts = $excepts;
29
        self::getInstance()->afterAll = $walking;
30
        return self::getInstance()->setOnRoutes($walking,'afterAll',$excepts);
31
    }
32
33
    private function setOnRoute($walking, string $state): Router
34
    {
35
        if($this->lastReturn !== null){
36
            $currentGroup = end($this->routers)['group'];
37
38
            foreach ($this->routers as $key => $value) {
39
40
                if($value['group'] === $currentGroup){
41
                    $this->routers[$key][$state] = $walking;
0 ignored issues
show
Bug Best Practice introduced by
The property routers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
                }
43
44
            }
45
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\ExtraJobsTrait which includes types incompatible with the type-hinted return HnrAzevedo\Router\Router.
Loading history...
46
        }
47
        
48
        $this->routers[count($this->routers)-1][$state] = $walking;
49
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\ExtraJobsTrait which includes types incompatible with the type-hinted return HnrAzevedo\Router\Router.
Loading history...
50
    }
51
52
    private function setOnRoutes($walking, string $state, array $excepts): Router
53
    {
54
        foreach($this->routers as $r => $route){
55
            if(!in_array($this->routers[$r]['name'],$excepts)){
56
                $this->routers[$r][$state] = $walking;
0 ignored issues
show
Bug Best Practice introduced by
The property routers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57
            }
58
        }
59
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type HnrAzevedo\Router\ExtraJobsTrait which includes types incompatible with the type-hinted return HnrAzevedo\Router\Router.
Loading history...
60
    }
61
62
}
63