Conditions | 8 |
Paths | 96 |
Total Lines | 97 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function get( ServerRequestInterface $request, ResponseInterface $response ) |
||
35 | { |
||
36 | $view = $this->getView(); |
||
37 | |||
38 | try |
||
39 | { |
||
40 | $ref = $view->param( 'include', ['media', 'price', 'text'] ); |
||
41 | |||
42 | if( is_string( $ref ) ) { |
||
43 | $ref = explode( ',', $ref ); |
||
44 | } |
||
45 | |||
46 | $cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'service' ); |
||
47 | $basketCntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'basket' ); |
||
48 | $basket = $basketCntl->get(); |
||
49 | |||
50 | if( ( $id = $view->param( 'id' ) ) != '' ) |
||
51 | { |
||
52 | $provider = $cntl->getProvider( $id, $ref ); |
||
53 | |||
54 | if( $provider->isAvailable( $basket ) === true ) |
||
55 | { |
||
56 | $view->attributes = [$id => $provider->getConfigFE( $basket )]; |
||
57 | $view->prices = [$id => $provider->calcPrice( $basket )]; |
||
58 | $view->items = $provider->getServiceItem(); |
||
59 | $view->total = 1; |
||
60 | } |
||
61 | } |
||
62 | else |
||
63 | { |
||
64 | $attributes = $prices = $items = []; |
||
65 | $type = $view->param( 'filter/cs_type' ); |
||
66 | |||
67 | foreach( $cntl->getProviders( $type, $ref ) as $id => $provider ) |
||
68 | { |
||
69 | if( $provider->isAvailable( $basket ) === true ) |
||
70 | { |
||
71 | $attributes[$id] = $provider->getConfigFE( $basket ); |
||
72 | $prices[$id] = $provider->calcPrice( $basket ); |
||
73 | $items[$id] = $provider->getServiceItem(); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | $view->attributes = $attributes; |
||
78 | $view->prices = $prices; |
||
79 | $view->items = $items; |
||
80 | $view->total = count( $items ); |
||
81 | } |
||
82 | |||
83 | $status = 200; |
||
84 | } |
||
85 | catch( \Aimeos\MShop\Exception $e ) |
||
86 | { |
||
87 | $status = 404; |
||
88 | $view->errors = array( array( |
||
89 | 'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ), |
||
90 | 'detail' => $e->getTraceAsString(), |
||
91 | ) ); |
||
92 | } |
||
93 | catch( \Exception $e ) |
||
94 | { |
||
95 | $status = 500; |
||
96 | $view->errors = array( array( |
||
97 | 'title' => $e->getMessage(), |
||
98 | 'detail' => $e->getTraceAsString(), |
||
99 | ) ); |
||
100 | } |
||
101 | |||
102 | /** client/jsonapi/service/standard/template |
||
103 | * Relative path to the service JSON API template |
||
104 | * |
||
105 | * The template file contains the code and processing instructions |
||
106 | * to generate the result shown in the JSON API body. The |
||
107 | * configuration string is the path to the template file relative |
||
108 | * to the templates directory (usually in client/jsonapi/templates). |
||
109 | * |
||
110 | * You can overwrite the template file configuration in extensions and |
||
111 | * provide alternative templates. These alternative templates should be |
||
112 | * named like the default one but with the string "standard" replaced by |
||
113 | * an unique name. You may use the name of your project for this. If |
||
114 | * you've implemented an alternative client class as well, "standard" |
||
115 | * should be replaced by the name of the new class. |
||
116 | * |
||
117 | * @param string Relative path to the template creating the body for the GET method of the JSON API |
||
118 | * @since 2017.03 |
||
119 | * @category Developer |
||
120 | */ |
||
121 | $tplconf = 'client/jsonapi/service/standard/template'; |
||
122 | $default = 'service/standard.php'; |
||
123 | |||
124 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
125 | |||
126 | return $response->withHeader( 'Allow', 'GET' ) |
||
127 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
128 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
129 | ->withStatus( $status ); |
||
130 | } |
||
131 | } |
||
132 |