| Total Complexity | 34 |
| Total Lines | 397 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 23 | class Standard |
||
| 24 | extends \Aimeos\Client\JsonApi\Base |
||
| 25 | implements \Aimeos\Client\JsonApi\Iface |
||
| 26 | { |
||
| 27 | private $controller; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * Initializes the client |
||
| 32 | * |
||
| 33 | * @param \Aimeos\MShop\Context\Item\Iface $context MShop context object |
||
| 34 | * @param string $path Name of the client, e.g "customer/address" |
||
| 35 | */ |
||
| 36 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context, $path ) |
||
| 37 | { |
||
| 38 | parent::__construct( $context, $path ); |
||
| 39 | |||
| 40 | $this->controller = \Aimeos\Controller\Frontend\Customer\Factory::create( $this->getContext() ); |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Deletes the resource or the resource list |
||
| 46 | * |
||
| 47 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 48 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 49 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 50 | */ |
||
| 51 | public function delete( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 52 | { |
||
| 53 | $view = $this->getView(); |
||
| 54 | |||
| 55 | try |
||
| 56 | { |
||
| 57 | $relId = $view->param( 'relatedid' ); |
||
| 58 | $body = (string) $request->getBody(); |
||
| 59 | |||
| 60 | if( $relId === '' || $relId === null ) |
||
| 61 | { |
||
| 62 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
| 63 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 64 | } |
||
| 65 | |||
| 66 | if( !is_array( $payload->data ) ) { |
||
| 67 | $payload->data = [$payload->data]; |
||
| 68 | } |
||
| 69 | |||
| 70 | foreach( $payload->data as $entry ) |
||
| 71 | { |
||
| 72 | if( !isset( $entry->id ) ) { |
||
| 73 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 ); |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->controller->deleteAddressItem( $entry->id ); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | else |
||
| 80 | { |
||
| 81 | $this->controller->deleteAddressItem( $relId ); |
||
| 82 | } |
||
| 83 | |||
| 84 | $status = 200; |
||
| 85 | } |
||
| 86 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 87 | { |
||
| 88 | $status = 403; |
||
| 89 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 90 | } |
||
| 91 | catch( \Aimeos\MShop\Exception $e ) |
||
| 92 | { |
||
| 93 | $status = 404; |
||
| 94 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 95 | } |
||
| 96 | catch( \Exception $e ) |
||
| 97 | { |
||
| 98 | $status = 500; |
||
| 99 | $view->errors = $this->getErrorDetails( $e ); |
||
| 100 | } |
||
| 101 | |||
| 102 | return $this->render( $response, $view, $status ); |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns the resource or the resource list |
||
| 108 | * |
||
| 109 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 110 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 111 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 112 | */ |
||
| 113 | public function get( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 114 | { |
||
| 115 | $view = $this->getView(); |
||
| 116 | |||
| 117 | try |
||
| 118 | { |
||
| 119 | $relId = $view->param( 'relatedid' ); |
||
| 120 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 121 | |||
| 122 | if( $relId == null ) |
||
| 123 | { |
||
| 124 | $view->items = $cntl->getItem( $view->param( 'id' ), ['customer/address'] )->getAddressItems(); |
||
|
1 ignored issue
–
show
|
|||
| 125 | $view->total = count( $view->items ); |
||
| 126 | } |
||
| 127 | else |
||
| 128 | { |
||
| 129 | $view->items = $cntl->getAddressItem( $relId ); |
||
|
1 ignored issue
–
show
|
|||
| 130 | $view->total = 1; |
||
| 131 | } |
||
| 132 | |||
| 133 | $status = 200; |
||
| 134 | } |
||
| 135 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 136 | { |
||
| 137 | $status = 403; |
||
| 138 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 139 | } |
||
| 140 | catch( \Aimeos\MShop\Exception $e ) |
||
| 141 | { |
||
| 142 | $status = 404; |
||
| 143 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 144 | } |
||
| 145 | catch( \Exception $e ) |
||
| 146 | { |
||
| 147 | $status = 500; |
||
| 148 | $view->errors = $this->getErrorDetails( $e ); |
||
| 149 | } |
||
| 150 | |||
| 151 | return $this->render( $response, $view, $status ); |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Updates the resource or the resource list partitially |
||
| 157 | * |
||
| 158 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 159 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 160 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 161 | */ |
||
| 162 | public function patch( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 163 | { |
||
| 164 | $view = $this->getView(); |
||
| 165 | |||
| 166 | try |
||
| 167 | { |
||
| 168 | $body = (string) $request->getBody(); |
||
| 169 | |||
| 170 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
||
| 171 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 172 | } |
||
| 173 | |||
| 174 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 175 | |||
| 176 | $view->items = $cntl->editAddressItem( $view->param( 'relatedid' ), (array) $payload->data->attributes ); |
||
|
1 ignored issue
–
show
|
|||
| 177 | $view->total = 1; |
||
| 178 | $status = 200; |
||
| 179 | } |
||
| 180 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 181 | { |
||
| 182 | $status = 403; |
||
| 183 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 184 | } |
||
| 185 | catch( \Aimeos\MShop\Exception $e ) |
||
| 186 | { |
||
| 187 | $status = 404; |
||
| 188 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 189 | } |
||
| 190 | catch( \Exception $e ) |
||
| 191 | { |
||
| 192 | $status = 500; |
||
| 193 | $view->errors = $this->getErrorDetails( $e ); |
||
| 194 | } |
||
| 195 | |||
| 196 | return $this->render( $response, $view, $status ); |
||
| 197 | } |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Creates or updates the resource or the resource list |
||
| 202 | * |
||
| 203 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 204 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 205 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 206 | */ |
||
| 207 | public function post( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 255 | } |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns the available REST verbs and the available parameters |
||
| 260 | * |
||
| 261 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 262 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 263 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 264 | */ |
||
| 265 | public function options( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 378 | } |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * Returns the response object with the rendered header and body |
||
| 383 | * |
||
| 384 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 385 | * @param \Aimeos\MW\View\Iface $view View instance |
||
| 386 | * @param integer $status HTTP status code |
||
| 387 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 388 | */ |
||
| 389 | protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) |
||
| 420 | } |
||
| 421 | } |
||
| 422 |