Passed
Push — master ( 18e951...67611c )
by Henri
01:38
created

AttributeTrait::setPipeline()   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 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace HnrAzevedo\Router;
6
7
use ReflectionObject, ReflectionMethod, Exception;
8
9
trait AttributeTrait
10
{
11
    use Helper;
12
13
    protected array $pipeline = [];
14
15
    public static function pipeline(array $pline): void
16
    {
17
        self::getInstance()->pipeline = $pline;
0 ignored issues
show
Bug introduced by
The property pipeline is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
18
    }
19
20
    public static function loadPipeline(): void
21
    {
22
        foreach(self::getInstance()->pipeline as $line){
0 ignored issues
show
Bug introduced by
The property pipeline is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
23
            self::getInstance()->loadLine(new ReflectionObject(new $line()));
24
        }
25
    }
26
27
    private function loadLine(ReflectionObject $reflection): void
28
    {
29
        foreach($reflection->getMethods() as $method){
30
            $this->loadMethod($method);
31
        }
32
    }
33
34
    private function loadMethod(ReflectionMethod $method): void
35
    {
36
        try{
37
            foreach ($method->getAttributes() as $attr) {
38
                if($attr->getName() != 'HnrAzevedo\Router\RouteAttribute') continue;
39
40
                $args = $attr->getArguments();
41
    
42
                $this->checkArgs($attr->getArguments());
43
                
44
                self::set(
45
                    strtolower(implode('|',$args['methods'])),
46
                    $args['uri'],
47
                    $method->class.'@'.$method->name
48
                );
49
                
50
                $this->attrName($args)
51
                    ->attrBefore($args)
52
                    ->attrAfter($args)
53
                    ->attrAttributes($args)
54
                    ->attrWhere($args)
55
                    ->attrMiddleware($args);
56
            }
57
        }catch(Exception $er){
58
            throw new Exception('Failed to add route via attribute: '.$er->getMessage());
59
        }
60
    }
61
62
    private function checkArgs(array $args): self
63
    {
64
        if(!array_key_exists('uri',$args) || !array_key_exists('methods',$args)){
65
            throw new Exception('Misconfigured route attribute');
66
        }
67
        return $this;
68
    }
69
70
    private function attrName(array $attr): self
71
    {
72
        if(array_key_exists('name',$attr)){
73
            $this->name($attr['name']);
0 ignored issues
show
Bug introduced by
It seems like name() 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

73
            $this->/** @scrutinizer ignore-call */ 
74
                   name($attr['name']);
Loading history...
74
        }
75
        return $this;
76
    }
77
78
    private function attrBefore(array $attr): self
79
    {
80
        if(array_key_exists('before',$attr)){
81
            $this->before($attr['before']);
0 ignored issues
show
Bug introduced by
It seems like before() 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

81
            $this->/** @scrutinizer ignore-call */ 
82
                   before($attr['before']);
Loading history...
82
        }
83
        return $this;
84
    }
85
86
    private function attrAfter(array $attr): self
87
    {
88
        if(array_key_exists('after',$attr)){
89
            $this->after($attr['after']);
0 ignored issues
show
Bug introduced by
It seems like after() 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

89
            $this->/** @scrutinizer ignore-call */ 
90
                   after($attr['after']);
Loading history...
90
        }
91
        return $this;
92
    }
93
94
    private function attrAttributes(array $attr): self
95
    {
96
        if(array_key_exists('attributes',$attr)){
97
            foreach($attr['attributes'] as $attribute){
98
                $this->attribute($attribute[0], $attribute[1]);
0 ignored issues
show
Bug introduced by
It seems like attribute() 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

98
                $this->/** @scrutinizer ignore-call */ 
99
                       attribute($attribute[0], $attribute[1]);
Loading history...
99
            }
100
        }
101
        return $this;
102
    }
103
104
    private function attrWhere(array $attr): self
105
    {
106
        if(array_key_exists('where',$attr)){
107
            $this->where($attr['where']);
0 ignored issues
show
Bug introduced by
It seems like where() 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

107
            $this->/** @scrutinizer ignore-call */ 
108
                   where($attr['where']);
Loading history...
108
        }
109
        return $this;
110
    }
111
112
    private function attrMiddleware(array $attr): self
113
    {
114
        if(array_key_exists('middleware',$attr)){
115
            $this->middleware($attr['middleware']);
0 ignored issues
show
Bug introduced by
It seems like middleware() 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

115
            $this->/** @scrutinizer ignore-call */ 
116
                   middleware($attr['middleware']);
Loading history...
116
        }
117
        return $this;
118
    }
119
120
}