Test Failed
Pull Request — master (#33)
by Giacomo
06:46 queued 03:09
created

MainMongoTrait   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 31
eloc 73
c 1
b 0
f 0
dl 0
loc 283
rs 9.92

21 Methods

Rating   Name   Duplication   Size   Complexity  
A setPartialGeneratedRequest() 0 6 1
A updateWithSync() 0 10 1
A setRequest() 0 4 1
A setTargetAdditionalData() 0 3 1
A getOptions() 0 3 1
A storeWithSync() 0 10 1
A getModelTobeSync() 0 9 1
A getOptionValue() 0 3 2
A setOptions() 0 3 1
A checkRequestExistence() 0 5 2
A getRequest() 0 3 1
A destroyWithSync() 0 38 5
A getIsSkippable() 0 3 3
A initDataForSync() 0 6 1
A getTargetAdditionalData() 0 3 1
A getHasPartialRequest() 0 3 1
A getRelationshipRequest() 0 8 1
A setHasPartialRequest() 0 6 1
A getPartialGeneratedRequest() 0 3 1
A checkArrayExistence() 0 5 2
A checkPropertyExistence() 0 5 2
1
<?php
2
3
namespace OfflineAgency\MongoAutoSync\Traits;
4
5
use Exception;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Arr;
8
9
trait MainMongoTrait
10
{
11
    protected $has_partial_request;
12
    protected $request;
13
    protected $target_additional_data;
14
    protected $partial_generated_request;
15
    protected $options;
16
    protected $tempEM;
17
18
    /**
19
     * @param Request $request
20
     * @param array $additionalData
21
     * @param array $options
22
     * @param array $target_additional_data
23
     * @return $this
24
     * @throws Exception
25
     */
26
    public function storeWithSync(Request $request, array $additionalData = [], array $options = [], array $target_additional_data = [])
27
    {
28
        $this->initDataForSync($request, $additionalData, $options, $target_additional_data);
29
        $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

29
        $this->/** @scrutinizer ignore-call */ 
30
               storeEditAllItems($request, 'add', $options);
Loading history...
30
        $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

30
        $this->/** @scrutinizer ignore-call */ 
31
               processAllRelationships($request, 'add', '', '', $options);
Loading history...
31
32
        //Dispatch the creation event
33
        $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

33
        $this->/** @scrutinizer ignore-call */ 
34
               fireModelEvent('storeWithSync');
Loading history...
34
35
        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

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

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

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

93
        $this->/** @scrutinizer ignore-call */ 
94
               delete();
Loading history...
94
95
        //Dispatch the destroy event
96
        $this->fireModelEvent('destroyWithSync');
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param array $options
103
     * @param string $key
104
     * @return bool|mixed
105
     */
106
    private function getOptionValue(array $options, string $key)
107
    {
108
        return Arr::has($options, $key) ? $options[$key] : '';
109
    }
110
111
    /**
112
     * @param $obj
113
     * @param string $EOkey
114
     * @param string $method
115
     * @param string $model
116
     * @throws Exception
117
     */
118
    public function checkPropertyExistence($obj, string $EOkey, $method = '', $model = '')
119
    {
120
        if (! property_exists($obj, $EOkey)) {
121
            $msg = 'Error - '.$EOkey.' attribute not found on obj '.json_encode($obj).' during save of model: '.$model.' and attribute: '.$method;
122
            throw new Exception($msg);
123
        }
124
    }
125
126
    /**
127
     * @param $arr
128
     * @param string $key
129
     * @throws Exception
130
     */
131
    public function checkArrayExistence($arr, string $key)
132
    {
133
        if (! Arr::has($arr, $key)) {
134
            $msg = ('Error - '.$key.' attribute not found on obj '.json_encode($arr));
135
            throw new Exception($msg);
136
        }
137
    }
138
139
    /**
140
     * @param Request $request
141
     * @param string $key
142
     * @throws Exception
143
     */
144
    private function checkRequestExistence(Request $request, string $key)
145
    {
146
        if (! $request->has($key)) {
147
            $msg = ('Error - '.$key.' attribute not found in Request '.json_encode($request->all()));
148
            throw new Exception($msg);
149
        }
150
    }
151
152
    /**
153
     * @param bool $request_has_key
154
     * @param bool $hasTarget
155
     * @return bool
156
     */
157
    public function getIsSkippable($request_has_key, $hasTarget = false)
158
    {
159
        return ! $request_has_key && $this->getHasPartialRequest() && ! $hasTarget;
160
    }
161
162
    /**
163
     * @return bool
164
     */
165
    public function getHasPartialRequest()
166
    {
167
        return $this->has_partial_request;
168
    }
169
170
    public function setHasPartialRequest(): void
171
    {
172
        $this->has_partial_request = $this->getOptionValue(
173
                $this->getOptions(),
174
                'request_type'
175
            ) == 'partial';
176
    }
177
178
    /**
179
     * @param string $modelTarget
180
     * @param stdClass $obj
181
     * @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...
182
     * @throws Exception
183
     */
184
    private function getModelTobeSync(string $modelTarget, stdClass $obj)
0 ignored issues
show
Bug introduced by
The type OfflineAgency\MongoAutoSync\Traits\stdClass was not found. Did you mean stdClass? If so, make sure to prefix the type with \.
Loading history...
185
    {
186
        $this->checkPropertyExistence($obj, 'ref_id');
187
        $target_id = $obj->ref_id;
188
189
        //Init the Target Model
190
        $modelToBeSync = new $modelTarget;
191
192
        return $modelToBeSync->find($target_id);
193
    }
194
195
    /**
196
     * @param string $key
197
     * @param Request $request
198
     * @return mixed
199
     * @throws Exception
200
     */
201
    private function getRelationshipRequest(string $key, Request $request)
202
    {
203
        $this->checkRequestExistence(
204
            $request,
205
            $key
206
        );
207
208
        return $request->input($key);
209
    }
210
211
    /**
212
     * @return Request
213
     */
214
    public function getRequest()
215
    {
216
        return $this->request;
217
    }
218
219
    /**
220
     * @param Request $request
221
     * @param array $additionalData
222
     */
223
    public function setRequest(Request $request, array $additionalData): void
224
    {
225
        $request = $request->merge($additionalData);
226
        $this->request = $request;
227
    }
228
229
    /**
230
     * @return Request
231
     */
232
    public function getPartialGeneratedRequest()
233
    {
234
        return $this->partial_generated_request;
235
    }
236
237
    /**
238
     * @param array $arr
239
     */
240
    public function setPartialGeneratedRequest(array $arr): void
241
    {
242
        $partial_generated_request = new Request;
243
        $partial_generated_request->merge($arr);
244
245
        $this->partial_generated_request = $partial_generated_request;
246
    }
247
248
    /**
249
     * @return array
250
     */
251
    public function getOptions()
252
    {
253
        return $this->options;
254
    }
255
256
    /**
257
     * @param array $options
258
     */
259
    public function setOptions(array $options): void
260
    {
261
        $this->options = $options;
262
    }
263
264
    /**
265
     * @return array
266
     */
267
    public function getTargetAdditionalData()
268
    {
269
        return $this->target_additional_data;
270
    }
271
272
    /**
273
     * @param array $target_additional_data
274
     */
275
    public function setTargetAdditionalData($target_additional_data): void
276
    {
277
        $this->target_additional_data = $target_additional_data;
278
    }
279
280
    /**
281
     * @param Request $request
282
     * @param array $additionalData
283
     * @param array $options
284
     * @param array $target_additional_data
285
     */
286
    public function initDataForSync(Request $request, array $additionalData, array $options, array $target_additional_data)
287
    {
288
        $this->setRequest($request, $additionalData);
289
        $this->setTargetAdditionalData($target_additional_data);
290
        $this->setOptions($options);
291
        $this->setHasPartialRequest();
292
    }
293
}
294