Passed
Push — master ( 817642...c23f74 )
by Henri
01:44
created

AttributeTrait::getPipeline()   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
declare(strict_types = 1);
4
5
namespace HnrAzevedo\Router;
6
7
use ReflectionObject, ReflectionMethod, Exception;
8
9
trait AttributeTrait
10
{
11
    use Helper, DefinitionsTrait;
12
13
    protected array $pipeline = [];
14
15
    protected function getPipeline(): array
16
    {
17
        return $this->pipeline;
18
    }
19
20
    protected function setPipeline(array $pipe): void
21
    {
22
        $this->pipeline = $pipe;
23
    }
24
25
    public static function pipeline(array $pline): void
26
    {
27
        self::getInstance()->setPipeline($pline);
28
    }
29
30
    public static function loadPipeline(): void
31
    {
32
        foreach(self::getInstance()->getPipeline() as $line){
33
            self::getInstance()->loadLine(new ReflectionObject(new $line()));
34
        }
35
    }
36
37
    private function loadLine(ReflectionObject $reflection): void
38
    {
39
        foreach($reflection->getMethods() as $method){
40
            $this->loadMethod($method);
41
        }
42
    }
43
44
    private function loadMethod(ReflectionMethod $method): void
45
    {
46
        try{
47
            foreach ($method->getAttributes() as $attr) {
48
                if($attr->getName() != 'HnrAzevedo\Router\RouteAttribute') continue;
49
50
                $args = $attr->getArguments();
51
    
52
                $this->checkArgs($attr->getArguments());
53
                
54
                self::set(
55
                    strtolower(implode('|', $args['methods'])),
56
                    $args['uri'],
57
                    $method->class.'@'.$method->name
58
                );
59
                
60
                $this->attrName($args)
61
                    ->attrBefore($args)
62
                    ->attrAfter($args)
63
                    ->attrAttributes($args)
64
                    ->attrWhere($args)
65
                    ->attrMiddleware($args);
66
            }
67
        }catch(Exception $er){
68
            throw new Exception('Failed to add route via attribute: '.$er->getMessage());
69
        }
70
    }
71
72
    private function checkArgs(array $args): self
73
    {
74
        if(!array_key_exists('uri', $args) || !array_key_exists('methods', $args)) {
75
            throw new Exception('Misconfigured route attribute');
76
        }
77
        return $this;
78
    }
79
80
    private function attrName(array $attr): self
81
    {
82
        if(array_key_exists('name', $attr)) {
83
            $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

83
            $this->/** @scrutinizer ignore-call */ 
84
                   name($attr['name']);
Loading history...
84
        }
85
        return $this;
86
    }
87
88
    private function attrBefore(array $attr): self
89
    {
90
        if(array_key_exists('before', $attr)) {
91
            $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

91
            $this->/** @scrutinizer ignore-call */ 
92
                   before($attr['before']);
Loading history...
92
        }
93
        return $this;
94
    }
95
96
    private function attrAfter(array $attr): self
97
    {
98
        if(array_key_exists('after', $attr)) {
99
            $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

99
            $this->/** @scrutinizer ignore-call */ 
100
                   after($attr['after']);
Loading history...
100
        }
101
        return $this;
102
    }
103
104
    private function attrAttributes(array $attr): self
105
    {
106
        if(array_key_exists('attributes', $attr)) {
107
            foreach($attr['attributes'] as $attribute){
108
                $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

108
                $this->/** @scrutinizer ignore-call */ 
109
                       attribute($attribute[0], $attribute[1]);
Loading history...
109
            }
110
        }
111
        return $this;
112
    }
113
114
    private function attrWhere(array $attr): self
115
    {
116
        if(array_key_exists('where', $attr)) {
117
            $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

117
            $this->/** @scrutinizer ignore-call */ 
118
                   where($attr['where']);
Loading history...
118
        }
119
        return $this;
120
    }
121
122
    private function attrMiddleware(array $attr): self
123
    {
124
        if(array_key_exists('middleware', $attr)) {
125
            $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

125
            $this->/** @scrutinizer ignore-call */ 
126
                   middleware($attr['middleware']);
Loading history...
126
        }
127
        return $this;
128
    }
129
130
}
131