Conditions | 10 |
Paths | 245 |
Total Lines | 95 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
147 | public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
148 | { |
||
149 | $view = $this->view(); |
||
150 | |||
151 | try |
||
152 | { |
||
153 | $ref = $view->param( 'include', ['media', 'price', 'text'] ); |
||
154 | |||
155 | if( is_string( $ref ) ) { |
||
156 | $ref = explode( ',', str_replace( '.', '/', $ref ) ); |
||
157 | } |
||
158 | |||
159 | $cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'service' )->uses( $ref ) |
||
160 | ->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 100 ) ); |
||
161 | |||
162 | $basketCntl = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' ); |
||
163 | $basket = $basketCntl->get(); |
||
164 | |||
165 | if( ( $id = $view->param( 'id' ) ) != '' ) |
||
166 | { |
||
167 | $provider = $cntl->getProvider( $id ); |
||
168 | |||
169 | if( $provider->isAvailable( $basket ) === true ) |
||
170 | { |
||
171 | $view->prices = map( [$id => $provider->calcPrice( $basket )] ); |
||
172 | $view->attributes = [$id => $provider->getConfigFE( $basket )]; |
||
173 | $view->items = $provider->getServiceItem(); |
||
174 | $view->total = 1; |
||
175 | } |
||
176 | } |
||
177 | else |
||
178 | { |
||
179 | $attributes = []; |
||
180 | $items = map(); |
||
181 | $prices = map(); |
||
182 | $cntl->type( $view->param( 'filter/cs_type' ) ); |
||
183 | |||
184 | foreach( $cntl->getProviders() as $id => $provider ) |
||
185 | { |
||
186 | if( $provider->isAvailable( $basket ) === true ) |
||
187 | { |
||
188 | $attributes[$id] = $provider->getConfigFE( $basket ); |
||
189 | $prices[$id] = $provider->calcPrice( $basket ); |
||
190 | $items[$id] = $provider->getServiceItem(); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | $view->attributes = $attributes; |
||
195 | $view->prices = $prices; |
||
196 | $view->items = $items; |
||
197 | $view->total = count( $items ); |
||
198 | } |
||
199 | |||
200 | $status = 200; |
||
201 | } |
||
202 | catch( \Aimeos\MShop\Exception $e ) |
||
203 | { |
||
204 | $status = 404; |
||
205 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
206 | } |
||
207 | catch( \Exception $e ) |
||
208 | { |
||
209 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
210 | $view->errors = $this->getErrorDetails( $e ); |
||
211 | } |
||
212 | |||
213 | /** client/jsonapi/service/template |
||
214 | * Relative path to the service JSON API template |
||
215 | * |
||
216 | * The template file contains the code and processing instructions |
||
217 | * to generate the result shown in the JSON API body. The |
||
218 | * configuration string is the path to the template file relative |
||
219 | * to the templates directory (usually in templates/client/jsonapi). |
||
220 | * |
||
221 | * You can overwrite the template file configuration in extensions and |
||
222 | * provide alternative templates. These alternative templates should be |
||
223 | * named like the default one but with the string "standard" replaced by |
||
224 | * an unique name. You may use the name of your project for this. If |
||
225 | * you've implemented an alternative client class as well, "standard" |
||
226 | * should be replaced by the name of the new class. |
||
227 | * |
||
228 | * @param string Relative path to the template creating the body for the GET method of the JSON API |
||
229 | * @since 2017.03 |
||
230 | * @category Developer |
||
231 | */ |
||
232 | $tplconf = 'client/jsonapi/service/template'; |
||
233 | $default = 'service/standard'; |
||
234 | |||
235 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
236 | |||
237 | return $response->withHeader( 'Allow', 'GET,OPTIONS' ) |
||
238 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
239 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
240 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
241 | ->withStatus( $status ); |
||
|
|||
242 | } |
||
275 |