Completed
Push — master ( e62f49...fb878c )
by Babak
03:24
created

RequestHelper::setModel()   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
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alive
5
 * Date: 10/7/17
6
 * Time: 12:19 PM
7
 */
8
9
namespace Alive2212\LaravelRequestHelper;
10
11
12
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Illuminate\Support\Facades\Validator;
14
use Illuminate\Support\MessageBag;
15
16
class RequestHelper
17
{
18
    //TODO some method  of this class (like uniqueCheck) must be return $this
19
    /**
20
     * @var array
21
     */
22
    protected $validatorMethods = ["POST", "PUT", "PATCH"];
23
24
    /**
25
     * @var array
26
     */
27
    protected $uniqueFields = [];
28
29
    /**
30
     * @var null
31
     */
32
    protected $model = null;
33
34
    /**
35
     * @param Request $request
36
     * @return array
37
     */
38
    public function getJsonPaginateValues(Request $request)
39
    {
40
        $request = $request->toArray();
41
        $size = 10;
42
        $number = 1;
43
        if (array_key_exists('page', $request)) {
44
            if (array_key_exists('size', $request['page'])) {
45
                $size = $request['page']['size'];
46
            }
47
            if (array_key_exists('number', $request['page'])) {
48
                $number = $request['page']['number'];
49
            }
50
        }
51
        return array($size, $number);
52
    }
53
54
    /**
55
     * @param $param
56
     * @return bool
57
     */
58
    public function isJson($param)
59
    {
60
        return collect(json_decode($param, true))->count() == 0 ? false : true;
61
    }
62
63
    /**
64
     * @param $param
65
     * @return mixed|string
66
     */
67
    public function getOperatorFromJson($param)
68
    {
69
        if (collect(json_decode($param, true))->get('operator') == null) {
70
            return '=';
71
        }
72
        return collect(json_decode($param, true))->get('operator');
73
    }
74
75
    /**
76
     * @param $param
77
     * @return mixed
78
     */
79
    public function getValueFromJson($param)
80
    {
81
        if (collect(json_decode($param, true))->get('value') == null) {
82
            return json_decode($param, true);
83
        }
84
        return collect(json_decode($param, true))->get('value');
85
    }
86
87
    /**
88
     * @param $param
89
     * @return \Illuminate\Support\Collection
90
     */
91
    public function getCollectFromJson($param)
92
    {
93
        return collect(json_decode($param, true));
94
    }
95
96
    /**
97
     * @param Request $request
98
     * @param $validatorArray
99
     * @param string $message
100
     * @return \Illuminate\Http\JsonResponse|MessageBag|null
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
101
     */
102
    public function validator(Request $request, $validatorArray, $message = 'validation_fails')
0 ignored issues
show
Unused Code introduced by
The parameter $message 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

102
    public function validator(Request $request, $validatorArray, /** @scrutinizer ignore-unused */ $message = 'validation_fails')

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...
103
    {
104
        $validationErrors = $this->checkRequestValidation($request, $validatorArray);
105
        if ($validationErrors != null) {
106
            return $validationErrors;
107
        }
108
        return $validationErrors;
109
    }
110
111
    /**
112
     * @param Request $request
113
     * @param $validationArray
114
     * @return MessageBag|null
115
     */
116
    public function checkRequestValidation(Request $request, $validationArray)
117
    {
118
        $requestParams = $request->toArray();
119
        $validator = Validator::make($request->all(), $validationArray);
120
        if ($validator->fails()) {
121
            return $validator->errors();
122
        }
123
        if (is_numeric(array_search($request->getMethod(), $this->validatorMethods))) {
124
            $errors = new MessageBag();
125
            foreach ($requestParams as $requestParamKey => $requestParamValue) {
126
                if (is_numeric(array_search($requestParamKey, $this->uniqueFields))) {
127
                    if ($this->checkExistUniqueRecord($requestParamKey, $requestParamValue)) {
128
                        $errors->add($requestParamKey, 'This ' . $requestParamKey . ' is exist try another.');
129
                    }
130
                }
131
            }
132
            if (collect($errors)->count() > 0) {
133
                return $errors;
134
            }
135
        }
136
        return null;
137
    }
138
139
    /**
140
     * @param $key
141
     * @param $value
142
     * @return bool
143
     */
144
    public function checkExistUniqueRecord($key, $value)
145
    {
146
        if (is_null($this->model)) {
147
            if ($this->model->where($key, $value)->count()) {
0 ignored issues
show
Bug introduced by
The method where() does not exist on null. ( Ignorable by Annotation )

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

147
            if ($this->model->/** @scrutinizer ignore-call */ where($key, $value)->count()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
                return true;
149
            }
150
        }
151
        return false;
152
    }
153
154
    /**
155
     * @return array
156
     */
157
    public function getUniqueFields()
158
    {
159
        return $this->uniqueFields;
160
    }
161
162
    /**
163
     * @param array $uniqueFields
164
     */
165
    public function setUniqueFields($uniqueFields)
166
    {
167
        $this->uniqueFields = $uniqueFields;
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    public function getValidatorMethods()
174
    {
175
        return $this->validatorMethods;
176
    }
177
178
    /**
179
     * @param array $validatorMethods
180
     */
181
    public function setValidatorMethods($validatorMethods)
182
    {
183
        $this->validatorMethods = $validatorMethods;
184
    }
185
186
    /**
187
     * @return mixed
188
     */
189
    public function getModel()
190
    {
191
        return $this->model;
192
    }
193
194
    /**
195
     * @param mixed $model
196
     */
197
    public function setModel($model)
198
    {
199
        $this->model = $model;
200
    }
201
202
}