Total Complexity | 42 |
Total Lines | 638 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Standard |
||
24 | extends \Aimeos\Admin\JsonAdm\Base |
||
25 | implements \Aimeos\Admin\JsonAdm\Common\Iface |
||
26 | { |
||
27 | /** |
||
28 | * Deletes the resource or the resource list |
||
29 | * |
||
30 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
31 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
32 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
33 | */ |
||
34 | public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
35 | { |
||
36 | $view = $this->view(); |
||
37 | |||
38 | try |
||
39 | { |
||
40 | $response = $this->deleteItems( $view, $request, $response ); |
||
41 | $status = 200; |
||
42 | } |
||
43 | catch( \Aimeos\Admin\JsonAdm\Exception $e ) |
||
44 | { |
||
45 | $status = $e->getCode(); |
||
46 | $view->errors = array( array( |
||
47 | 'title' => $this->context()->translate( 'admin/jsonadm', $e->getMessage() ), |
||
48 | 'detail' => $e->getTraceAsString(), |
||
49 | ) ); |
||
50 | } |
||
51 | catch( \Aimeos\MShop\Exception $e ) |
||
52 | { |
||
53 | $status = 404; |
||
54 | $view->errors = array( array( |
||
55 | 'title' => $this->context()->translate( 'mshop', $e->getMessage() ), |
||
56 | 'detail' => $e->getTraceAsString(), |
||
57 | ) ); |
||
58 | } |
||
59 | catch( \Exception $e ) |
||
60 | { |
||
61 | $status = 500; |
||
62 | $view->errors = array( array( |
||
63 | 'title' => $e->getMessage(), |
||
64 | 'detail' => $e->getTraceAsString(), |
||
65 | ) ); |
||
66 | } |
||
67 | |||
68 | /** admin/jsonadm/template-delete |
||
69 | * Relative path to the JSON API template for DELETE requests |
||
70 | * |
||
71 | * The template file contains the code and processing instructions |
||
72 | * to generate the result shown in the JSON API body. The |
||
73 | * configuration string is the path to the template file relative |
||
74 | * to the templates directory (usually in templates/admin/jsonadm). |
||
75 | * |
||
76 | * You can overwrite the template file configuration in extensions and |
||
77 | * provide alternative templates. These alternative templates should be |
||
78 | * named like the default one but with the string "standard" replaced by |
||
79 | * an unique name. You may use the name of your project for this. If |
||
80 | * you've implemented an alternative client class as well, "standard" |
||
81 | * should be replaced by the name of the new class. |
||
82 | * |
||
83 | * @param string Relative path to the template creating the body for the DELETE method of the JSON API |
||
84 | * @since 2015.12 |
||
85 | * @category Developer |
||
86 | * @see admin/jsonadm/template-aggregate |
||
87 | * @see admin/jsonadm/template-get |
||
88 | * @see admin/jsonadm/template-patch |
||
89 | * @see admin/jsonadm/template-post |
||
90 | * @see admin/jsonadm/template-put |
||
91 | * @see admin/jsonadm/template-options |
||
92 | */ |
||
93 | $tplconf = 'admin/jsonadm/template-delete'; |
||
94 | $default = 'delete'; |
||
95 | |||
96 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
97 | |||
98 | return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' ) |
||
99 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
100 | ->withStatus( $status ); |
||
|
|||
101 | } |
||
102 | |||
103 | |||
104 | /** |
||
105 | * Returns the requested resource or the resource list |
||
106 | * |
||
107 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
108 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
109 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
110 | */ |
||
111 | public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
112 | { |
||
113 | $view = $this->view(); |
||
114 | |||
115 | try |
||
116 | { |
||
117 | $response = $this->getItems( $view, $request, $response ); |
||
118 | $status = 200; |
||
119 | } |
||
120 | catch( \Aimeos\MShop\Exception $e ) |
||
121 | { |
||
122 | $status = 404; |
||
123 | $view->errors = array( array( |
||
124 | 'title' => $this->context()->translate( 'mshop', $e->getMessage() ), |
||
125 | 'detail' => $e->getTraceAsString(), |
||
126 | ) ); |
||
127 | } |
||
128 | catch( \Exception $e ) |
||
129 | { |
||
130 | $status = 500; |
||
131 | $view->errors = array( array( |
||
132 | 'title' => $e->getMessage(), |
||
133 | 'detail' => $e->getTraceAsString(), |
||
134 | ) ); |
||
135 | } |
||
136 | |||
137 | if( $view->param( 'aggregate' ) !== null ) |
||
138 | { |
||
139 | /** admin/jsonadm/template-aggregate |
||
140 | * Relative path to the JSON API template for GET aggregate requests |
||
141 | * |
||
142 | * The template file contains the code and processing instructions |
||
143 | * to generate the result shown in the JSON API body. The |
||
144 | * configuration string is the path to the template file relative |
||
145 | * to the templates directory (usually in templates/admin/jsonadm). |
||
146 | * |
||
147 | * You can overwrite the template file configuration in extensions and |
||
148 | * provide alternative templates. These alternative templates should be |
||
149 | * named like the default one but with the string "standard" replaced by |
||
150 | * an unique name. You may use the name of your project for this. If |
||
151 | * you've implemented an alternative client class as well, "standard" |
||
152 | * should be replaced by the name of the new class. |
||
153 | * |
||
154 | * @param string Relative path to the template creating the body for the GET aggregate request of the JSON API |
||
155 | * @since 2016.07 |
||
156 | * @category Developer |
||
157 | * @see admin/jsonadm/template-delete |
||
158 | * @see admin/jsonadm/template-get |
||
159 | * @see admin/jsonadm/template-patch |
||
160 | * @see admin/jsonadm/template-post |
||
161 | * @see admin/jsonadm/template-put |
||
162 | * @see admin/jsonadm/template-options |
||
163 | */ |
||
164 | $tplconf = 'admin/jsonadm/template-aggregate'; |
||
165 | $default = 'aggregate'; |
||
166 | } |
||
167 | else |
||
168 | { |
||
169 | /** admin/jsonadm/template-get |
||
170 | * Relative path to the JSON API template for GET requests |
||
171 | * |
||
172 | * The template file contains the code and processing instructions |
||
173 | * to generate the result shown in the JSON API body. The |
||
174 | * configuration string is the path to the template file relative |
||
175 | * to the templates directory (usually in templates/admin/jsonadm). |
||
176 | * |
||
177 | * You can overwrite the template file configuration in extensions and |
||
178 | * provide alternative templates. These alternative templates should be |
||
179 | * named like the default one but with the string "standard" replaced by |
||
180 | * an unique name. You may use the name of your project for this. If |
||
181 | * you've implemented an alternative client class as well, "standard" |
||
182 | * should be replaced by the name of the new class. |
||
183 | * |
||
184 | * @param string Relative path to the template creating the body for the GET method of the JSON API |
||
185 | * @since 2015.12 |
||
186 | * @category Developer |
||
187 | * @see admin/jsonadm/template-aggregate |
||
188 | * @see admin/jsonadm/template-delete |
||
189 | * @see admin/jsonadm/template-patch |
||
190 | * @see admin/jsonadm/template-post |
||
191 | * @see admin/jsonadm/template-put |
||
192 | * @see admin/jsonadm/template-options |
||
193 | */ |
||
194 | $tplconf = 'admin/jsonadm/template-get'; |
||
195 | $default = 'get'; |
||
196 | } |
||
197 | |||
198 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
199 | |||
200 | return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' ) |
||
201 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
202 | ->withStatus( $status ); |
||
203 | } |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Updates the resource or the resource list partitially |
||
208 | * |
||
209 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
210 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
211 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
212 | */ |
||
213 | public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
214 | { |
||
215 | $view = $this->view(); |
||
216 | |||
217 | try |
||
218 | { |
||
219 | $response = $this->patchItems( $view, $request, $response ); |
||
220 | $status = 200; |
||
221 | } |
||
222 | catch( \Aimeos\Admin\JsonAdm\Exception $e ) |
||
223 | { |
||
224 | $status = $e->getCode(); |
||
225 | $view->errors = array( array( |
||
226 | 'title' => $this->context()->translate( 'admin/jsonadm', $e->getMessage() ), |
||
227 | 'detail' => $e->getTraceAsString(), |
||
228 | ) ); |
||
229 | } |
||
230 | catch( \Aimeos\MShop\Exception $e ) |
||
231 | { |
||
232 | $status = 404; |
||
233 | $view->errors = array( array( |
||
234 | 'title' => $this->context()->translate( 'mshop', $e->getMessage() ), |
||
235 | 'detail' => $e->getTraceAsString(), |
||
236 | ) ); |
||
237 | } |
||
238 | catch( \Exception $e ) |
||
239 | { |
||
240 | $status = 500; |
||
241 | $view->errors = array( array( |
||
242 | 'title' => $e->getMessage(), |
||
243 | 'detail' => $e->getTraceAsString(), |
||
244 | ) ); |
||
245 | } |
||
246 | |||
247 | /** admin/jsonadm/template-patch |
||
248 | * Relative path to the JSON API template for PATCH requests |
||
249 | * |
||
250 | * The template file contains the code and processing instructions |
||
251 | * to generate the result shown in the JSON API body. The |
||
252 | * configuration string is the path to the template file relative |
||
253 | * to the templates directory (usually in templates/admin/jsonadm). |
||
254 | * |
||
255 | * You can overwrite the template file configuration in extensions and |
||
256 | * provide alternative templates. These alternative templates should be |
||
257 | * named like the default one but with the string "standard" replaced by |
||
258 | * an unique name. You may use the name of your project for this. If |
||
259 | * you've implemented an alternative client class as well, "standard" |
||
260 | * should be replaced by the name of the new class. |
||
261 | * |
||
262 | * @param string Relative path to the template creating the body for the PATCH method of the JSON API |
||
263 | * @since 2015.12 |
||
264 | * @category Developer |
||
265 | * @see admin/jsonadm/template-aggregate |
||
266 | * @see admin/jsonadm/template-get |
||
267 | * @see admin/jsonadm/template-post |
||
268 | * @see admin/jsonadm/template-delete |
||
269 | * @see admin/jsonadm/template-put |
||
270 | * @see admin/jsonadm/template-options |
||
271 | */ |
||
272 | $tplconf = 'admin/jsonadm/template-patch'; |
||
273 | $default = 'patch'; |
||
274 | |||
275 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
276 | |||
277 | return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' ) |
||
278 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
279 | ->withStatus( $status ); |
||
280 | } |
||
281 | |||
282 | |||
283 | /** |
||
284 | * Creates or updates the resource or the resource list |
||
285 | * |
||
286 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
287 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
288 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
289 | */ |
||
290 | public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
291 | { |
||
292 | $view = $this->view(); |
||
293 | |||
294 | try |
||
295 | { |
||
296 | $response = $this->postItems( $view, $request, $response ); |
||
297 | $status = 201; |
||
298 | } |
||
299 | catch( \Aimeos\Admin\JsonAdm\Exception $e ) |
||
300 | { |
||
301 | $status = $e->getCode(); |
||
302 | $view->errors = array( array( |
||
303 | 'title' => $this->context()->translate( 'admin/jsonadm', $e->getMessage() ), |
||
304 | 'detail' => $e->getTraceAsString(), |
||
305 | ) ); |
||
306 | } |
||
307 | catch( \Aimeos\MShop\Exception $e ) |
||
308 | { |
||
309 | $status = 404; |
||
310 | $view->errors = array( array( |
||
311 | 'title' => $this->context()->translate( 'mshop', $e->getMessage() ), |
||
312 | 'detail' => $e->getTraceAsString(), |
||
313 | ) ); |
||
314 | } |
||
315 | catch( \Exception $e ) |
||
316 | { |
||
317 | $status = 500; |
||
318 | $view->errors = array( array( |
||
319 | 'title' => $e->getMessage(), |
||
320 | 'detail' => $e->getTraceAsString(), |
||
321 | ) ); |
||
322 | } |
||
323 | |||
324 | /** admin/jsonadm/template-post |
||
325 | * Relative path to the JSON API template for POST requests |
||
326 | * |
||
327 | * The template file contains the code and processing instructions |
||
328 | * to generate the result shown in the JSON API body. The |
||
329 | * configuration string is the path to the template file relative |
||
330 | * to the templates directory (usually in templates/admin/jsonadm). |
||
331 | * |
||
332 | * You can overwrite the template file configuration in extensions and |
||
333 | * provide alternative templates. These alternative templates should be |
||
334 | * named like the default one but with the string "standard" replaced by |
||
335 | * an unique name. You may use the name of your project for this. If |
||
336 | * you've implemented an alternative client class as well, "standard" |
||
337 | * should be replaced by the name of the new class. |
||
338 | * |
||
339 | * @param string Relative path to the template creating the body for the POST method of the JSON API |
||
340 | * @since 2015.12 |
||
341 | * @category Developer |
||
342 | * @see admin/jsonadm/template-aggregate |
||
343 | * @see admin/jsonadm/template-get |
||
344 | * @see admin/jsonadm/template-patch |
||
345 | * @see admin/jsonadm/template-delete |
||
346 | * @see admin/jsonadm/template-put |
||
347 | * @see admin/jsonadm/template-options |
||
348 | */ |
||
349 | $tplconf = 'admin/jsonadm/template-post'; |
||
350 | $default = 'post'; |
||
351 | |||
352 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
353 | |||
354 | return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' ) |
||
355 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
356 | ->withStatus( $status ); |
||
357 | } |
||
358 | |||
359 | |||
360 | /** |
||
361 | * Creates or updates the resource or the resource list |
||
362 | * |
||
363 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
364 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
365 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
366 | */ |
||
367 | public function put( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
409 | } |
||
410 | |||
411 | |||
412 | /** |
||
413 | * Returns the available REST verbs and the available resources |
||
414 | * |
||
415 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
416 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
417 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
418 | */ |
||
419 | public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
420 | { |
||
421 | $context = $this->context(); |
||
422 | $view = $this->view(); |
||
423 | |||
424 | try |
||
425 | { |
||
426 | $resources = $attributes = []; |
||
427 | |||
428 | foreach( $this->getDomains( $view ) as $domain ) |
||
429 | { |
||
430 | $manager = \Aimeos\MShop::create( $context, $domain ); |
||
431 | $attributes = array_merge( $attributes, $manager->getSearchAttributes( true ) ); |
||
432 | $resources[] = join( '/', $manager->type() ); |
||
433 | } |
||
434 | |||
435 | foreach( $this->getResources( $view ) as $resource ) { |
||
436 | $resources[] = $resource; |
||
437 | } |
||
438 | |||
439 | $view->resources = $this->getAllowedResources( $view, $resources ); |
||
440 | $view->attributes = $attributes; |
||
441 | |||
442 | $status = 200; |
||
443 | } |
||
444 | catch( \Aimeos\MShop\Exception $e ) |
||
445 | { |
||
446 | $status = 404; |
||
447 | $view->errors = array( array( |
||
448 | 'title' => $context->translate( 'mshop', $e->getMessage() ), |
||
449 | 'detail' => $e->getTraceAsString(), |
||
450 | ) ); |
||
451 | } |
||
452 | catch( \Exception $e ) |
||
453 | { |
||
454 | $status = 500; |
||
455 | $view->errors = array( array( |
||
456 | 'title' => $e->getMessage(), |
||
457 | 'detail' => $e->getTraceAsString(), |
||
458 | ) ); |
||
459 | } |
||
460 | |||
461 | /** admin/jsonadm/template-options |
||
462 | * Relative path to the JSON API template for OPTIONS requests |
||
463 | * |
||
464 | * The template file contains the code and processing instructions |
||
465 | * to generate the result shown in the JSON API body. The |
||
466 | * configuration string is the path to the template file relative |
||
467 | * to the templates directory (usually in templates/admin/jsonadm). |
||
468 | * |
||
469 | * You can overwrite the template file configuration in extensions and |
||
470 | * provide alternative templates. These alternative templates should be |
||
471 | * named like the default one but with the string "standard" replaced by |
||
472 | * an unique name. You may use the name of your project for this. If |
||
473 | * you've implemented an alternative client class as well, "standard" |
||
474 | * should be replaced by the name of the new class. |
||
475 | * |
||
476 | * @param string Relative path to the template creating the body for the OPTIONS method of the JSON API |
||
477 | * @since 2015.12 |
||
478 | * @category Developer |
||
479 | * @see admin/jsonadm/template-aggregate |
||
480 | * @see admin/jsonadm/template-delete |
||
481 | * @see admin/jsonadm/template-patch |
||
482 | * @see admin/jsonadm/template-post |
||
483 | * @see admin/jsonadm/template-get |
||
484 | * @see admin/jsonadm/template-put |
||
485 | */ |
||
486 | $tplconf = 'admin/jsonadm/template-options'; |
||
487 | $default = 'options'; |
||
488 | |||
489 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
490 | |||
491 | return $response->withHeader( 'Allow', 'DELETE,GET,PATCH,POST,OPTIONS' ) |
||
492 | ->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' ) |
||
493 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
494 | ->withStatus( $status ); |
||
495 | } |
||
496 | |||
497 | |||
498 | /** |
||
499 | * Deletes one or more items |
||
500 | * |
||
501 | * @param \Aimeos\Base\View\Iface $view View instance with "param" view helper |
||
502 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
503 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
504 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
505 | * @throws \Aimeos\Admin\JsonAdm\Exception If the request body is invalid |
||
506 | */ |
||
507 | protected function deleteItems( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
530 | } |
||
531 | |||
532 | |||
533 | /** |
||
534 | * Retrieves the item or items and adds the data to the view |
||
535 | * |
||
536 | * @param \Aimeos\Base\View\Iface $view View instance |
||
537 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
538 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
539 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
540 | */ |
||
541 | protected function getItems( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
573 | } |
||
574 | |||
575 | |||
576 | /** |
||
577 | * Saves new attributes for one or more items |
||
578 | * |
||
579 | * @param \Aimeos\Base\View\Iface $view View that will contain the "data" and "total" properties afterwards |
||
580 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
581 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
582 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
583 | * @throws \Aimeos\Admin\JsonAdm\Exception If "id" parameter isn't available or the body is invalid |
||
584 | */ |
||
585 | protected function patchItems( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
617 | } |
||
618 | |||
619 | |||
620 | /** |
||
621 | * Creates one or more new items |
||
622 | * |
||
623 | * @param \Aimeos\Base\View\Iface $view View that will contain the "data" and "total" properties afterwards |
||
624 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
625 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
626 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
627 | */ |
||
628 | protected function postItems( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
661 | } |
||
662 | } |
||
663 |