Test Failed
Pull Request — master (#49)
by Giacomo
16:35
created

prepareRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 2
eloc 7
c 3
b 1
f 1
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
use Illuminate\Support\Facades\Config;
5
use Jenssegers\Mongodb\Eloquent\Model;
6
7
if (! function_exists('getTranslatedContent')) {
8
9
    /**
10
     * @param array $mlCollection
11
     *
12
     * @return string
13
     */
14
    function getTranslatedContent($mlCollection)
15
    {
16
        //Get current Lang
17
        $cl = Config::get('app.locale');
18
19
        if (is_array($mlCollection) && (array_key_exists('en_EN', $mlCollection) || array_key_exists('it_IT', $mlCollection) || ! is_null($mlCollection || ! isset($destination)))) {
0 ignored issues
show
introduced by
The condition is_null($mlCollection || ! IssetNode) is always false.
Loading history...
Comprehensibility Best Practice introduced by
The variable $destination seems to never exist and therefore isset should always be false.
Loading history...
Bug Best Practice introduced by
The expression $mlCollection of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
20
            return $mlCollection[$cl];
21
        } else {
22
            return '';
23
        }
24
    }
25
}
26
27
if (! function_exists('cl')) {
28
29
    /**
30
     * @return string current Lang
31
     */
32
    function cl()
33
    {
34
        //Get current Lang
35
        return Config::get('app.locale');
36
    }
37
}
38
39
if (! function_exists('ml')) {
40
    //save a localized field
41
    /**
42
     * @param array $destination
43
     * @param string $input
44
     *
45
     * @return array ready to be saved
46
     */
47
    function ml(array $destination, string $input)
48
    {
49
        if (is_null($destination)) {
0 ignored issues
show
introduced by
The condition is_null($destination) is always false.
Loading history...
50
            $destination = [];
51
        }
52
53
        return array_merge($destination, [cl() => $input]);
54
    }
55
}
56
57
if (! function_exists('isML')) {
58
    function isML($value)
59
    {
60
        try {
61
            if (gettype($value) !== 'array') {
62
                throw new Exception($value.' is not a valid array!');
63
            }
64
        } catch (Exception $exception) {
65
            echo $exception->getMessage();
66
        }
67
68
        if (array_key_exists('is-ml', $value)) {
69
            return $value['is-ml'];
70
        } else {
71
            return false;
72
        }
73
    }
74
}
75
76
if (! function_exists('isMD')) {
77
    function isMD($value)
78
    {
79
        if (array_key_exists('is-md', $value)) {
80
            return $value['is-md'];
81
        } else {
82
            return false;
83
        }
84
    }
85
}
86
if (! function_exists('is_EM')) {
87
    function is_EM($value)
88
    {
89
        if ($value === 'EmbedsMany') {
90
            return true;
91
        } else {
92
            return false;
93
        }
94
    }
95
}
96
97
if (! function_exists('is_EO')) {
98
    function is_EO($value)
99
    {
100
        if ($value === 'EmbedsOne') {
101
            return true;
102
        } else {
103
            return false;
104
        }
105
    }
106
}
107
108
if (! function_exists('is_HM')) {
109
    function is_HM($value)
110
    {
111
        if ($value === 'HasMany') {
112
            return true;
113
        } else {
114
            return false;
115
        }
116
    }
117
}
118
119
if (! function_exists('is_HO')) {
120
    function is_HO($value)
121
    {
122
        if ($value === 'HasOne') {
123
            return true;
124
        } else {
125
            return false;
126
        }
127
    }
128
}
129
130
if (! function_exists('isEditable')) {
131
    function isEditable($value)
132
    {
133
        if (array_key_exists('is-editable', $value)) {
134
            return $value['is-editable'];
135
        } else {
136
            return true;
137
        }
138
    }
139
}
140
141
if (! function_exists('hasTarget')) {
142
    function hasTarget($value)
143
    {
144
        if (array_key_exists('has-target', $value)) {
145
            return $value['has-target'];
146
        } else {
147
            return true;
148
        }
149
    }
150
}
151
if (! function_exists('isFillable')) {
152
    function isFillable($value, $event)
153
    {
154
        if ($event === 'add') {
155
            return true;
156
        } else {
157
            return isEditable($value);
158
        }
159
    }
160
}
161
162
if (! function_exists('getRequestToBeSync')) {
163
    /**
164
     * @param $ref_id
165
     * @param $modelOnTarget
166
     * @param Request $request
167
     *
168
     * @return Request
169
     */
170
    function getRequestToBeSync($ref_id, $modelOnTarget, Request $request, $methodOnTarget)
171
    {
172
        $new_req_embeded = new stdClass();
173
        $model = new $modelOnTarget;
174
        $items = $model->getItems();
175
176
        foreach ($items as $key => $item) {
177
            if ($key == 'ref_id') {
178
                $new_req_embeded->$key = $ref_id;
179
            } else {
180
                $new_req_embeded->$key = $request->input($key);
181
            }
182
        }
183
184
        $new_req = [];
185
        $new_req[$methodOnTarget] = json_encode($new_req_embeded);
186
        $request = new Request;
187
        $request->merge($new_req);
188
189
        return $request;
190
    }
191
}
192
193
if (! function_exists('getPrimaryRequest')) {
194
    /**
195
     * @param string $request
196
     *
197
     * @return string
198
     */
199
    function getPrimaryRequest($request)
200
    {
201
        if (! is_null($request)) {
0 ignored issues
show
introduced by
The condition is_null($request) is always false.
Loading history...
202
            $arr = [];
203
            $categorylistdataJson = $request;
204
            $categorylistdataArr = json_decode($categorylistdataJson);
205
            if (empty($categorylistdataArr)) {
206
                return;
207
            } else {
208
                //extract first array  and create the primary category
209
                $primarycategory = ($categorylistdataArr[0]);
210
                $arr[] = $primarycategory;
211
212
                return json_encode($arr);
213
            }
214
        } else {
215
            return '';
216
        }
217
    }
218
}
219
220
if (! function_exists('getAID')) {
221
    /**
222
     * @param Model $model
223
     * @return string
224
     */
225
    function getAID(Model $model)
226
    {
227
        //Get Last Obj
228
        $obj = $model->orderBy('created_at', 'desc')->first();
229
230
        return is_null($obj) ? 1 : $obj->autoincrement_id + 1;
231
    }
232
}
233
234
if (! function_exists('processList')) {
235
    /**
236
     * @param array $array
237
     *
238
     * @return string
239
     */
240
    function processList($array)
241
    {
242
        $final = [];
243
        $n = count($array);
244
        for ($i = 0; $i < $n; $i++) {
245
            $obj = [];
246
            if ($array[$i] !== null) {
247
                $obj = ['label' => $array[$i], 'key' => $i];
248
                $final[] = $obj;
249
            } else {
250
                $final[] = $obj;
251
                array_pop($final);
252
            }
253
        }
254
255
        return json_encode($final);
256
    }
257
}
258
259
if (! function_exists('isRequestReadyToBeProcessed')) {
260
    function isRequestReadyToBeProcessed(Request $request)
261
    {
262
        $requests = $request->all();
263
        unset($requests['_token']);
264
265
        foreach ($requests as $key => $val) {
266
            $pos = strpos($key, '-');
267
            if ($pos === false) {
268
                return false;
269
            }
270
        }
271
272
        return true;
273
    }
274
}
275
276
if (! function_exists('removeSubCollectionInput')) {
277
    function removeSubCollectionInput(Request $request)
278
    {
279
        return $request;
280
    }
281
}
282
283
if (! function_exists('prepareRequest')) {
284
285
    /**
286
     * @param Request $request
287
     * @param array $additionalData
288
     *
289
     * @return Request
290
     */
291
    function prepareRequest(Request $request, array $additionalData)
292
    {
293
        $request = $request->merge($additionalData);
294
        $additionalData = removeSubCollectionInput($request);
295
        $request = new Request;
296
297
        if (isRequestReadyToBeProcessed($request)) {
298
            return $request->merge($additionalData);
0 ignored issues
show
Bug introduced by
$additionalData of type Illuminate\Http\Request is incompatible with the type array expected by parameter $input of Illuminate\Http\Request::merge(). ( Ignorable by Annotation )

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

298
            return $request->merge(/** @scrutinizer ignore-type */ $additionalData);
Loading history...
299
        } else {
300
            return prepareRequest($request, $additionalData);
0 ignored issues
show
Bug introduced by
$additionalData of type Illuminate\Http\Request is incompatible with the type array expected by parameter $additionalData of prepareRequest(). ( Ignorable by Annotation )

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

300
            return prepareRequest($request, /** @scrutinizer ignore-type */ $additionalData);
Loading history...
301
        }
302
    }
303
}
304
305
if (! function_exists('getArrayWithEmptyObj')) {
306
307
    /**
308
     * @param $model
309
     *
310
     * @return array
311
     */
312
    function getArrayWithEmptyObj($model, $is_EO, $is_EM)
0 ignored issues
show
Unused Code introduced by
The parameter $is_EM 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

312
    function getArrayWithEmptyObj($model, $is_EO, /** @scrutinizer ignore-unused */ $is_EM)

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...
313
    {
314
        $arr = [];
315
        if ($is_EO) {
316
            $obj = new stdClass();
317
            $embedObj = new $model;
318
            $EOitems = $embedObj->getItems();
319
320
            //Current Obj Create
321
            foreach ($EOitems as $EOkey => $item) {
322
                $obj->$EOkey = null;
323
            }
324
325
            $arr[] = $obj;
326
        }// $is_EM == empty array
327
328
        return $arr;
329
    }
330
}
331
332
if (! function_exists('getCounterForRelationships')) {
333
334
    /**
335
     * @param $method
336
     * @param $is_EO
337
     * @param $is_EM
338
     * @param $i
339
     *
340
     * @return string
341
     */
342
    function getCounterForRelationships($method, $is_EO, $is_EM, $i)
343
    {
344
        if ($method === '' || ($method !== '' && $is_EO) || ($method === '' && $is_EM)) {
345
            return '';
346
        } else {
347
            return '-'.$i;
348
        }
349
    }
350
}
351