Test Failed
Pull Request — master (#33)
by Giacomo
03:45
created

RelationshipMongoTrait::processAllRelationships()   F

Complexity

Conditions 19
Paths 316

Size

Total Lines 97
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 97
rs 2.2333
cc 19
nc 316
nop 5

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace OfflineAgency\MongoAutoSync\Traits;
5
6
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Arr;
9
use MongoDB\BSON\UTCDateTime;
10
11
trait RelationshipMongoTrait
12
{
13
    /**
14
     * @param Request $request
15
     * @param string $event
16
     * @param string $parent
17
     * @param string $counter
18
     * @param array $options
19
     * @throws Exception
20
     */
21
    public function processAllRelationships(Request $request, string $event, string $parent, string $counter, array $options)
22
    {
23
        $this->setMiniModels(); // For target Sync
0 ignored issues
show
Bug introduced by
It seems like setMiniModels() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

23
        $this->/** @scrutinizer ignore-call */ 
24
               setMiniModels(); // For target Sync
Loading history...
24
25
        //Get the relation info
26
        $relations = $this->getMongoRelation();
0 ignored issues
show
Bug introduced by
It seems like getMongoRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

26
        /** @scrutinizer ignore-call */ 
27
        $relations = $this->getMongoRelation();
Loading history...
27
28
        //Process all relationships
29
        foreach ($relations as $method => $relation) {
30
            //Get Relation Save Mode
31
            $type = $relation['type'];
32
            $model = $relation['model'];
33
            $hasTarget = hasTarget($relation);
34
            if ($hasTarget) {
35
                $modelTarget = $relation['modelTarget'];
36
                $methodOnTarget = $relation['methodOnTarget'];
37
                $modelOnTarget = $relation['modelOnTarget'];
38
                $typeOnTarget = Arr::has($relation, 'typeOnTarget') ? Arr::get($relation, 'typeOnTarget') : 'EmbedsMany';
39
            } else {
40
                $modelTarget = '';
41
                $methodOnTarget = '';
42
                $modelOnTarget = '';
43
                $typeOnTarget = '';
44
            }
45
46
            $is_EO = is_EO($type);
47
            $is_EM = is_EM($type);
48
49
            $is_EM_target = is_EM($typeOnTarget);
50
            $is_EO_target = is_EO($typeOnTarget);
51
52
            $key = $parent.$method.$counter;
53
            $is_skippable = $this->getIsSkippable($request->has($key), $hasTarget);
0 ignored issues
show
Bug introduced by
It seems like getIsSkippable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

53
            /** @scrutinizer ignore-call */ 
54
            $is_skippable = $this->getIsSkippable($request->has($key), $hasTarget);
Loading history...
54
55
            if ($is_skippable) {
56
                continue;
57
            }
58
            $current_request = $request->has($key) ? $request : $this->getPartialGeneratedRequest();
0 ignored issues
show
Bug introduced by
It seems like getPartialGeneratedRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

58
            $current_request = $request->has($key) ? $request : $this->/** @scrutinizer ignore-call */ getPartialGeneratedRequest();
Loading history...
59
60
            $value = $this->getRelationshipRequest($key, $current_request);
0 ignored issues
show
Bug introduced by
It seems like getRelationshipRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

60
            /** @scrutinizer ignore-call */ 
61
            $value = $this->getRelationshipRequest($key, $current_request);
Loading history...
61
62
            $is_embeds_has_to_be_updated = $request->has($key);
63
64
            if (! is_null($value) && ! ($value == '') && ! ($value == '[]')) {
65
                $objs = json_decode($value);
66
            } else {
67
                $objs = getArrayWithEmptyObj($model, $is_EO, $is_EM);
68
            }
69
70
            if ($is_EO || $is_EM) {//EmbedsOne Create - EmbedsMany Create
71
                if ($event == 'update' && $is_embeds_has_to_be_updated) {
72
73
                    //Delete EmbedsMany or EmbedsOne on Target - TODO: check if it is necessary to run deleteTargetObj method
74
                    if ($hasTarget) {
75
                        $this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO, $is_EO_target, $is_EM_target);
76
                    }
77
                    //Delete EmbedsMany or EmbedsOne on current object
78
                    if ($is_EM) {
79
                        $this->$method = [];
80
                        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

80
                        $this->/** @scrutinizer ignore-call */ 
81
                               save();
Loading history...
81
                    }
82
                }
83
84
                if (! empty($objs)) {
85
                    if ($is_EM) {
86
                        $this->tempEM = [];
0 ignored issues
show
Bug Best Practice introduced by
The property tempEM does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
                    }
88
89
                    $i = 0;
90
                    foreach ($objs as $obj) {
91
                        $this->processOneEmbeddedRelationship(
92
                            $request,
93
                            $obj,
94
                            $type,
95
                            $model,
96
                            $method,
97
                            $modelTarget,
98
                            $methodOnTarget,
99
                            $modelOnTarget, $event,
100
                            $hasTarget,
101
                            $is_EO,
102
                            $is_EM,
103
                            $is_EO_target,
104
                            $is_EM_target,
105
                            $i,
106
                            $is_embeds_has_to_be_updated,
107
                            $options);
108
                        $i++;
109
                    }
110
111
                    if ($is_EM) {
112
                        $this->$method = $this->tempEM;
113
                    }
114
                } else {
115
                    $this->$method = [];
116
                }
117
                $this->save();
118
            }
119
        }
120
    }
121
122
    /**
123
     * @param $mini_model
124
     * @param string $method_on_target
125
     * @param bool $is_EO_target
126
     * @param bool $is_EM_target
127
     */
128
    public function updateRelationWithSync($mini_model, string $method_on_target, $is_EO_target, $is_EM_target)
0 ignored issues
show
Unused Code introduced by
The parameter $is_EO_target 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

128
    public function updateRelationWithSync($mini_model, string $method_on_target, /** @scrutinizer ignore-unused */ $is_EO_target, $is_EM_target)

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...
129
    {
130
        if ($is_EM_target) {
131
            $new_values = [];
132
            foreach ($this->$method_on_target as $temp) {
133
                $new_values[] = $temp->attributes;
134
            }
135
            $new_values[] = $mini_model->attributes;
136
        } else {
137
            $new_values = $mini_model->attributes;
138
        }
139
140
        $this->$method_on_target = $new_values;
141
        $this->save();
142
    }
143
144
    /**
145
     * @param Request $request
146
     * @param $obj
147
     * @param $type
148
     * @param $model
149
     * @param $method
150
     * @param $modelTarget
151
     * @param $methodOnTarget
152
     * @param $modelOnTarget
153
     * @param $event
154
     * @param $hasTarget
155
     * @param bool $is_EO
156
     * @param bool $is_EM
157
     * @param bool $is_EO_target
158
     * @param bool $is_EM_target
159
     * @param $i
160
     * @param bool $is_embeds_has_to_be_updated
161
     * @param $options
162
     * @throws Exception
163
     */
164
    public function processOneEmbeddedRelationship(Request $request, $obj, $type, $model, $method, $modelTarget, $methodOnTarget, $modelOnTarget, $event, $hasTarget, $is_EO, $is_EM, $is_EO_target, $is_EM_target, $i, $is_embeds_has_to_be_updated, $options)
165
    {
166
        if ($is_embeds_has_to_be_updated) {
167
            $this->processEmbedOnCurrentCollection($request, $obj, $type, $model, $method, $event, $is_EO, $is_EM, $i, $options);
168
        }
169
170
        if ($hasTarget) {
171
            $this->processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget, $is_EO_target, $is_EM_target);
172
        }
173
    }
174
175
    /**
176
     * @param $method
177
     * @param $modelTarget
178
     * @param $methodOnTarget
179
     * @param bool $is_EO
180
     * @param bool $is_EO_target
181
     * @param bool $is_EM_target
182
     */
183
    public function deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO, $is_EO_target, $is_EM_target)
184
    {
185
        if ($is_EO) {
186
            $embedObj = $this->$method;
187
            if (! is_null($embedObj)) {
188
                $target_id = $embedObj->ref_id;
189
                $this->handleSubTarget($target_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target);
190
            }
191
        } else {
192
            foreach ($this->$method as $target) {
193
                $this->handleSubTarget($target->ref_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target);
194
            }
195
        }
196
    }
197
198
    /**
199
     * @param $target_id
200
     * @param $modelTarget
201
     * @param $methodOnTarget
202
     * @param bool $is_EO_target
203
     * @param bool $is_EM_target
204
     */
205
    public function handleSubTarget($target_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target)
0 ignored issues
show
Unused Code introduced by
The parameter $is_EO_target 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

205
    public function handleSubTarget($target_id, $modelTarget, $methodOnTarget, /** @scrutinizer ignore-unused */ $is_EO_target, $is_EM_target)

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...
206
    {
207
        if ($is_EM_target) {
208
            $target = new $modelTarget;
209
            $target = $target->all()->where('id', $target_id)->first();
210
            if (! is_null($target)) {
211
                $new_values = [];
212
                foreach ($target->$methodOnTarget as $temp) {
213
                    if ($temp->ref_id !== $this->getId()) {
0 ignored issues
show
Bug introduced by
It seems like getId() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

213
                    if ($temp->ref_id !== $this->/** @scrutinizer ignore-call */ getId()) {
Loading history...
214
                        $new_values[] = $temp->attributes;
215
                    }
216
                }
217
                $target->$methodOnTarget = $new_values;
218
                $target->save();
219
            }
220
        }
221
    }
222
223
    /**
224
     * @param Request $request
225
     * @param $obj
226
     * @param $type
227
     * @param $model
228
     * @param $method
229
     * @param $event
230
     * @param $is_EO
231
     * @param $is_EM
232
     * @param $i
233
     * @param $options
234
     * @throws Exception
235
     */
236
    private function processEmbedOnCurrentCollection(Request $request, $obj, $type, $model, $method, $event, $is_EO, $is_EM, $i, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $type 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

236
    private function processEmbedOnCurrentCollection(Request $request, $obj, /** @scrutinizer ignore-unused */ $type, $model, $method, $event, $is_EO, $is_EM, $i, $options)

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...
237
    {
238
        //Init the embed one model
239
        $embedObj = new $model;
240
241
        $EOitems = $embedObj->getItems();
242
        //Current Obj Create
243
        foreach ($EOitems as $EOkey => $item) {
244
            if (! is_null($obj)) {
245
                $is_ML = isML($item);
246
                $is_MD = isMD($item);
247
                $this->checkPropertyExistence($obj, $EOkey, $method, $model);
0 ignored issues
show
Bug introduced by
It seems like checkPropertyExistence() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

247
                $this->/** @scrutinizer ignore-call */ 
248
                       checkPropertyExistence($obj, $EOkey, $method, $model);
Loading history...
248
249
                if ($is_ML) {
250
                    $embedObj->$EOkey = ml([], $obj->$EOkey);
251
                } elseif ($EOkey == 'updated_at' || $EOkey == 'created_at') {
252
                    $embedObj->$EOkey = now();
253
                } elseif ($is_MD) {
254
                    if ($obj->$EOkey == '' || $obj->$EOkey == null) {
255
                        $embedObj->$EOkey = null;
256
                    } else {
257
                        $embedObj->$EOkey = new UTCDateTime(new DateTime($obj->$EOkey));
0 ignored issues
show
Bug introduced by
The type OfflineAgency\MongoAutoSync\Traits\DateTime was not found. Did you mean DateTime? If so, make sure to prefix the type with \.
Loading history...
Bug introduced by
new OfflineAgency\MongoA...\DateTime($obj->$EOkey) of type OfflineAgency\MongoAutoSync\Traits\DateTime is incompatible with the type integer expected by parameter $milliseconds of MongoDB\BSON\UTCDateTime::__construct(). ( Ignorable by Annotation )

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

257
                        $embedObj->$EOkey = new UTCDateTime(/** @scrutinizer ignore-type */ new DateTime($obj->$EOkey));
Loading history...
258
                    }
259
                } else {
260
                    $embedObj->$EOkey = $obj->$EOkey;
261
                }
262
            }
263
        }
264
265
        //else if($is_EM){//To be implemented}
266
        //else if($is_HM){//To be implemented}
267
        //else if($is_HO){//To be implemented}
268
269
        //Get counter for embeds many with level > 1
270
        $counter = getCounterForRelationships($method, $is_EO, $is_EM, $i);
271
        //Check for another Level of Relationship
272
        $embedObj->processAllRelationships($request, $event, $method.'-', $counter, $options);
273
274
        if ($is_EO) {
275
            $this->$method = $embedObj->attributes;
276
        } else {
277
            $this->tempEM[] = $embedObj->attributes;
0 ignored issues
show
Bug Best Practice introduced by
The property tempEM does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
278
        }
279
    }
280
281
    /**
282
     * @param $modelTarget
283
     * @param $obj
284
     * @param $methodOnTarget
285
     * @param $modelOnTarget
286
     * @param bool $is_EO_target
287
     * @param bool $is_EM_target
288
     * @throws Exception
289
     */
290
    private function processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget, $is_EO_target, $is_EM_target)
291
    {
292
        $modelToBeSync = $this->getModelTobeSync($modelTarget, $obj);
0 ignored issues
show
Bug introduced by
It seems like getModelTobeSync() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

292
        /** @scrutinizer ignore-call */ 
293
        $modelToBeSync = $this->getModelTobeSync($modelTarget, $obj);
Loading history...
293
        if (! is_null($modelToBeSync)) {
294
            $miniModel = $this->getEmbedModel($modelOnTarget);
0 ignored issues
show
Bug introduced by
It seems like getEmbedModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

294
            /** @scrutinizer ignore-call */ 
295
            $miniModel = $this->getEmbedModel($modelOnTarget);
Loading history...
295
            $modelToBeSync->updateRelationWithSync($miniModel, $methodOnTarget, $is_EO_target, $is_EM_target);
296
            //TODO:Sync target on level > 1
297
            //$modelToBeSync->processAllRelationships($request, $event, $methodOnTarget, $methodOnTarget . "-");
298
        }
299
    }
300
}
301