Passed
Push — master ( 18570f...cbd528 )
by Giacomo
15:24
created

getBillingAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
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...
5
use Illuminate\Support\Facades\Config;
6
use Jenssegers\Mongodb\Eloquent\Model;
7
8
if (!function_exists('getTranslatedContent')) {
9
10
    /**
11
     * @param array $mlCollection
12
     *
13
     * @return string
14
     */
15
    function getTranslatedContent($mlCollection)
16
    {
17
        //Get current Lang
18
        $cl = Config::get('app.locale');
19
20
        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
Comprehensibility Best Practice introduced by
The variable $destination seems to never exist and therefore isset should always be false.
Loading history...
introduced by
The condition is_null($mlCollection || ! IssetNode) is always 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...
21
            return $mlCollection[$cl];
22
        } else {
23
            return "";
24
        }
25
    }
26
}
27
28
if (!function_exists('cl')) {
29
30
    /**
31
     *
32
     * @return string current Lang
33
     */
34
    function cl()
35
    {
36
        //Get current Lang
37
        return Config::get('app.locale');
38
    }
39
}
40
41
if (!function_exists('ml')) {
42
    //save a localized field
43
    /**
44
     * @param array $destination
45
     * @param string $input
46
     *
47
     * @return array ready to be saved
48
     */
49
    function ml($destination, $input)
50
    {
51
        if (is_null($destination)) {
0 ignored issues
show
introduced by
The condition is_null($destination) is always false.
Loading history...
52
            $destination = array();
53
        }
54
55
        return array_merge($destination, array(cl() => $input));
56
    }
57
}
58
59
if (!function_exists('isML')) {
60
    function isML($value)
61
    {
62
        if (array_key_exists('is-ml', $value)) {
63
            return $value['is-ml'];
64
        } else {
65
            return false;
66
        }
67
    }
68
}
69
70
71
if (!function_exists('isMD')) {
72
    function isMD($value)
73
    {
74
        if (array_key_exists('is-md', $value)) {
75
            return $value['is-md'];
76
        } else {
77
            return false;
78
        }
79
    }
80
}
81
if (!function_exists('is_EM')) {
82
    function is_EM($value)
83
    {
84
        if ($value === 'EmbedsMany') {
85
            return true;
86
        } else {
87
            return false;
88
        }
89
    }
90
}
91
92
if (!function_exists('is_EO')) {
93
    function is_EO($value)
94
    {
95
        if ($value === 'EmbedsOne') {
96
            return true;
97
        } else {
98
            return false;
99
        }
100
    }
101
}
102
103
if (!function_exists('is_HM')) {
104
    function is_HM($value)
105
    {
106
        if ($value === 'HasMany') {
107
            return true;
108
        } else {
109
            return false;
110
        }
111
    }
112
}
113
114
if (!function_exists('is_HO')) {
115
    function is_HO($value)
116
    {
117
        if ($value === 'HasOne') {
118
            return true;
119
        } else {
120
            return false;
121
        }
122
    }
123
}
124
125
if (!function_exists('isEditable')) {
126
    function isEditable($value)
127
    {
128
        if (array_key_exists('is-editable', $value)) {
129
            return $value['is-editable'];
130
        } else {
131
            return true;
132
        }
133
    }
134
}
135
136
if (!function_exists('hasTarget')) {
137
    function hasTarget($value)
138
    {
139
        if (array_key_exists('has-target', $value)) {
140
            return $value['has-target'];
141
        } else {
142
            return true;
143
        }
144
    }
145
}
146
if (!function_exists('isFillable')) {
147
    function isFillable($value, $event)
148
    {
149
        if ($event === 'add') {
150
            return true;
151
        } else {
152
            return isEditable($value);
153
        }
154
    }
155
}
156
157
if (!function_exists('getRequestToBeSync')) {
158
    /**
159
     * @param $ref_id
160
     * @param $modelOnTarget
161
     * @param Request $request
162
     *
163
     * @return Request
164
     */
165
    function getRequestToBeSync($ref_id, $modelOnTarget, Request $request, $methodOnTarget)
166
    {
167
        $new_req_embeded = new stdClass();
168
        $model = new $modelOnTarget;
169
        $items = $model->getItems();
170
171
        foreach ($items as $key => $item) {
172
            if ($key == "ref_id") {
173
                $new_req_embeded->$key = $ref_id;
174
            } else {
175
                $new_req_embeded->$key = $request->input($key);
176
            }
177
        }
178
179
        $new_req = array();
180
        $new_req[$methodOnTarget] = json_encode($new_req_embeded);
181
        $request = new Request;
182
        $request->merge($new_req);
183
        return $request;
184
    }
185
}
186
187
if (!function_exists('getPrimaryRequest')) {
188
    /**
189
     * @param String $request
190
     *
191
     * @return String
192
     */
193
    function getPrimaryRequest($request)
194
    {
195
        if (!is_null($request)) {
0 ignored issues
show
introduced by
The condition is_null($request) is always false.
Loading history...
196
            $arr = [];
197
            $categorylistdataJson = $request;
198
            $categorylistdataArr = json_decode($categorylistdataJson);
199
            if (empty($categorylistdataArr)) {
200
                return null;
201
            } else {
202
                //extract first array  and create the primary category
203
                $primarycategory = ($categorylistdataArr[0]);
204
                $arr[] = $primarycategory;
205
                $out = json_encode($arr);
206
                return $out;
207
            }
208
        } else {
209
            return "";
210
        }
211
    }
212
}
213
214
if (!function_exists('getAID')) {
215
    /**
216
     * @param String $request
217
     *
218
     * @return String
219
     */
220
    function getAID(Model $model)
221
    {
222
        //Get Last Obj
223
        $q = $model->orderBy('created_at', 'desc')->first();
224
        //check if Obj exist and increments numeric id
225
        if ($q == null) {
226
            $arr['autoincrement_id'] = 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr = array(); before regardless.
Loading history...
227
        } else {
228
            $arr['autoincrement_id'] = $q->autoincrement_id + 1;
229
        }
230
        return $arr['autoincrement_id'];
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
        for ($i = 0; $i < count($array); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
244
            $obj = [];
245
            if ($array[$i] != null) {
246
                $obj = ['label' => $array[$i], 'key' => $i];
247
                $final[] = $obj;
248
            } else {
249
                $final[] = $obj;
250
                array_pop($final);
251
            }
252
        }
253
        return json_encode($final);
254
    }
255
}
256
257
if (!function_exists('isRequestReadyToBeProcessed')) {
258
    function isRequestReadyToBeProcessed(Request $request)
259
    {
260
        $requests = $request->all();
261
        unset($requests['_token']);
262
263
        foreach ($requests as $key => $val) {
264
            $pos = strpos($key, "-");
265
            if ($pos === false) {
266
                return false;
267
            }
268
        }
269
        return true;
270
    }
271
}
272
273
if (!function_exists('removeSubCollectionInput')) {
274
    function removeSubCollectionInput(Request $request)
275
    {
276
        return $request;
277
    }
278
}
279
280
if (!function_exists('prepareRequest')) {
281
282
    /**
283
     * @param Request $request
284
     * @param array $additionalData
285
     *
286
     * @return Request
287
     */
288
    function prepareRequest(Request $request, array $additionalData)
289
    {
290
        $request = $request->merge($additionalData);
291
        $additionalData = removeSubCollectionInput($request);
292
        $request = new Request;
293
294
        if (isRequestReadyToBeProcessed($request)) {
295
            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

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

297
            return prepareRequest($request, /** @scrutinizer ignore-type */ $additionalData);
Loading history...
298
        }
299
    }
300
}
301
302
303
if (!function_exists('getArrayWithEmptyObj')) {
304
305
    /**
306
     * @param $model
307
     *
308
     * @return array
309
     */
310
    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

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