| Total Complexity | 36 |
| Total Lines | 398 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 ) |
||
| 35 | { |
||
| 36 | $view = $this->getView(); |
||
| 37 | |||
| 38 | try |
||
| 39 | { |
||
| 40 | $body = (string) $request->getBody(); |
||
| 41 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 42 | $items = $cntl->use( ['customer/address'] )->get()->getAddressItems(); |
||
|
1 ignored issue
–
show
|
|||
| 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 | foreach( $payload->data as $entry ) |
||
| 55 | { |
||
| 56 | if( !isset( $entry->id ) ) { |
||
| 57 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 ); |
||
| 58 | } |
||
| 59 | |||
| 60 | if( isset( $items[$entry->id] ) ) { |
||
| 61 | $cntl->deleteAddressItem( $items[$entry->id] ); |
||
|
1 ignored issue
–
show
|
|||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $cntl->store(); |
||
|
1 ignored issue
–
show
|
|||
| 66 | } |
||
| 67 | else |
||
| 68 | { |
||
| 69 | if( isset( $items[$relId] ) ) { |
||
| 70 | $cntl->deleteAddressItem( $items[$relId] )->store(); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | $status = 200; |
||
| 75 | } |
||
| 76 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 77 | { |
||
| 78 | $status = 403; |
||
| 79 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 80 | } |
||
| 81 | catch( \Aimeos\MShop\Exception $e ) |
||
| 82 | { |
||
| 83 | $status = 404; |
||
| 84 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 85 | } |
||
| 86 | catch( \Exception $e ) |
||
| 87 | { |
||
| 88 | $status = 500; |
||
| 89 | $view->errors = $this->getErrorDetails( $e ); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $this->render( $response, $view, $status ); |
||
| 93 | } |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns the resource or the resource list |
||
| 98 | * |
||
| 99 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 100 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 101 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 102 | */ |
||
| 103 | public function get( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 104 | { |
||
| 105 | $view = $this->getView(); |
||
| 106 | |||
| 107 | try |
||
| 108 | { |
||
| 109 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 110 | $item = $cntl->use( ['customer/address'] )->get(); |
||
| 111 | |||
| 112 | if( ( $relId = $view->param( 'relatedid' ) ) == null ) |
||
| 113 | { |
||
| 114 | $view->items = $item->getAddressItems(); |
||
| 115 | $view->total = count( $view->items ); |
||
| 116 | } |
||
| 117 | else |
||
| 118 | { |
||
| 119 | $view->items = $item->getAddressItem( $relId ); |
||
| 120 | $view->total = empty( $view->items ) ? 0 : 1; |
||
| 121 | } |
||
| 122 | |||
| 123 | $status = 200; |
||
| 124 | } |
||
| 125 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 126 | { |
||
| 127 | $status = 403; |
||
| 128 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 129 | } |
||
| 130 | catch( \Aimeos\MShop\Exception $e ) |
||
| 131 | { |
||
| 132 | $status = 404; |
||
| 133 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 134 | } |
||
| 135 | catch( \Exception $e ) |
||
| 136 | { |
||
| 137 | $status = 500; |
||
| 138 | $view->errors = $this->getErrorDetails( $e ); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $this->render( $response, $view, $status ); |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Updates the resource or the resource list partitially |
||
| 147 | * |
||
| 148 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 149 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 150 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 151 | */ |
||
| 152 | public function patch( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 153 | { |
||
| 154 | $view = $this->getView(); |
||
| 155 | |||
| 156 | try |
||
| 157 | { |
||
| 158 | $body = (string) $request->getBody(); |
||
| 159 | |||
| 160 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
||
| 161 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 162 | } |
||
| 163 | |||
| 164 | $status = 404; |
||
| 165 | $view->total = 0; |
||
| 166 | $id = $view->param( 'relatedid' ); |
||
| 167 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 168 | |||
| 169 | if( ( $item = $cntl->use( ['customer/address'] )->get()->getAddressItem( $id ) ) !== null ) |
||
| 170 | { |
||
| 171 | $attributes = (array) $payload->data->attributes; |
||
| 172 | $item = $item->fromArray( $attributes ); |
||
| 173 | $cntl->addAddressItem( $item, $id )->store(); |
||
|
1 ignored issue
–
show
|
|||
| 174 | |||
| 175 | $view->items = $item; |
||
| 176 | $view->total = 1; |
||
| 177 | $status = 200; |
||
| 178 | } |
||
| 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 ) |
||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns the available REST verbs and the available parameters |
||
| 261 | * |
||
| 262 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 263 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 264 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 265 | */ |
||
| 266 | public function options( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 379 | } |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns the response object with the rendered header and body |
||
| 384 | * |
||
| 385 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 386 | * @param \Aimeos\MW\View\Iface $view View instance |
||
| 387 | * @param integer $status HTTP status code |
||
| 388 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 389 | */ |
||
| 390 | protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) |
||
| 421 | } |
||
| 422 | } |
||
| 423 |