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 | * |
||||||
26 | * @throws Exception |
||||||
27 | */ |
||||||
28 | public function storeWithSync(Request $request, array $additionalData = [], array $options = [], array $target_additional_data = []) |
||||||
29 | { |
||||||
30 | $this->initDataForSync($request, $additionalData, $options, $target_additional_data); |
||||||
31 | $this->storeEditAllItems($request, 'add', $options); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
32 | $this->processAllRelationships($request, 'add', '', '', $options); |
||||||
0 ignored issues
–
show
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
![]() |
|||||||
33 | |||||||
34 | //Dispatch the creation event |
||||||
35 | $this->fireModelEvent('storeWithSync'); |
||||||
0 ignored issues
–
show
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
![]() |
|||||||
36 | |||||||
37 | return $this->fresh(); |
||||||
0 ignored issues
–
show
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
![]() |
|||||||
38 | } |
||||||
39 | |||||||
40 | /** |
||||||
41 | * @param Request $request |
||||||
42 | * @param array $additionalData |
||||||
43 | * @param array $options |
||||||
44 | * @param array $target_additional_data |
||||||
45 | * @return $this |
||||||
46 | * |
||||||
47 | * @throws Exception |
||||||
48 | */ |
||||||
49 | public function updateWithSync(Request $request, array $additionalData = [], array $options = [], array $target_additional_data = []) |
||||||
50 | { |
||||||
51 | $this->initDataForSync($request, $additionalData, $options, $target_additional_data); |
||||||
52 | $this->storeEditAllItems($request, 'update', $options); |
||||||
53 | $this->processAllRelationships($request, 'update', '', '', $options); |
||||||
54 | |||||||
55 | //Dispatch the update event |
||||||
56 | $this->fireModelEvent('updateWithSync'); |
||||||
57 | |||||||
58 | return $this->fresh(); |
||||||
59 | } |
||||||
60 | |||||||
61 | /** |
||||||
62 | * @return $this |
||||||
63 | */ |
||||||
64 | public function destroyWithSync() |
||||||
65 | { |
||||||
66 | //Get the relation info |
||||||
67 | $relations = $this->getMongoRelation(); |
||||||
0 ignored issues
–
show
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
![]() |
|||||||
68 | //Process all relationships |
||||||
69 | foreach ($relations as $method => $relation) { |
||||||
70 | //Get Relation Save Mode |
||||||
71 | $type = $relation['type']; |
||||||
72 | $hasTarget = hasTarget($relation); |
||||||
73 | if ($hasTarget) { |
||||||
74 | $modelTarget = $relation['modelTarget']; |
||||||
75 | $methodOnTarget = $relation['methodOnTarget']; |
||||||
76 | $modelOnTarget = $relation['modelOnTarget']; |
||||||
0 ignored issues
–
show
|
|||||||
77 | $is_EO = is_EO($type); |
||||||
78 | $is_EM = is_EM($type); |
||||||
79 | $is_HO = is_HO($type); |
||||||
0 ignored issues
–
show
|
|||||||
80 | $is_HM = is_HM($type); |
||||||
0 ignored issues
–
show
|
|||||||
81 | $typeOnTarget = getTypeOnTarget($relation); |
||||||
82 | $is_EM_target = is_EM($typeOnTarget); |
||||||
83 | $is_EO_target = is_EO($typeOnTarget); |
||||||
84 | if ($is_EO || $is_EM) {//EmbedsOne Create - EmbedsMany Create |
||||||
85 | //Delete EmbedsMany or EmbedsOne on Target |
||||||
86 | $this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO, $is_EM, $is_EO_target, $is_EM_target); |
||||||
0 ignored issues
–
show
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
![]() |
|||||||
87 | } |
||||||
88 | //TODO: Need to be implemented |
||||||
89 | /* elseif ($is_HM) {//HasMany |
||||||
90 | } elseif ($is_HO) {//HasOne Create |
||||||
91 | }*/ |
||||||
92 | } |
||||||
93 | } |
||||||
94 | //Delete current object |
||||||
95 | $this->delete(); |
||||||
0 ignored issues
–
show
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
![]() |
|||||||
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 | * |
||||||
118 | * @throws Exception |
||||||
119 | */ |
||||||
120 | public function checkPropertyExistence($obj, string $EOkey, $method = '', $model = '') |
||||||
121 | { |
||||||
122 | if (! property_exists($obj, $EOkey)) { |
||||||
123 | $msg = 'Error - '.$EOkey.' attribute not found on obj '.json_encode($obj).' during save of model: '.$model.' and attribute: '.$method; |
||||||
124 | throw new Exception($msg); |
||||||
125 | } |
||||||
126 | } |
||||||
127 | |||||||
128 | /** |
||||||
129 | * @param $arr |
||||||
130 | * @param string $key |
||||||
131 | * |
||||||
132 | * @throws Exception |
||||||
133 | */ |
||||||
134 | public function checkArrayExistence($arr, string $key) |
||||||
135 | { |
||||||
136 | if (! Arr::has($arr, $key)) { |
||||||
137 | $msg = ('Error - '.$key.' attribute not found on obj '.json_encode($arr)); |
||||||
138 | throw new Exception($msg); |
||||||
139 | } |
||||||
140 | } |
||||||
141 | |||||||
142 | /** |
||||||
143 | * @param Request $request |
||||||
144 | * @param string $key |
||||||
145 | * |
||||||
146 | * @throws Exception |
||||||
147 | */ |
||||||
148 | private function checkRequestExistence(Request $request, string $key) |
||||||
149 | { |
||||||
150 | if (! $request->has($key)) { |
||||||
151 | $msg = ('Error - '.$key.' attribute not found in Request '.json_encode($request->all())); |
||||||
152 | throw new Exception($msg); |
||||||
153 | } |
||||||
154 | } |
||||||
155 | |||||||
156 | /** |
||||||
157 | * @param bool $request_has_key |
||||||
158 | * @param bool $hasTarget |
||||||
159 | * @return bool |
||||||
160 | */ |
||||||
161 | public function getIsSkippable($request_has_key, $hasTarget = false) |
||||||
162 | { |
||||||
163 | return ! $request_has_key && $this->getHasPartialRequest() && ! $hasTarget; |
||||||
164 | } |
||||||
165 | |||||||
166 | /** |
||||||
167 | * @return bool |
||||||
168 | */ |
||||||
169 | public function getHasPartialRequest() |
||||||
170 | { |
||||||
171 | return $this->has_partial_request; |
||||||
172 | } |
||||||
173 | |||||||
174 | public function setHasPartialRequest(): void |
||||||
175 | { |
||||||
176 | $this->has_partial_request = $this->getOptionValue( |
||||||
177 | $this->getOptions(), |
||||||
178 | 'request_type' |
||||||
179 | ) == 'partial'; |
||||||
180 | } |
||||||
181 | |||||||
182 | /** |
||||||
183 | * @param string $modelTarget |
||||||
184 | * @param stdClass $obj |
||||||
185 | * @return MDModel|null |
||||||
0 ignored issues
–
show
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||||
186 | * |
||||||
187 | * @throws Exception |
||||||
188 | */ |
||||||
189 | private function getModelTobeSync(string $modelTarget, stdClass $obj) |
||||||
190 | { |
||||||
191 | $this->checkPropertyExistence($obj, 'ref_id'); |
||||||
192 | $target_id = $obj->ref_id; |
||||||
193 | |||||||
194 | //Init the Target Model |
||||||
195 | $modelToBeSync = new $modelTarget; |
||||||
196 | |||||||
197 | return $modelToBeSync->find($target_id); |
||||||
198 | } |
||||||
199 | |||||||
200 | /** |
||||||
201 | * @param string $key |
||||||
202 | * @param Request $request |
||||||
203 | * @return mixed |
||||||
204 | * |
||||||
205 | * @throws Exception |
||||||
206 | */ |
||||||
207 | private function getRelationshipRequest(string $key, Request $request) |
||||||
208 | { |
||||||
209 | $this->checkRequestExistence( |
||||||
210 | $request, |
||||||
211 | $key |
||||||
212 | ); |
||||||
213 | |||||||
214 | return $request->input($key); |
||||||
215 | } |
||||||
216 | |||||||
217 | /** |
||||||
218 | * @return Request |
||||||
219 | */ |
||||||
220 | public function getRequest() |
||||||
221 | { |
||||||
222 | return $this->request; |
||||||
223 | } |
||||||
224 | |||||||
225 | /** |
||||||
226 | * @param Request $request |
||||||
227 | * @param array $additionalData |
||||||
228 | */ |
||||||
229 | public function setRequest(Request $request, array $additionalData): void |
||||||
230 | { |
||||||
231 | $request = $request->merge($additionalData); |
||||||
232 | $this->request = $request; |
||||||
233 | } |
||||||
234 | |||||||
235 | /** |
||||||
236 | * @return Request |
||||||
237 | */ |
||||||
238 | public function getPartialGeneratedRequest() |
||||||
239 | { |
||||||
240 | return $this->partial_generated_request; |
||||||
241 | } |
||||||
242 | |||||||
243 | /** |
||||||
244 | * @param array $arr |
||||||
245 | */ |
||||||
246 | public function setPartialGeneratedRequest(array $arr): void |
||||||
247 | { |
||||||
248 | $partial_generated_request = new Request; |
||||||
249 | $partial_generated_request->merge($arr); |
||||||
250 | |||||||
251 | $this->partial_generated_request = $partial_generated_request; |
||||||
252 | } |
||||||
253 | |||||||
254 | /** |
||||||
255 | * @return array |
||||||
256 | */ |
||||||
257 | public function getOptions() |
||||||
258 | { |
||||||
259 | return $this->options; |
||||||
260 | } |
||||||
261 | |||||||
262 | /** |
||||||
263 | * @param array $options |
||||||
264 | */ |
||||||
265 | public function setOptions(array $options): void |
||||||
266 | { |
||||||
267 | $this->options = $options; |
||||||
268 | } |
||||||
269 | |||||||
270 | /** |
||||||
271 | * @return array |
||||||
272 | */ |
||||||
273 | public function getTargetAdditionalData() |
||||||
274 | { |
||||||
275 | return $this->target_additional_data; |
||||||
276 | } |
||||||
277 | |||||||
278 | /** |
||||||
279 | * @param array $target_additional_data |
||||||
280 | */ |
||||||
281 | public function setTargetAdditionalData($target_additional_data): void |
||||||
282 | { |
||||||
283 | $this->target_additional_data = $target_additional_data; |
||||||
284 | } |
||||||
285 | |||||||
286 | /** |
||||||
287 | * @param Request $request |
||||||
288 | * @param array $additionalData |
||||||
289 | * @param array $options |
||||||
290 | * @param array $target_additional_data |
||||||
291 | */ |
||||||
292 | public function initDataForSync(Request $request, array $additionalData, array $options, array $target_additional_data) |
||||||
293 | { |
||||||
294 | $this->setRequest($request, $additionalData); |
||||||
295 | $this->setTargetAdditionalData($target_additional_data); |
||||||
296 | $this->setOptions($options); |
||||||
297 | $this->setHasPartialRequest(); |
||||||
298 | } |
||||||
299 | } |
||||||
300 |