Passed
Branch v2-dev (0ddf7c)
by Henri
10:02
created

RunInTrait::addInRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
trait RunInTrait
6
{
7
    use Helper, CheckTrait;
8
9
    protected array $beforeExcepts = [];
10
    protected array $afterExcepts = [];
11
12
    public static function before($closure): RouterInterface
13
    {
14
        return self::addInRoute('before',$closure);
15
    }
16
17
    public static function after($closure): RouterInterface
18
    {
19
        return self::addInRoute('after',$closure);
20
    }
21
22
    public static function beforeAll($closure, $excepts): RouterInterface
23
    {
24
        self::getInstance()->beforeExcepts = (is_array($excepts)) ? $excepts : [ $excepts ];
0 ignored issues
show
Bug introduced by
The property beforeExcepts is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
25
        self::getInstance()->beforeAll = $closure;
0 ignored issues
show
Bug introduced by
The property beforeAll is declared private in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
26
        return self::getInstance();
27
    }
28
29
    public static function afterAll($closure, $excepts): RouterInterface
30
    {
31
        self::getInstance()->afterExcepts = (is_array($excepts)) ? $excepts : [ $excepts ];
0 ignored issues
show
Bug introduced by
The property afterExcepts is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
32
        self::getInstance()->afterAll = $closure;
0 ignored issues
show
Bug introduced by
The property afterAll is declared private in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
33
        return self::getInstance();
34
    }
35
36
    public static function beforeGroup($closure, $excepts): RouterInterface
37
    {
38
        return self::addInRoutes('before', $closure, $excepts);
39
    }
40
41
    public static function afterGroup($closure, $excepts): RouterInterface
42
    {
43
        return self::addInRoutes('after', $closure, $excepts);
44
    }
45
46
    private static function addInRoutes(string $state, $closure, $excepts): RouterInterface
47
    {
48
        self::getInstance()->isInPseudGroup();
49
        $excepts = (is_array($excepts)) ? $excepts : [ $excepts ];
50
        $group = self::getInstance()->inSave()['group'];
51
52
        foreach(self::getInstance()->routes as $r => $route){
53
            if($route['group'] === $group && !in_array($r,$excepts)){
54
                self::getInstance()->routes[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); 
0 ignored issues
show
Bug introduced by
The property routes is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
55
            }
56
        }
57
58
        return self::getInstance();
59
    }
60
61
    private static function addInRoute(string $state, $closure): RouterInterface
62
    {
63
        $route = self::getInstance()->inSave();
64
        $state = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]);
65
        $route[$state] = $state;
66
        self::updateRoute($route,array_key_last(self::getInstance()->routes));
0 ignored issues
show
Bug introduced by
The property routes is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
67
        return self::getInstance();
68
    }
69
70
}
71