ApiRequest::isValueSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 1
c 1
b 1
f 0
nc 2
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Napp\Core\Api\Requests;
4
5
use Illuminate\Contracts\Validation\Validator;
6
use Illuminate\Foundation\Http\FormRequest;
7
use Illuminate\Support\Str;
8
use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException;
9
use Napp\Core\Api\Exceptions\Exceptions\InvalidFieldException;
10
use Napp\Core\Api\Exceptions\Exceptions\ValidationException;
11
use Napp\Core\Api\Transformers\ApiTransformer;
12
use Napp\Core\Api\Transformers\TransformerInterface;
13
14
/**
15
 * Class ApiRequest.
16
 */
17
abstract class ApiRequest extends FormRequest
18
{
19
    /**
20
     * @throws \Napp\Core\Api\Exceptions\Exceptions\InvalidFieldException
21
     *
22
     * @return Validator
23
     */
24 10
    protected function getValidatorInstance()
25
    {
26 10
        $this->replace($this->transformInput());
27 10
        $this->validateInputFields();
28
29 8
        return parent::getValidatorInstance();
30
    }
31
32
    /**
33
     * @param Validator $validator
34
     *
35
     * @throws \Illuminate\Validation\ValidationException
36
     * @throws \Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException
37
     * @throws \Napp\Core\Api\Exceptions\Exceptions\ValidationException
38
     *
39
     * @return void
40
     */
41 8
    protected function failedValidation(Validator $validator)
42
    {
43 8
        if (false === $this->isApiInternalCall()) {
44 8
            $this->handleApiCallFailedValidation($validator);
45
        } else {
46
            $this->handleApiInternalCallFailedValidation($validator);
47
        }
48
    }
49
50
    /**
51
     * @throws InvalidFieldException
52
     *
53
     * @return void
54
     */
55 10
    protected function validateInputFields(): void
56
    {
57 10
        $input = $this->input();
58 10
        $rules = $this->rules();
0 ignored issues
show
Bug introduced by
The method rules() does not exist on Napp\Core\Api\Requests\ApiRequest. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

58
        /** @scrutinizer ignore-call */ 
59
        $rules = $this->rules();
Loading history...
59 10
        if (false === empty(array_diff_key($input, $rules))) {
60 2
            $exception = new InvalidFieldException();
61 2
            $exception->statusMessage = $exception->statusMessage . ': ' . implode(',', array_keys(array_diff_key($input, $rules)));
62
63 2
            throw $exception;
64
        }
65 8
    }
66
67
    /**
68
     * @return array
69
     */
70 10
    protected function transformInput(): array
71
    {
72
        /**
73
         * Remove input fields like _method, _token, etc.
74
         */
75
        $input = array_filter($this->input(), function ($key) {
76 2
            return ! Str::startsWith($key, '_');
77 10
        }, ARRAY_FILTER_USE_KEY);
78
79 10
        return $this->getTransformer()->transformInput($input);
80
    }
81
82
    /**
83
     * @return TransformerInterface
84
     */
85
    protected function getTransformer(): TransformerInterface
86
    {
87
        return app(ApiTransformer::class);
88
    }
89
90
    /**
91
     * @see AppServiceProvider
92
     *
93
     * @return bool
94
     */
95 8
    protected function isApiInternalCall(): bool
96
    {
97 8
        $request = request();
98 8
        if (true === $request->hasMacro('isApiInternalCall')) {
99 8
            return $request->isApiInternalCall();
100
        }
101
102
        return false;
103
    }
104
105
    /**
106
     * @param Validator $validator
107
     *
108
     * @throws ValidationException
109
     * @throws \Illuminate\Validation\ValidationException
110
     *
111
     * @return void
112
     */
113 8
    protected function handleApiCallFailedValidation(Validator $validator)
114
    {
115 8
        $message = $validator->messages()->first();
116 8
        $exception = new ValidationException();
117 8
        $exception->statusMessage = $exception->statusMessage . ': ' . $message;
118 8
        $exception->validation = $this->transformValidationOutput($validator);
119
120 8
        throw $exception;
121
    }
122
123
    /**
124
     * @param Validator $validator
125
     *
126
     * @throws ApiInternalCallValidationException
127
     *
128
     * @return void
129
     */
130
    protected function handleApiInternalCallFailedValidation(Validator $validator): void
131
    {
132
        $input = $this->getTransformer()->transformOutput($this->except($this->dontFlash));
133
        $errors = $this->transformValidationOutput($validator);
134
135
        throw new ApiInternalCallValidationException($input, $errors);
136
    }
137
138
    /**
139
     * @param array  $input
140
     * @param string $key
141
     *
142
     * @return bool
143
     */
144
    protected function isValueSet(array $input, string $key): bool
145
    {
146
        return true === isset($input[$key]) && false === empty($input[$key]);
147
    }
148
149
    /**
150
     * @param Validator $validator
151
     *
152
     * @return array
153
     */
154 8
    protected function transformValidationOutput($validator): array
155
    {
156 8
        return collect($this->getTransformer()->transformOutputKeys($validator->getMessageBag()->toArray()))
157
            ->reject(function ($error) {
158 8
                return false === \is_array($error);
159 8
            })
160 8
            ->toArray();
161
    }
162
}
163