Passed
Push — master ( 3b373d...6530d8 )
by Giacomo
02:21 queued 11s
created

MainMongoTrait::destroyWithSync()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 38
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 38
rs 9.3888
cc 5
nc 4
nop 0
1
<?php
2
3
namespace OfflineAgency\MongoAutoSync\Traits;
4
5
use Exception;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Arr;
8
use stdClass;
9
10
trait MainMongoTrait
11
{
12
    protected $has_partial_request;
13
    protected $request;
14
    protected $target_additional_data;
15
    protected $partial_generated_request;
16
    protected $options;
17
    protected $tempEM;
18
19
    /**
20
     * @param Request $request
21
     * @param array $additionalData
22
     * @param array $options
23
     * @param array $target_additional_data
24
     * @return $this
25
     * @throws Exception
26
     */
27
    public function storeWithSync(Request $request, array $additionalData = [], array $options = [], array $target_additional_data = [])
28
    {
29
        $this->initDataForSync($request, $additionalData, $options, $target_additional_data);
30
        $this->storeEditAllItems($request, 'add', $options);
0 ignored issues
show
Bug introduced by
It seems like storeEditAllItems() 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

30
        $this->/** @scrutinizer ignore-call */ 
31
               storeEditAllItems($request, 'add', $options);
Loading history...
31
        $this->processAllRelationships($request, 'add', '', '', $options);
0 ignored issues
show
Bug introduced by
It seems like processAllRelationships() 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

31
        $this->/** @scrutinizer ignore-call */ 
32
               processAllRelationships($request, 'add', '', '', $options);
Loading history...
32
33
        //Dispatch the creation event
34
        $this->fireModelEvent('storeWithSync');
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() 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

34
        $this->/** @scrutinizer ignore-call */ 
35
               fireModelEvent('storeWithSync');
Loading history...
35
36
        return $this->fresh();
0 ignored issues
show
Bug introduced by
It seems like fresh() 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

36
        return $this->/** @scrutinizer ignore-call */ fresh();
Loading history...
37
    }
38
39
    /**
40
     * @param Request $request
41
     * @param array $additionalData
42
     * @param array $options
43
     * @param array $target_additional_data
44
     * @return $this
45
     * @throws Exception
46
     */
47
    public function updateWithSync(Request $request, array $additionalData = [], array $options = [], array $target_additional_data = [])
48
    {
49
        $this->initDataForSync($request, $additionalData, $options, $target_additional_data);
50
        $this->storeEditAllItems($request, 'update', $options);
51
        $this->processAllRelationships($request, 'update', '', '', $options);
52
53
        //Dispatch the update event
54
        $this->fireModelEvent('updateWithSync');
55
56
        return $this->fresh();
57
    }
58
59
    /**
60
     * @return $this
61
     */
62
    public function destroyWithSync()
63
    {
64
        //Get the relation info
65
        $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

65
        /** @scrutinizer ignore-call */ 
66
        $relations = $this->getMongoRelation();
Loading history...
66
67
        //Process all relationships
68
        foreach ($relations as $method => $relation) {
69
            //Get Relation Save Mode
70
            $type = $relation['type'];
71
            $hasTarget = hasTarget($relation);
72
            if ($hasTarget) {
73
                $modelTarget = $relation['modelTarget'];
74
                $methodOnTarget = $relation['methodOnTarget'];
75
                $modelOnTarget = $relation['modelOnTarget'];
0 ignored issues
show
Unused Code introduced by
The assignment to $modelOnTarget is dead and can be removed.
Loading history...
76
77
                $is_EO = is_EO($type);
78
                $is_EM = is_EM($type);
79
                $is_HO = is_HO($type);
0 ignored issues
show
Unused Code introduced by
The assignment to $is_HO is dead and can be removed.
Loading history...
80
                $is_HM = is_HM($type);
0 ignored issues
show
Unused Code introduced by
The assignment to $is_HM is dead and can be removed.
Loading history...
81
82
                if ($is_EO || $is_EM) {//EmbedsOne Create - EmbedsMany Create
83
                    //Delete EmbedsMany or EmbedsOne on Target
84
                    $this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO);
0 ignored issues
show
Bug introduced by
It seems like deleteTargetObj() 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

84
                    $this->/** @scrutinizer ignore-call */ 
85
                           deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO);
Loading history...
85
                }
86
87
                //TODO: Need to be implemented
88
                /* elseif ($is_HM) {//HasMany
89
                 } elseif ($is_HO) {//HasOne Create
90
                 }*/
91
            }
92
        }
93
        //Delete current object
94
        $this->delete();
0 ignored issues
show
Bug introduced by
It seems like delete() 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

94
        $this->/** @scrutinizer ignore-call */ 
95
               delete();
Loading history...
95
96
        //Dispatch the destroy event
97
        $this->fireModelEvent('destroyWithSync');
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param array $options
104
     * @param string $key
105
     * @return bool|mixed
106
     */
107
    private function getOptionValue(array $options, string $key)
108
    {
109
        return Arr::has($options, $key) ? $options[$key] : '';
110
    }
111
112
    /**
113
     * @param $obj
114
     * @param string $EOkey
115
     * @param string $method
116
     * @param string $model
117
     * @throws Exception
118
     */
119
    public function checkPropertyExistence($obj, string $EOkey, $method = '', $model = '')
120
    {
121
        if (! property_exists($obj, $EOkey)) {
122
            $msg = 'Error - '.$EOkey.' attribute not found on obj '.json_encode($obj).' during save of model: '.$model.' and attribute: '.$method;
123
            throw new Exception($msg);
124
        }
125
    }
126
127
    /**
128
     * @param $arr
129
     * @param string $key
130
     * @throws Exception
131
     */
132
    public function checkArrayExistence($arr, string $key)
133
    {
134
        if (! Arr::has($arr, $key)) {
135
            $msg = ('Error - '.$key.' attribute not found on obj '.json_encode($arr));
136
            throw new Exception($msg);
137
        }
138
    }
139
140
    /**
141
     * @param Request $request
142
     * @param string $key
143
     * @throws Exception
144
     */
145
    private function checkRequestExistence(Request $request, string $key)
146
    {
147
        if (! $request->has($key)) {
148
            $msg = ('Error - '.$key.' attribute not found in Request '.json_encode($request->all()));
149
            throw new Exception($msg);
150
        }
151
    }
152
153
    /**
154
     * @param bool $request_has_key
155
     * @param bool $hasTarget
156
     * @return bool
157
     */
158
    public function getIsSkippable($request_has_key, $hasTarget = false)
159
    {
160
        return ! $request_has_key && $this->getHasPartialRequest() && ! $hasTarget;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166
    public function getHasPartialRequest()
167
    {
168
        return $this->has_partial_request;
169
    }
170
171
    public function setHasPartialRequest(): void
172
    {
173
        $this->has_partial_request = $this->getOptionValue(
174
                $this->getOptions(),
175
                'request_type'
176
            ) == 'partial';
177
    }
178
179
    /**
180
     * @param string $modelTarget
181
     * @param stdClass $obj
182
     * @return MDModel
0 ignored issues
show
Bug introduced by
The type OfflineAgency\MongoAutoSync\Traits\MDModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
183
     * @throws Exception
184
     */
185
    private function getModelTobeSync(string $modelTarget, stdClass $obj)
186
    {
187
        $this->checkPropertyExistence($obj, 'ref_id');
188
        $target_id = $obj->ref_id;
189
190
        //Init the Target Model
191
        $modelToBeSync = new $modelTarget;
192
193
        return $modelToBeSync->find($target_id);
194
    }
195
196
    /**
197
     * @param string $key
198
     * @param Request $request
199
     * @return mixed
200
     * @throws Exception
201
     */
202
    private function getRelationshipRequest(string $key, Request $request)
203
    {
204
        $this->checkRequestExistence(
205
            $request,
206
            $key
207
        );
208
209
        return $request->input($key);
210
    }
211
212
    /**
213
     * @return Request
214
     */
215
    public function getRequest()
216
    {
217
        return $this->request;
218
    }
219
220
    /**
221
     * @param Request $request
222
     * @param array $additionalData
223
     */
224
    public function setRequest(Request $request, array $additionalData): void
225
    {
226
        $request = $request->merge($additionalData);
227
        $this->request = $request;
228
    }
229
230
    /**
231
     * @return Request
232
     */
233
    public function getPartialGeneratedRequest()
234
    {
235
        return $this->partial_generated_request;
236
    }
237
238
    /**
239
     * @param array $arr
240
     */
241
    public function setPartialGeneratedRequest(array $arr): void
242
    {
243
        $partial_generated_request = new Request;
244
        $partial_generated_request->merge($arr);
245
246
        $this->partial_generated_request = $partial_generated_request;
247
    }
248
249
    /**
250
     * @return array
251
     */
252
    public function getOptions()
253
    {
254
        return $this->options;
255
    }
256
257
    /**
258
     * @param array $options
259
     */
260
    public function setOptions(array $options): void
261
    {
262
        $this->options = $options;
263
    }
264
265
    /**
266
     * @return array
267
     */
268
    public function getTargetAdditionalData()
269
    {
270
        return $this->target_additional_data;
271
    }
272
273
    /**
274
     * @param array $target_additional_data
275
     */
276
    public function setTargetAdditionalData($target_additional_data): void
277
    {
278
        $this->target_additional_data = $target_additional_data;
279
    }
280
281
    /**
282
     * @param Request $request
283
     * @param array $additionalData
284
     * @param array $options
285
     * @param array $target_additional_data
286
     */
287
    public function initDataForSync(Request $request, array $additionalData, array $options, array $target_additional_data)
288
    {
289
        $this->setRequest($request, $additionalData);
290
        $this->setTargetAdditionalData($target_additional_data);
291
        $this->setOptions($options);
292
        $this->setHasPartialRequest();
293
    }
294
}
295