Passed
Push — master ( 6530d8...a8e4d4 )
by Giacomo
03:19 queued 10s
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($destination, $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('isRequestReadyToBeProcessed')) {
235
    function isRequestReadyToBeProcessed(Request $request)
236
    {
237
        $requests = $request->all();
238
        unset($requests['_token']);
239
240
        foreach ($requests as $key => $val) {
241
            $pos = strpos($key, '-');
242
            if ($pos === false) {
243
                return false;
244
            }
245
        }
246
247
        return true;
248
    }
249
}
250
251
if (! function_exists('removeSubCollectionInput')) {
252
    function removeSubCollectionInput(Request $request)
253
    {
254
        return $request;
255
    }
256
}
257
258
if (! function_exists('prepareRequest')) {
259
260
    /**
261
     * @param Request $request
262
     * @param array $additionalData
263
     *
264
     * @return Request
265
     */
266
    function prepareRequest(Request $request, array $additionalData)
267
    {
268
        $request = $request->merge($additionalData);
269
        $additionalData = removeSubCollectionInput($request);
270
        $request = new Request;
271
272
        if (isRequestReadyToBeProcessed($request)) {
273
            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

273
            return $request->merge(/** @scrutinizer ignore-type */ $additionalData);
Loading history...
274
        } else {
275
            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

275
            return prepareRequest($request, /** @scrutinizer ignore-type */ $additionalData);
Loading history...
276
        }
277
    }
278
}
279
280
if (! function_exists('getArrayWithEmptyObj')) {
281
282
    /**
283
     * @param $model
284
     *
285
     * @return array
286
     */
287
    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

287
    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...
288
    {
289
        $arr = [];
290
        if ($is_EO) {
291
            $obj = new stdClass();
292
            $embedObj = new $model;
293
            $EOitems = $embedObj->getItems();
294
295
            //Current Obj Create
296
            foreach ($EOitems as $EOkey => $item) {
297
                $obj->$EOkey = null;
298
            }
299
300
            $arr[] = $obj;
301
        }// $is_EM == empty array
302
303
        return $arr;
304
    }
305
}
306
307
if (! function_exists('getCounterForRelationships')) {
308
309
    /**
310
     * @param $method
311
     * @param $is_EO
312
     * @param $is_EM
313
     * @param $i
314
     *
315
     * @return string
316
     */
317
    function getCounterForRelationships($method, $is_EO, $is_EM, $i)
318
    {
319
        if ($method === '' || ($method !== '' && $is_EO) || ($method === '' && $is_EM)) {
320
            return '';
321
        } else {
322
            return '-'.$i;
323
        }
324
    }
325
}
326