Passed
Push — master ( 21d927...ee3ef8 )
by Php Easy Api
03:01
created

RequestAnnotationManager::getRegex()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 7
nop 2
dl 0
loc 17
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Request;
4
5
use Resta\Support\ClosureDispatcher;
6
use Resta\Contracts\ApplicationContracts;
7
use Resta\Foundation\ApplicationProvider;
8
9
class RequestAnnotationManager extends RequestAnnotationAbstract
10
{
11
    /**
12
     * @var array $exceptionParams
13
     */
14
    protected $exceptionParams = [];
15
16
    /**
17
     * @var $annotation
0 ignored issues
show
Documentation Bug introduced by
The doc comment $annotation at position 0 could not be parsed: Unknown type name '$annotation' at position 0 in $annotation.
Loading history...
18
     */
19
    protected $annotation;
20
21
    /**
22
     * RequestAnnotationManager constructor.
23
     * @param ApplicationContracts $app
24
     * @param $request
25
     */
26
    public function __construct(ApplicationContracts $app,$request)
27
    {
28
        parent::__construct($app);
29
30
        $this->setReflection($request);
31
32
        $this->getInputs();
33
    }
34
35
    /**
36
     * checkt annotations
37
     *
38
     * @param $method
39
     * @param $key
40
     *
41
     * @throws \ReflectionException
42
     */
43
    public function annotation($method,$key)
44
    {
45
        //set annotation value with getting reflection
46
        $reflection = $this->getReflection('reflection')->reflectionMethodParams($method);
47
        $this->annotation = $reflection->document;
48
49
        //get remove from request object
50
        $this->getRemove();
51
52
        //get exception values from request object
53
        $this->getException();
54
55
        //get regex from request object
56
        $this->getRegex($method,$key);
57
    }
58
59
    /**
60
     * catch exception from regex method
61
     *
62
     * @param $key
63
     * @param $data
64
     */
65
    private function catchException($key,$data)
66
    {
67
        if(isset($this->exceptionParams[$key])){
68
            $keyParams = ($this->exceptionParams[$key]['params']) ?? [];
69
            exception($this->exceptionParams[$key]['name'],$keyParams)->unexpectedValue($key.' input value is not valid as format ('.$data.')');
70
        }
71
        else{
72
            exception()->unexpectedValue($key.' input value is not valid as format ('.$data.')');
73
        }
74
    }
75
76
    /**
77
     * get request exception from annotation
78
     *
79
     * @param $annotation
80
     */
81
    private function getException()
82
    {
83
        if(preg_match('@exception\((.*?)\)\r\n@is',$this->annotation,$exception)){
84
85
            $exceptionSpaceExplode = explode(" ",$exception[1]);
86
            foreach ($exceptionSpaceExplode as $exceptions){
87
                $exceptionsDotExplode = explode(":",$exceptions);
88
                $this->exceptionParams[$key][$exceptionsDotExplode[0]] = $exceptionsDotExplode[1];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key seems to be never defined.
Loading history...
89
            }
90
91
            if(isset($this->exceptionParams[$key]['params'])){
92
                $paramsCommaExplode = explode(",",$this->exceptionParams[$key]['params']);
93
                unset($this->exceptionParams[$key]['params']);
94
                foreach ($paramsCommaExplode as $params){
95
                    $paramsEqualExplode = explode("=",$params);
96
                    if(isset($paramsEqualExplode[0]) && isset($paramsEqualExplode[1])){
97
                        $this->exceptionParams[$key]['params'][$paramsEqualExplode[0]] = $paramsEqualExplode[1];
98
                    }
99
                }
100
            }
101
        }
102
    }
103
104
    /**
105
     * get regular expression from request object
106
     *
107
     * @param $method
108
     * @param $key
109
     */
110
    private function getRegex($method,$key)
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

110
    private function getRegex(/** @scrutinizer ignore-unused */ $method,$key)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
        if(preg_match('@regex\((.*?)\)\r\n@is',$this->annotation,$regex)){
113
            if(isset($this->inputs[$key])){
114
115
                if(is_array($this->inputs[$key])){
116
117
                    foreach ($this->inputs[$key] as $this->inputsKey=>$this->inputsValue){
118
                        if(!preg_match('@'.$regex[1].'@is',$this->inputsValue)){
119
                            $this->catchException($key,$regex[1]);
120
                        }
121
                    }
122
                }
123
                else{
124
125
                    if(!preg_match('@'.$regex[1].'@is',$this->inputs[$key])){
126
                        $this->catchException($key,$regex[1]);
127
                    }
128
                }
129
            }
130
        }
131
    }
132
133
    /**
134
     * get remove regex pattern with request object
135
     *
136
     * @return void|mixed
137
     */
138
    private function getRemove()
139
    {
140
        if(preg_match('@remove\((.*?)\)\r\n@is',$this->annotation,$remove)){
141
            if(isset($this->inputs[$key])){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key seems to be never defined.
Loading history...
142
                if(preg_match('@'.$remove[1].'@is',$this->inputs[$key])){
143
                    unset($this->inputs[$key]);
144
                }
145
            }
146
        }
147
    }
148
}