| Total Complexity | 41 |
| Total Lines | 456 |
| Duplicated Lines | 0 % |
| Changes | 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\Basket\Base |
||
| 25 | implements \Aimeos\Client\JsonApi\Iface |
||
| 26 | { |
||
| 27 | /** client/jsonapi/basket/address/name |
||
| 28 | * Class name of the used basket/address client implementation |
||
| 29 | * |
||
| 30 | * Each default JSON API client can be replace by an alternative imlementation. |
||
| 31 | * To use this implementation, you have to set the last part of the class |
||
| 32 | * name as configuration value so the client factory knows which class it |
||
| 33 | * has to instantiate. |
||
| 34 | * |
||
| 35 | * For example, if the name of the default class is |
||
| 36 | * |
||
| 37 | * \Aimeos\Client\JsonApi\Basket\Address\Standard |
||
| 38 | * |
||
| 39 | * and you want to replace it with your own version named |
||
| 40 | * |
||
| 41 | * \Aimeos\Client\JsonApi\Basket\Address\Mybasket/address |
||
| 42 | * |
||
| 43 | * then you have to set the this configuration option: |
||
| 44 | * |
||
| 45 | * client/jsonapi/basket/address/name = Mybasket/address |
||
| 46 | * |
||
| 47 | * The value is the last part of your own class name and it's case sensitive, |
||
| 48 | * so take care that the configuration value is exactly named like the last |
||
| 49 | * part of the class name. |
||
| 50 | * |
||
| 51 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
| 52 | * characters are possible! You should always start the last part of the class |
||
| 53 | * name with an upper case character and continue only with lower case characters |
||
| 54 | * or numbers. Avoid chamel case names like "MyAddress"! |
||
| 55 | * |
||
| 56 | * @param string Last part of the class name |
||
| 57 | * @since 2017.03 |
||
| 58 | * @category Developer |
||
| 59 | */ |
||
| 60 | |||
| 61 | /** client/jsonapi/basket/address/decorators/excludes |
||
| 62 | * Excludes decorators added by the "common" option from the JSON API clients |
||
| 63 | * |
||
| 64 | * Decorators extend the functionality of a class by adding new aspects |
||
| 65 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 66 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 67 | * modify what is returned to the caller. |
||
| 68 | * |
||
| 69 | * This option allows you to remove a decorator added via |
||
| 70 | * "client/jsonapi/common/decorators/default" before they are wrapped |
||
| 71 | * around the JsonApi client. |
||
| 72 | * |
||
| 73 | * client/jsonapi/decorators/excludes = array( 'decorator1' ) |
||
| 74 | * |
||
| 75 | * This would remove the decorator named "decorator1" from the list of |
||
| 76 | * common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via |
||
| 77 | * "client/jsonapi/common/decorators/default" for the JSON API client. |
||
| 78 | * |
||
| 79 | * @param array List of decorator names |
||
| 80 | * @since 2017.07 |
||
| 81 | * @category Developer |
||
| 82 | * @see client/jsonapi/common/decorators/default |
||
| 83 | * @see client/jsonapi/basket/address/decorators/global |
||
| 84 | * @see client/jsonapi/basket/address/decorators/local |
||
| 85 | */ |
||
| 86 | |||
| 87 | /** client/jsonapi/basket/address/decorators/global |
||
| 88 | * Adds a list of globally available decorators only to the JsonApi client |
||
| 89 | * |
||
| 90 | * Decorators extend the functionality of a class by adding new aspects |
||
| 91 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 92 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 93 | * modify what is returned to the caller. |
||
| 94 | * |
||
| 95 | * This option allows you to wrap global decorators |
||
| 96 | * ("\Aimeos\Client\JsonApi\Common\Decorator\*") around the JsonApi |
||
| 97 | * client. |
||
| 98 | * |
||
| 99 | * client/jsonapi/basket/address/decorators/global = array( 'decorator1' ) |
||
| 100 | * |
||
| 101 | * This would add the decorator named "decorator1" defined by |
||
| 102 | * "\Aimeos\Client\JsonApi\Common\Decorator\Decorator1" only to the |
||
| 103 | * "basket" JsonApi client. |
||
| 104 | * |
||
| 105 | * @param array List of decorator names |
||
| 106 | * @since 2017.07 |
||
| 107 | * @category Developer |
||
| 108 | * @see client/jsonapi/common/decorators/default |
||
| 109 | * @see client/jsonapi/basket/address/decorators/excludes |
||
| 110 | * @see client/jsonapi/basket/address/decorators/local |
||
| 111 | */ |
||
| 112 | |||
| 113 | /** client/jsonapi/basket/address/decorators/local |
||
| 114 | * Adds a list of local decorators only to the JsonApi client |
||
| 115 | * |
||
| 116 | * Decorators extend the functionality of a class by adding new aspects |
||
| 117 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 118 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 119 | * modify what is returned to the caller. |
||
| 120 | * |
||
| 121 | * This option allows you to wrap local decorators |
||
| 122 | * ("\Aimeos\Client\JsonApi\Basket\Address\Decorator\*") around the JsonApi |
||
| 123 | * client. |
||
| 124 | * |
||
| 125 | * client/jsonapi/basket/address/decorators/local = array( 'decorator2' ) |
||
| 126 | * |
||
| 127 | * This would add the decorator named "decorator2" defined by |
||
| 128 | * "\Aimeos\Client\JsonApi\Basket\Address\Decorator\Decorator2" only to the |
||
| 129 | * "basket address" JsonApi client. |
||
| 130 | * |
||
| 131 | * @param array List of decorator names |
||
| 132 | * @since 2017.07 |
||
| 133 | * @category Developer |
||
| 134 | * @see client/jsonapi/common/decorators/default |
||
| 135 | * @see client/jsonapi/basket/address/decorators/excludes |
||
| 136 | * @see client/jsonapi/basket/address/decorators/global |
||
| 137 | */ |
||
| 138 | |||
| 139 | |||
| 140 | private $controller; |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Initializes the client |
||
| 145 | * |
||
| 146 | * @param \Aimeos\MShop\ContextIface $context MShop context object |
||
| 147 | */ |
||
| 148 | public function __construct( \Aimeos\MShop\ContextIface $context ) |
||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Deletes the resource or the resource list |
||
| 158 | * |
||
| 159 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 160 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 161 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 162 | */ |
||
| 163 | public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 221 | } |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * Updates the resource or the resource list partitially |
||
| 226 | * |
||
| 227 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 228 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 229 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 230 | */ |
||
| 231 | public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 232 | { |
||
| 233 | $view = $this->view(); |
||
| 234 | |||
| 235 | try |
||
| 236 | { |
||
| 237 | $this->clearCache(); |
||
| 238 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
| 239 | |||
| 240 | $body = (string) $request->getBody(); |
||
| 241 | |||
| 242 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
| 243 | throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 ); |
||
| 244 | } |
||
| 245 | |||
| 246 | if( !is_array( $payload->data ) ) { |
||
| 247 | $payload->data = [$payload->data]; |
||
| 248 | } |
||
| 249 | |||
| 250 | if( $relId = $view->param( 'relatedid' ) ) { |
||
| 251 | $this->controller->deleteAddress( $relId ); |
||
| 252 | } |
||
| 253 | |||
| 254 | foreach( $payload->data as $entry ) |
||
| 255 | { |
||
| 256 | if( !isset( $entry->id ) || !isset( $entry->attributes ) ) { |
||
| 257 | throw new \Aimeos\Client\JsonApi\Exception( 'Address type or attributes are missing', 400 ); |
||
| 258 | } |
||
| 259 | |||
| 260 | $this->controller->addAddress( $entry->id, (array) $entry->attributes ); |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | $view->item = $this->controller->get(); |
||
| 265 | $status = 200; |
||
| 266 | } |
||
| 267 | catch( \Aimeos\MShop\Plugin\Provider\Exception $e ) |
||
| 268 | { |
||
| 269 | $status = 409; |
||
| 270 | $errors = $this->translatePluginErrorCodes( $e->getErrorCodes() ); |
||
| 271 | $view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors; |
||
| 272 | } |
||
| 273 | catch( \Aimeos\MShop\Exception $e ) |
||
| 274 | { |
||
| 275 | $status = 404; |
||
| 276 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 277 | } |
||
| 278 | catch( \Exception $e ) |
||
| 279 | { |
||
| 280 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 281 | $view->errors = $this->getErrorDetails( $e ); |
||
| 282 | } |
||
| 283 | |||
| 284 | return $this->render( $response, $view, $status ); |
||
| 285 | } |
||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * Creates or updates the resource or the resource list |
||
| 290 | * |
||
| 291 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 292 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 293 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 294 | */ |
||
| 295 | public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 296 | { |
||
| 297 | $view = $this->view(); |
||
| 298 | |||
| 299 | try |
||
| 300 | { |
||
| 301 | $this->clearCache(); |
||
| 302 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
| 303 | |||
| 304 | $body = (string) $request->getBody(); |
||
| 305 | |||
| 306 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
| 307 | throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 ); |
||
| 308 | } |
||
| 309 | |||
| 310 | if( !is_array( $payload->data ) ) { |
||
| 311 | $payload->data = [$payload->data]; |
||
| 312 | } |
||
| 313 | |||
| 314 | foreach( $payload->data as $entry ) |
||
| 315 | { |
||
| 316 | if( !isset( $entry->id ) || !isset( $entry->attributes ) ) { |
||
| 317 | throw new \Aimeos\Client\JsonApi\Exception( 'Address type or attributes are missing', 400 ); |
||
| 318 | } |
||
| 319 | |||
| 320 | $this->controller->addAddress( $entry->id, (array) $entry->attributes ); |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | $view->item = $this->controller->get(); |
||
| 325 | $status = 201; |
||
| 326 | } |
||
| 327 | catch( \Aimeos\MShop\Plugin\Provider\Exception $e ) |
||
| 328 | { |
||
| 329 | $status = 409; |
||
| 330 | $errors = $this->translatePluginErrorCodes( $e->getErrorCodes() ); |
||
| 331 | $view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors; |
||
| 332 | } |
||
| 333 | catch( \Aimeos\MShop\Exception $e ) |
||
| 334 | { |
||
| 335 | $status = 404; |
||
| 336 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 337 | } |
||
| 338 | catch( \Exception $e ) |
||
| 339 | { |
||
| 340 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 341 | $view->errors = $this->getErrorDetails( $e ); |
||
| 342 | } |
||
| 343 | |||
| 344 | return $this->render( $response, $view, $status ); |
||
| 345 | } |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * Returns the available REST verbs and the available parameters |
||
| 350 | * |
||
| 351 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 352 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 353 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 354 | */ |
||
| 355 | public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 356 | { |
||
| 357 | $view = $this->view(); |
||
| 358 | |||
| 359 | $view->attributes = [ |
||
| 360 | 'addressid' => [ |
||
| 361 | 'label' => 'ID of the customer address', |
||
| 362 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 363 | ], |
||
| 364 | 'salutation' => [ |
||
| 365 | 'label' => 'Customer salutation, i.e. "comany" ,"mr", "ms" or ""', |
||
| 366 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 367 | ], |
||
| 368 | 'company' => [ |
||
| 369 | 'label' => 'Company name', |
||
| 370 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 371 | ], |
||
| 372 | 'vatid' => [ |
||
| 373 | 'label' => 'VAT ID of the company', |
||
| 374 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 375 | ], |
||
| 376 | 'title' => [ |
||
| 377 | 'label' => 'Title of the customer', |
||
| 378 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 379 | ], |
||
| 380 | 'firstname' => [ |
||
| 381 | 'label' => 'First name of the customer', |
||
| 382 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 383 | ], |
||
| 384 | 'lastname' => [ |
||
| 385 | 'label' => 'Last name of the customer or full name', |
||
| 386 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 387 | ], |
||
| 388 | 'address1' => [ |
||
| 389 | 'label' => 'First address part like street', |
||
| 390 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 391 | ], |
||
| 392 | 'address2' => [ |
||
| 393 | 'label' => 'Second address part like house number', |
||
| 394 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 395 | ], |
||
| 396 | 'address3' => [ |
||
| 397 | 'label' => 'Third address part like flat number', |
||
| 398 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 399 | ], |
||
| 400 | 'postal' => [ |
||
| 401 | 'label' => 'Zip code of the city', |
||
| 402 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 403 | ], |
||
| 404 | 'city' => [ |
||
| 405 | 'label' => 'Name of the town/city', |
||
| 406 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 407 | ], |
||
| 408 | 'state' => [ |
||
| 409 | 'label' => 'Two letter code of the country state', |
||
| 410 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 411 | ], |
||
| 412 | 'countryid' => [ |
||
| 413 | 'label' => 'Two letter ISO country code', |
||
| 414 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 415 | ], |
||
| 416 | 'languageid' => [ |
||
| 417 | 'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"', |
||
| 418 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 419 | ], |
||
| 420 | 'telephone' => [ |
||
| 421 | 'label' => 'Telephone number consisting of optional leading "+" and digits without spaces', |
||
| 422 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 423 | ], |
||
| 424 | 'telefax' => [ |
||
| 425 | 'label' => 'Facsimile number consisting of optional leading "+" and digits without spaces', |
||
| 426 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 427 | ], |
||
| 428 | 'email' => [ |
||
| 429 | 'label' => 'E-mail address', |
||
| 430 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 431 | ], |
||
| 432 | 'website' => [ |
||
| 433 | 'label' => 'Web site including "http://" or "https://"', |
||
| 434 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 435 | ], |
||
| 436 | 'longitude' => [ |
||
| 437 | 'label' => 'Longitude of the customer location as float value', |
||
| 438 | 'type' => 'float', 'default' => '', 'required' => false, |
||
| 439 | ], |
||
| 440 | 'latitude' => [ |
||
| 441 | 'label' => 'Latitude of the customer location as float value', |
||
| 442 | 'type' => 'float', 'default' => '', 'required' => false, |
||
| 443 | ], |
||
| 444 | ]; |
||
| 445 | |||
| 446 | $tplconf = 'client/jsonapi/template-options'; |
||
| 447 | $default = 'options-standard'; |
||
| 448 | |||
| 449 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
| 450 | |||
| 451 | return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
||
| 452 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
| 453 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
| 454 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
| 455 | ->withStatus( 200 ); |
||
| 456 | } |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns the response object with the rendered header and body |
||
| 461 | * |
||
| 462 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 463 | * @param \Aimeos\Base\View\Iface $view View instance |
||
| 464 | * @param int $status HTTP status code |
||
| 465 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 466 | */ |
||
| 467 | protected function render( ResponseInterface $response, \Aimeos\Base\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface |
||
| 479 | } |
||
| 480 | } |
||
| 481 |