Completed
Pull Request — master (#348)
by
unknown
13:10
created

Validator::purgeNonRemoteRules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.8333
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Isrenato\JsValidation\Remote;
4
5
use Illuminate\Http\Exceptions\HttpResponseException;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Validation\ValidationException;
8
use Illuminate\Validation\ValidationRuleParser;
9
use Illuminate\Validation\Validator as BaseValidator;
10
use Isrenato\JsValidation\Support\AccessProtectedTrait;
11
use Isrenato\JsValidation\Support\RuleListTrait;
12
13
class Validator
14
{
15
    use AccessProtectedTrait;
16
    use RuleListTrait;
17
18
    /**
19
     * Validator extension name.
20
     */
21
    const EXTENSION_NAME = 'jsvalidation';
22
23
    /**
24
     * @var \Illuminate\Validation\Validator
25
     */
26
    protected $validator;
27
28
    /**
29
     * RemoteValidator constructor.
30
     *
31
     * @param \Illuminate\Validation\Validator $validator
32
     */
33 60
    public function __construct(BaseValidator $validator)
34
    {
35 60
        $this->validator = $validator;
36 60
    }
37
38
    /**
39
     * Validate request.
40
     *
41
     * @param $field
42
     * @param $parameters
43
     * @return void
44 60
     *
45
     * @throws \Illuminate\Validation\ValidationException
46 60
     */
47 60
    public function validate($field, $parameters = [])
48 60
    {
49
        $attribute = $this->parseAttributeName($field);
50 60
        $validationParams = $this->parseParameters($parameters);
51
        $validationResult = $this->validateJsRemoteRequest($attribute, $validationParams);
52
53
        $this->throwValidationException($validationResult, $this->validator);
54
    }
55
56
    /**
57
     * Throw the failed validation exception.
58
     *
59
     * @param mixed $result
60
     * @param \Illuminate\Validation\Validator  $validator
61
     * @return void
62 60
     *
63
     * @throws \Illuminate\Validation\ValidationException|\Illuminate\Http\Exceptions\HttpResponseException
64 60
     */
65
    protected function throwValidationException($result, $validator)
66 60
    {
67 10
        $response = new JsonResponse($result, 200);
68
69 50
        if ($result !== true && class_exists(ValidationException::class)) {
70
            throw new ValidationException($validator, $response);
71
        }
72
73
        throw new HttpResponseException($response);
74
    }
75
76
    /**
77
     *  Parse Validation input request data.
78 60
     *
79
     * @param $data
80 60
     * @return array
81 60
     */
82 60
    protected function parseAttributeName($data)
83
    {
84 60
        parse_str($data, $attrParts);
85
        $attrParts = is_null($attrParts) ? [] : $attrParts;
86
        $newAttr = array_keys(array_dot($attrParts));
87
88
        return array_pop($newAttr);
89
    }
90
91
    /**
92
     *  Parse Validation parameters.
93 60
     *
94
     * @param $parameters
95 60
     * @return array
96 60
     */
97 48
    protected function parseParameters($parameters)
98 32
    {
99
        $newParams = ['validate_all' => false];
100 60
        if (isset($parameters[0])) {
101
            $newParams['validate_all'] = ($parameters[0] === 'true') ? true : false;
102
        }
103
104
        return $newParams;
105
    }
106
107
    /**
108
     * Validate remote Javascript Validations.
109
     *
110 60
     * @param $attribute
111
     * @param array $parameters
112 60
     * @return array|bool
113
     */
114 60
    protected function validateJsRemoteRequest($attribute, $parameters)
115 60
    {
116 36
        $this->setRemoteValidation($attribute, $parameters['validate_all']);
117
118
        $validator = $this->validator;
119 24
        if ($validator->passes()) {
120
            return true;
121
        }
122
123
        return $validator->messages()->get($attribute);
124
    }
125
126
    /**
127
     * Sets data for validate remote rules.
128 60
     *
129
     * @param $attribute
130 60
     * @param bool $validateAll
131 60
     * @return void
132 60
     */
133 60
    protected function setRemoteValidation($attribute, $validateAll = false)
134 12
    {
135
        $validator = $this->validator;
136 12
        $rules = $validator->getRules();
137
        $rules = isset($rules[$attribute]) ? $rules[$attribute] : [];
138 48
        if (in_array('no_js_validation', $rules)) {
139 36
            $validator->setRules([$attribute => []]);
140 24
141 48
            return;
142 48
        }
143
        if (! $validateAll) {
144
            $rules = $this->purgeNonRemoteRules($rules, $validator);
145
        }
146
        $validator->setRules([$attribute => $rules]);
147
    }
148
149
    /**
150
     * Remove rules that should not be validated remotely.
151 36
     *
152
     * @param $rules
153 36
     * @param BaseValidator $validator
154
     * @return mixed
155 36
     */
156 36
    protected function purgeNonRemoteRules($rules, $validator)
157 36
    {
158 36
        $protectedValidator = $this->createProtectedCaller($validator);
0 ignored issues
show
Unused Code introduced by
$protectedValidator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159 24
160 24
        foreach ($rules as $i => $rule) {
161
            $parsedRule = ValidationRuleParser::parse([$rule]);
162 36
            if (! $this->isRemoteRule($parsedRule[0])) {
163
                unset($rules[$i]);
164
            }
165
        }
166
167
        return $rules;
168
    }
169
}
170