Passed
Push — master ( a8e4d4...81b17d )
by Giacomo
51:25 queued 46:20
created

getTypeOnTarget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
nop 1
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\Arr;
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
     * @return string current Lang
32
     */
33
    function cl()
34
    {
35
        //Get current Lang
36
        return Config::get('app.locale');
37
    }
38
}
39
40
if (! function_exists('ml')) {
41
    //save a localized field
42
    /**
43
     * @param array $destination
44
     * @param string $input
45
     *
46
     * @return array ready to be saved
47
     */
48
    function ml($destination, $input)
49
    {
50
        if (is_null($destination)) {
0 ignored issues
show
introduced by
The condition is_null($destination) is always false.
Loading history...
51
            $destination = [];
52
        }
53
54
        return array_merge($destination, [cl() => $input]);
55
    }
56
}
57
58
if (! function_exists('isML')) {
59
    function isML($value)
60
    {
61
        if (array_key_exists('is-ml', $value)) {
62
            return $value['is-ml'];
63
        } else {
64
            return false;
65
        }
66
    }
67
}
68
69
if (! function_exists('isMD')) {
70
    function isMD($value)
71
    {
72
        if (array_key_exists('is-md', $value)) {
73
            return $value['is-md'];
74
        } else {
75
            return false;
76
        }
77
    }
78
}
79
if (! function_exists('is_EM')) {
80
    function is_EM($value)
81
    {
82
        if ($value === 'EmbedsMany') {
83
            return true;
84
        } else {
85
            return false;
86
        }
87
    }
88
}
89
90
if (! function_exists('is_EO')) {
91
    function is_EO($value)
92
    {
93
        if ($value === 'EmbedsOne') {
94
            return true;
95
        } else {
96
            return false;
97
        }
98
    }
99
}
100
101
if (! function_exists('is_HM')) {
102
    function is_HM($value)
103
    {
104
        if ($value === 'HasMany') {
105
            return true;
106
        } else {
107
            return false;
108
        }
109
    }
110
}
111
112
if (! function_exists('is_HO')) {
113
    function is_HO($value)
114
    {
115
        if ($value === 'HasOne') {
116
            return true;
117
        } else {
118
            return false;
119
        }
120
    }
121
}
122
123
if (! function_exists('isEditable')) {
124
    function isEditable($value)
125
    {
126
        if (array_key_exists('is-editable', $value)) {
127
            return $value['is-editable'];
128
        } else {
129
            return true;
130
        }
131
    }
132
}
133
134
if (! function_exists('hasTarget')) {
135
    function hasTarget($value)
136
    {
137
        if (array_key_exists('has-target', $value)) {
138
            return $value['has-target'];
139
        } else {
140
            return true;
141
        }
142
    }
143
}
144
if (! function_exists('isFillable')) {
145
    function isFillable($value, $event)
146
    {
147
        if ($event === 'add') {
148
            return true;
149
        } else {
150
            return isEditable($value);
151
        }
152
    }
153
}
154
155
if (! function_exists('getRequestToBeSync')) {
156
    /**
157
     * @param $ref_id
158
     * @param $modelOnTarget
159
     * @param Request $request
160
     *
161
     * @return Request
162
     */
163
    function getRequestToBeSync($ref_id, $modelOnTarget, Request $request, $methodOnTarget)
164
    {
165
        $new_req_embeded = new stdClass();
166
        $model = new $modelOnTarget;
167
        $items = $model->getItems();
168
169
        foreach ($items as $key => $item) {
170
            if ($key == 'ref_id') {
171
                $new_req_embeded->$key = $ref_id;
172
            } else {
173
                $new_req_embeded->$key = $request->input($key);
174
            }
175
        }
176
177
        $new_req = [];
178
        $new_req[$methodOnTarget] = json_encode($new_req_embeded);
179
        $request = new Request;
180
        $request->merge($new_req);
181
182
        return $request;
183
    }
184
}
185
186
if (! function_exists('getPrimaryRequest')) {
187
    /**
188
     * @param string $request
189
     *
190
     * @return string
191
     */
192
    function getPrimaryRequest($request)
193
    {
194
        if (! is_null($request)) {
0 ignored issues
show
introduced by
The condition is_null($request) is always false.
Loading history...
195
            $arr = [];
196
            $categorylistdataJson = $request;
197
            $categorylistdataArr = json_decode($categorylistdataJson);
198
            if (empty($categorylistdataArr)) {
199
                return;
200
            } else {
201
                //extract first array  and create the primary category
202
                $primarycategory = ($categorylistdataArr[0]);
203
                $arr[] = $primarycategory;
204
205
                return json_encode($arr);
206
            }
207
        } else {
208
            return '';
209
        }
210
    }
211
}
212
213
if (! function_exists('getAID')) {
214
    /**
215
     * @param Model $model
216
     * @return string
217
     */
218
    function getAID(Model $model)
219
    {
220
        //Get Last Obj
221
        $obj = $model->orderBy('created_at', 'desc')->first();
222
223
        return is_null($obj) ? 1 : $obj->autoincrement_id + 1;
224
    }
225
}
226
227
if (! function_exists('isRequestReadyToBeProcessed')) {
228
    function isRequestReadyToBeProcessed(Request $request)
229
    {
230
        $requests = $request->all();
231
        unset($requests['_token']);
232
233
        foreach ($requests as $key => $val) {
234
            $pos = strpos($key, '-');
235
            if ($pos === false) {
236
                return false;
237
            }
238
        }
239
240
        return true;
241
    }
242
}
243
244
if (! function_exists('removeSubCollectionInput')) {
245
    function removeSubCollectionInput(Request $request)
246
    {
247
        return $request;
248
    }
249
}
250
251
if (! function_exists('prepareRequest')) {
252
253
    /**
254
     * @param Request $request
255
     * @param array $additionalData
256
     *
257
     * @return Request
258
     */
259
    function prepareRequest(Request $request, array $additionalData)
260
    {
261
        $request = $request->merge($additionalData);
262
        $additionalData = removeSubCollectionInput($request);
263
        $request = new Request;
264
265
        if (isRequestReadyToBeProcessed($request)) {
266
            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

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

268
            return prepareRequest($request, /** @scrutinizer ignore-type */ $additionalData);
Loading history...
269
        }
270
    }
271
}
272
273
if (! function_exists('getArrayWithEmptyObj')) {
274
275
    /**
276
     * @param $model
277
     *
278
     * @return array
279
     */
280
    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

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