| Total Complexity | 34 |
| Total Lines | 330 |
| 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/relationships" |
||
| 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->deleteListItem( $entry->id ); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | else |
||
| 80 | { |
||
| 81 | $this->controller->deleteListItem( $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 | $total = 1; |
||
| 120 | $relId = $view->param( 'relatedid' ); |
||
| 121 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 122 | |||
| 123 | if( $relId == null ) |
||
| 124 | { |
||
| 125 | $filter = $this->initCriteria( $cntl->createListsFilter(), $view->param( 'filter', [] ) ); |
||
|
1 ignored issue
–
show
|
|||
| 126 | $view->items = $cntl->searchListItems( $filter, $total ); |
||
|
1 ignored issue
–
show
|
|||
| 127 | } |
||
| 128 | else |
||
| 129 | { |
||
| 130 | $view->items = $cntl->getListItem( $relId ); |
||
|
1 ignored issue
–
show
|
|||
| 131 | } |
||
| 132 | |||
| 133 | $view->total = $total; |
||
| 134 | $status = 200; |
||
| 135 | } |
||
| 136 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 137 | { |
||
| 138 | $status = 403; |
||
| 139 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 140 | } |
||
| 141 | catch( \Aimeos\MShop\Exception $e ) |
||
| 142 | { |
||
| 143 | $status = 404; |
||
| 144 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 145 | } |
||
| 146 | catch( \Exception $e ) |
||
| 147 | { |
||
| 148 | $status = 500; |
||
| 149 | $view->errors = $this->getErrorDetails( $e ); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $this->render( $response, $view, $status ); |
||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Updates the resource or the resource list partitially |
||
| 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 patch( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 164 | { |
||
| 165 | $view = $this->getView(); |
||
| 166 | |||
| 167 | try |
||
| 168 | { |
||
| 169 | $body = (string) $request->getBody(); |
||
| 170 | |||
| 171 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
||
| 172 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 173 | } |
||
| 174 | |||
| 175 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 176 | |||
| 177 | $view->items = $cntl->editListItem( $view->param( 'relatedid' ), (array) $payload->data->attributes ); |
||
|
1 ignored issue
–
show
|
|||
| 178 | $view->total = 1; |
||
| 179 | $status = 200; |
||
| 180 | } |
||
| 181 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 182 | { |
||
| 183 | $status = 403; |
||
| 184 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 185 | } |
||
| 186 | catch( \Aimeos\MShop\Exception $e ) |
||
| 187 | { |
||
| 188 | $status = 404; |
||
| 189 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 190 | } |
||
| 191 | catch( \Exception $e ) |
||
| 192 | { |
||
| 193 | $status = 500; |
||
| 194 | $view->errors = $this->getErrorDetails( $e ); |
||
| 195 | } |
||
| 196 | |||
| 197 | return $this->render( $response, $view, $status ); |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Creates or updates the resource or the resource list |
||
| 203 | * |
||
| 204 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 205 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 206 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 207 | */ |
||
| 208 | public function post( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 209 | { |
||
| 210 | $view = $this->getView(); |
||
| 211 | |||
| 212 | try |
||
| 213 | { |
||
| 214 | $list = []; |
||
| 215 | $body = (string) $request->getBody(); |
||
| 216 | |||
| 217 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
| 218 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 219 | } |
||
| 220 | |||
| 221 | if( !is_array( $payload->data ) ) { |
||
| 222 | $payload->data = [$payload->data]; |
||
| 223 | } |
||
| 224 | |||
| 225 | foreach( $payload->data as $entry ) |
||
| 226 | { |
||
| 227 | if( !isset( $entry->attributes ) ) { |
||
| 228 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) ); |
||
| 229 | } |
||
| 230 | |||
| 231 | $list[] = $this->controller->addListItem( (array) $entry->attributes ); |
||
| 232 | } |
||
| 233 | |||
| 234 | |||
| 235 | $view->total = count( $list ); |
||
| 236 | $view->items = $list; |
||
| 237 | $status = 201; |
||
| 238 | } |
||
| 239 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 240 | { |
||
| 241 | $status = 403; |
||
| 242 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 243 | } |
||
| 244 | catch( \Aimeos\MShop\Exception $e ) |
||
| 245 | { |
||
| 246 | $status = 404; |
||
| 247 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 248 | } |
||
| 249 | catch( \Exception $e ) |
||
| 250 | { |
||
| 251 | $status = 500; |
||
| 252 | $view->errors = $this->getErrorDetails( $e ); |
||
| 253 | } |
||
| 254 | |||
| 255 | return $this->render( $response, $view, $status ); |
||
| 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 ) |
||
| 311 | } |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Returns the response object with the rendered header and body |
||
| 316 | * |
||
| 317 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 318 | * @param \Aimeos\MW\View\Iface $view View instance |
||
| 319 | * @param integer $status HTTP status code |
||
| 320 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 321 | */ |
||
| 322 | protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) |
||
| 355 |