| Total Complexity | 40 |
| Total Lines | 348 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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\Client\JsonApi\Base |
||
| 25 | implements \Aimeos\Client\JsonApi\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->getView(); |
||
| 37 | $context = $this->getContext(); |
||
| 38 | |||
| 39 | try |
||
| 40 | { |
||
| 41 | $body = (string) $request->getBody(); |
||
| 42 | $manager = \Aimeos\MShop::create( $context, 'review' ); |
||
| 43 | |||
| 44 | if( ( $relId = $view->param( 'relatedid' ) ) === null ) |
||
| 45 | { |
||
| 46 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
| 47 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 48 | } |
||
| 49 | |||
| 50 | if( !is_array( $payload->data ) ) { |
||
| 51 | $payload->data = [$payload->data]; |
||
| 52 | } |
||
| 53 | |||
| 54 | $ids = []; |
||
| 55 | |||
| 56 | foreach( $payload->data as $entry ) |
||
| 57 | { |
||
| 58 | if( !isset( $entry->id ) ) { |
||
| 59 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 ); |
||
| 60 | } |
||
| 61 | |||
| 62 | $ids[] = $entry->id; |
||
| 63 | } |
||
| 64 | |||
| 65 | $filter = $manager->filter()->add( [ |
||
| 66 | 'review.customerid' => $context->getUserId(), |
||
| 67 | 'review.id' => $ids |
||
| 68 | ] ); |
||
| 69 | |||
| 70 | $manager->delete( $manager->search( $filter )->toArray() ); |
||
| 71 | } |
||
| 72 | else |
||
| 73 | { |
||
| 74 | $item = $manager->get( $relId ); |
||
| 75 | |||
| 76 | if( $item->getCustomerId() !== $context->getUserId() ) { |
||
| 77 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Operation not allowed' ), 403 ); |
||
| 78 | } |
||
| 79 | |||
| 80 | $manager->delete( $item ); |
||
| 81 | } |
||
| 82 | |||
| 83 | $status = 200; |
||
| 84 | } |
||
| 85 | catch( \Aimeos\Controller\Frontend\Review\Exception $e ) |
||
| 86 | { |
||
| 87 | $status = 403; |
||
| 88 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 89 | } |
||
| 90 | catch( \Aimeos\MShop\Exception $e ) |
||
| 91 | { |
||
| 92 | $status = 404; |
||
| 93 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 94 | } |
||
| 95 | catch( \Exception $e ) |
||
| 96 | { |
||
| 97 | $status = $e->getCode() ?: 500; |
||
| 98 | $view->errors = $this->getErrorDetails( $e ); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $this->render( $response, $view, $status ); |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns the resource or the resource list |
||
| 107 | * |
||
| 108 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 109 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 110 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 111 | */ |
||
| 112 | public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 113 | { |
||
| 114 | $view = $this->getView(); |
||
| 115 | $context = $this->getContext(); |
||
| 116 | |||
| 117 | try |
||
| 118 | { |
||
| 119 | $manager = \Aimeos\MShop::create( $context, 'review' ); |
||
| 120 | |||
| 121 | if( ( $relId = $view->param( 'relatedid' ) ) == null ) |
||
| 122 | { |
||
| 123 | $total = 0; |
||
| 124 | $filter = $manager->filter()->add( 'review.customerid', '==', $context->getUserId() ); |
||
| 125 | $filter->add( $filter->parse( $view->param( 'filter', [] ) ) ?: [] ); |
||
| 126 | |||
| 127 | $view->items = $manager->search( $filter, [], $total ); |
||
| 128 | $view->total = $total; |
||
| 129 | } |
||
| 130 | else |
||
| 131 | { |
||
| 132 | $item = $manager->get( $relId ); |
||
| 133 | |||
| 134 | if( $item->getCustomerId() !== $context->getUserId() ) { |
||
| 135 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Item not found' ), 404 ); |
||
| 136 | } |
||
| 137 | |||
| 138 | $view->items = $item; |
||
| 139 | $view->total = 1; |
||
| 140 | } |
||
| 141 | |||
| 142 | $status = 200; |
||
| 143 | } |
||
| 144 | catch( \Aimeos\Controller\Frontend\Review\Exception $e ) |
||
| 145 | { |
||
| 146 | $status = 403; |
||
| 147 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 148 | } |
||
| 149 | catch( \Aimeos\MShop\Exception $e ) |
||
| 150 | { |
||
| 151 | $status = 404; |
||
| 152 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 153 | } |
||
| 154 | catch( \Exception $e ) |
||
| 155 | { |
||
| 156 | $status = $e->getCode() ?: 500; |
||
| 157 | $view->errors = $this->getErrorDetails( $e ); |
||
| 158 | } |
||
| 159 | |||
| 160 | return $this->render( $response, $view, $status ); |
||
| 161 | } |
||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * Updates the resource or the resource list partitially |
||
| 166 | * |
||
| 167 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 168 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 169 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 170 | */ |
||
| 171 | public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 172 | { |
||
| 173 | $view = $this->getView(); |
||
| 174 | $context = $this->getContext(); |
||
| 175 | |||
| 176 | try |
||
| 177 | { |
||
| 178 | $body = (string) $request->getBody(); |
||
| 179 | |||
| 180 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
||
| 181 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 182 | } |
||
| 183 | |||
| 184 | if( ( $id = $view->param( 'relatedid' ) ) === null ) { |
||
| 185 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Required "relatedid" is missing' ), 400 ); |
||
| 186 | } |
||
| 187 | |||
| 188 | $manager = \Aimeos\MShop::create( $context, 'review' ); |
||
| 189 | $filter = $manager->filter()->add( 'review.customerid', '==', $context->getUserId() ); |
||
| 190 | $item = $manager->get( $id, [], false, $filter ); |
||
|
|
|||
| 191 | |||
| 192 | $attr = (array) $payload->data->attributes; |
||
| 193 | unset( $attr['review.response'], $attr['review.status'] ); |
||
| 194 | |||
| 195 | $manager->save( $item->fromArray( $attr )->setCustomerId( $context->getUserId() ) ); |
||
| 196 | |||
| 197 | $view->items = $item; |
||
| 198 | $view->total = 1; |
||
| 199 | $status = 200; |
||
| 200 | } |
||
| 201 | catch( \Aimeos\Controller\Frontend\Review\Exception $e ) |
||
| 202 | { |
||
| 203 | $status = 403; |
||
| 204 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 205 | } |
||
| 206 | catch( \Aimeos\MShop\Exception $e ) |
||
| 207 | { |
||
| 208 | $status = 404; |
||
| 209 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 210 | } |
||
| 211 | catch( \Exception $e ) |
||
| 212 | { |
||
| 213 | $status = $e->getCode() ?: 500; |
||
| 214 | $view->errors = $this->getErrorDetails( $e ); |
||
| 215 | } |
||
| 216 | |||
| 217 | return $this->render( $response, $view, $status ); |
||
| 218 | } |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Creates or updates the resource or the resource list |
||
| 223 | * |
||
| 224 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 225 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 226 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 227 | */ |
||
| 228 | public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 229 | { |
||
| 230 | $view = $this->getView(); |
||
| 231 | $context = $this->getContext(); |
||
| 232 | |||
| 233 | try |
||
| 234 | { |
||
| 235 | $body = (string) $request->getBody(); |
||
| 236 | |||
| 237 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
| 238 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 239 | } |
||
| 240 | |||
| 241 | if( !is_array( $payload->data ) ) { |
||
| 242 | $payload->data = [$payload->data]; |
||
| 243 | } |
||
| 244 | |||
| 245 | $items = []; |
||
| 246 | $manager = \Aimeos\MShop::create( $context, 'review' ); |
||
| 247 | |||
| 248 | foreach( $payload->data as $entry ) |
||
| 249 | { |
||
| 250 | if( !isset( $entry->attributes ) ) { |
||
| 251 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing', 400 ) ); |
||
| 252 | } |
||
| 253 | |||
| 254 | $attr = (array) $entry->attributes; |
||
| 255 | unset( $attr['review.response'], $attr['review.status'] ); |
||
| 256 | |||
| 257 | $items[] = $manager->create()->fromArray( $attr )->setCustomerId( $context->getUserId() ); |
||
| 258 | } |
||
| 259 | |||
| 260 | $view->items = map( $manager->save( $items ) ); |
||
| 261 | $view->total = count( $view->items ); |
||
| 262 | $status = 201; |
||
| 263 | } |
||
| 264 | catch( \Aimeos\Controller\Frontend\Review\Exception $e ) |
||
| 265 | { |
||
| 266 | $status = 403; |
||
| 267 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 268 | } |
||
| 269 | catch( \Aimeos\MShop\Exception $e ) |
||
| 270 | { |
||
| 271 | $status = 404; |
||
| 272 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 273 | } |
||
| 274 | catch( \Exception $e ) |
||
| 275 | { |
||
| 276 | $status = $e->getCode() ?: 500; |
||
| 277 | $view->errors = $this->getErrorDetails( $e ); |
||
| 278 | } |
||
| 279 | |||
| 280 | return $this->render( $response, $view, $status ); |
||
| 281 | } |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns the available REST verbs and the available parameters |
||
| 286 | * |
||
| 287 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 288 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 289 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 290 | */ |
||
| 291 | public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 292 | { |
||
| 293 | $view = $this->getView(); |
||
| 294 | |||
| 295 | $view->attributes = [ |
||
| 296 | 'review.domain' => [ |
||
| 297 | 'label' => 'Domain of the reviewed item', |
||
| 298 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 299 | ], |
||
| 300 | 'review.refid' => [ |
||
| 301 | 'label' => 'ID of the reviewd item', |
||
| 302 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 303 | ], |
||
| 304 | 'review.rating' => [ |
||
| 305 | 'label' => 'Rating from 0 to 5', |
||
| 306 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 307 | ], |
||
| 308 | 'review.name' => [ |
||
| 309 | 'label' => 'Name of the reviewer', |
||
| 310 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 311 | ], |
||
| 312 | 'review.comment' => [ |
||
| 313 | 'label' => 'Comment for the rating', |
||
| 314 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 315 | ], |
||
| 316 | ]; |
||
| 317 | |||
| 318 | $tplconf = 'client/jsonapi/standard/template-options'; |
||
| 319 | $default = 'options-standard'; |
||
| 320 | |||
| 321 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
| 322 | |||
| 323 | return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
||
| 324 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
| 325 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
| 326 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
| 327 | ->withStatus( 200 ); |
||
| 328 | } |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * Returns the response object with the rendered header and body |
||
| 333 | * |
||
| 334 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 335 | * @param \Aimeos\MW\View\Iface $view View instance |
||
| 336 | * @param int $status HTTP status code |
||
| 337 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 338 | */ |
||
| 339 | protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface |
||
| 371 | } |
||
| 372 | } |
||
| 373 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.