| Conditions | 10 |
| Paths | 245 |
| Total Lines | 93 |
| Code Lines | 44 |
| 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 |
||
| 34 | public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 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::create( $this->getContext(), 'service' )->uses( $ref ); |
||
| 47 | $basketCntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'basket' ); |
||
| 48 | $basket = $basketCntl->get(); |
||
| 49 | |||
| 50 | if( ( $id = $view->param( 'id' ) ) != '' ) |
||
| 51 | { |
||
| 52 | $provider = $cntl->getProvider( $id ); |
||
| 53 | |||
| 54 | if( $provider->isAvailable( $basket ) === true ) |
||
| 55 | { |
||
| 56 | $view->prices = map( [$id => $provider->calcPrice( $basket )] ); |
||
| 57 | $view->attributes = [$id => $provider->getConfigFE( $basket )]; |
||
| 58 | $view->items = $provider->getServiceItem(); |
||
| 59 | $view->total = 1; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | else |
||
| 63 | { |
||
| 64 | $attributes = []; |
||
| 65 | $items = map(); |
||
| 66 | $prices = map(); |
||
| 67 | $cntl->type( $view->param( 'filter/cs_type' ) ); |
||
| 68 | |||
| 69 | foreach( $cntl->getProviders() as $id => $provider ) |
||
| 70 | { |
||
| 71 | if( $provider->isAvailable( $basket ) === true ) |
||
| 72 | { |
||
| 73 | $attributes[$id] = $provider->getConfigFE( $basket ); |
||
| 74 | $prices[$id] = $provider->calcPrice( $basket ); |
||
| 75 | $items[$id] = $provider->getServiceItem(); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | $view->attributes = $attributes; |
||
| 80 | $view->prices = $prices; |
||
| 81 | $view->items = $items; |
||
| 82 | $view->total = count( $items ); |
||
| 83 | } |
||
| 84 | |||
| 85 | $status = 200; |
||
| 86 | } |
||
| 87 | catch( \Aimeos\MShop\Exception $e ) |
||
| 88 | { |
||
| 89 | $status = 404; |
||
| 90 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 91 | } |
||
| 92 | catch( \Exception $e ) |
||
| 93 | { |
||
| 94 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 95 | $view->errors = $this->getErrorDetails( $e ); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** client/jsonapi/service/standard/template |
||
| 99 | * Relative path to the service JSON API template |
||
| 100 | * |
||
| 101 | * The template file contains the code and processing instructions |
||
| 102 | * to generate the result shown in the JSON API body. The |
||
| 103 | * configuration string is the path to the template file relative |
||
| 104 | * to the templates directory (usually in client/jsonapi/templates). |
||
| 105 | * |
||
| 106 | * You can overwrite the template file configuration in extensions and |
||
| 107 | * provide alternative templates. These alternative templates should be |
||
| 108 | * named like the default one but with the string "standard" replaced by |
||
| 109 | * an unique name. You may use the name of your project for this. If |
||
| 110 | * you've implemented an alternative client class as well, "standard" |
||
| 111 | * should be replaced by the name of the new class. |
||
| 112 | * |
||
| 113 | * @param string Relative path to the template creating the body for the GET method of the JSON API |
||
| 114 | * @since 2017.03 |
||
| 115 | * @category Developer |
||
| 116 | */ |
||
| 117 | $tplconf = 'client/jsonapi/service/standard/template'; |
||
| 118 | $default = 'service/standard'; |
||
| 119 | |||
| 120 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
| 121 | |||
| 122 | return $response->withHeader( 'Allow', 'GET,OPTIONS' ) |
||
| 123 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
| 124 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
| 125 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
| 126 | ->withStatus( $status ); |
||
| 127 | } |
||
| 160 |