| Total Complexity | 23 |
| Total Lines | 340 |
| 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 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 41 | $cntl->deleteItem( $view->param( 'id' ) ); |
||
|
1 ignored issue
–
show
|
|||
| 42 | $status = 200; |
||
| 43 | } |
||
| 44 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 45 | { |
||
| 46 | $status = 403; |
||
| 47 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 48 | } |
||
| 49 | catch( \Aimeos\MShop\Exception $e ) |
||
| 50 | { |
||
| 51 | $status = 404; |
||
| 52 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 53 | } |
||
| 54 | catch( \Exception $e ) |
||
| 55 | { |
||
| 56 | $status = 500; |
||
| 57 | $view->errors = $this->getErrorDetails( $e ); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $this->render( $response, $view, $status ); |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * Returns the resource or the resource list |
||
| 66 | * |
||
| 67 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 68 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 69 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 70 | */ |
||
| 71 | public function get( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 72 | { |
||
| 73 | $view = $this->getView(); |
||
| 74 | |||
| 75 | try |
||
| 76 | { |
||
| 77 | $ref = $view->param( 'include', [] ); |
||
| 78 | |||
| 79 | if( is_string( $ref ) ) { |
||
| 80 | $ref = explode( ',', $ref ); |
||
| 81 | } |
||
| 82 | |||
| 83 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 84 | |||
| 85 | $view->item = $cntl->getItem( $view->param( 'id' ), $ref ); |
||
|
1 ignored issue
–
show
|
|||
| 86 | $status = 200; |
||
| 87 | } |
||
| 88 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 89 | { |
||
| 90 | $status = 403; |
||
| 91 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 92 | } |
||
| 93 | catch( \Aimeos\MShop\Exception $e ) |
||
| 94 | { |
||
| 95 | $status = 404; |
||
| 96 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 97 | } |
||
| 98 | catch( \Exception $e ) |
||
| 99 | { |
||
| 100 | $status = 500; |
||
| 101 | $view->errors = $this->getErrorDetails( $e ); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $this->render( $response, $view, $status ); |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Updates the resource or the resource list partitially |
||
| 110 | * |
||
| 111 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 112 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 113 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 114 | */ |
||
| 115 | public function patch( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Creates or updates the resource or the resource list |
||
| 154 | * |
||
| 155 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 156 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 157 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 158 | */ |
||
| 159 | public function post( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 160 | { |
||
| 161 | $view = $this->getView(); |
||
| 162 | |||
| 163 | try |
||
| 164 | { |
||
| 165 | $body = (string) $request->getBody(); |
||
| 166 | |||
| 167 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
||
| 168 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
| 169 | } |
||
| 170 | |||
| 171 | $cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' ); |
||
| 172 | |||
| 173 | $view->item = $cntl->addItem( (array) $payload->data->attributes ); |
||
|
1 ignored issue
–
show
|
|||
| 174 | $view->nodata = true; // only expose customer ID to attackers |
||
| 175 | $status = 201; |
||
| 176 | } |
||
| 177 | catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
||
| 178 | { |
||
| 179 | $status = 403; |
||
| 180 | $view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
||
| 181 | } |
||
| 182 | catch( \Aimeos\MShop\Exception $e ) |
||
| 183 | { |
||
| 184 | $status = 404; |
||
| 185 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 186 | } |
||
| 187 | catch( \Exception $e ) |
||
| 188 | { |
||
| 189 | $status = 500; |
||
| 190 | $view->errors = $this->getErrorDetails( $e ); |
||
| 191 | } |
||
| 192 | |||
| 193 | return $this->render( $response, $view, $status ); |
||
| 194 | } |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the available REST verbs and the available parameters |
||
| 199 | * |
||
| 200 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 201 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 202 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 203 | */ |
||
| 204 | public function options( ServerRequestInterface $request, ResponseInterface $response ) |
||
| 205 | { |
||
| 206 | $view = $this->getView(); |
||
| 207 | |||
| 208 | $view->attributes = [ |
||
| 209 | 'customer.salutation' => [ |
||
| 210 | 'label' => 'Customer salutation, i.e. "comany" ,"mr", "mrs", "miss" or ""', |
||
| 211 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 212 | ], |
||
| 213 | 'customer.company' => [ |
||
| 214 | 'label' => 'Company name', |
||
| 215 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 216 | ], |
||
| 217 | 'customer.vatid' => [ |
||
| 218 | 'label' => 'VAT ID of the company', |
||
| 219 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 220 | ], |
||
| 221 | 'customer.title' => [ |
||
| 222 | 'label' => 'Title of the customer', |
||
| 223 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 224 | ], |
||
| 225 | 'customer.firstname' => [ |
||
| 226 | 'label' => 'First name of the customer', |
||
| 227 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 228 | ], |
||
| 229 | 'customer.lastname' => [ |
||
| 230 | 'label' => 'Last name of the customer or full name', |
||
| 231 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 232 | ], |
||
| 233 | 'customer.address1' => [ |
||
| 234 | 'label' => 'First address part like street', |
||
| 235 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 236 | ], |
||
| 237 | 'customer.address2' => [ |
||
| 238 | 'label' => 'Second address part like house number', |
||
| 239 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 240 | ], |
||
| 241 | 'customer.address3' => [ |
||
| 242 | 'label' => 'Third address part like flat number', |
||
| 243 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 244 | ], |
||
| 245 | 'customer.postal' => [ |
||
| 246 | 'label' => 'Zip code of the city', |
||
| 247 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 248 | ], |
||
| 249 | 'customer.city' => [ |
||
| 250 | 'label' => 'Name of the town/city', |
||
| 251 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 252 | ], |
||
| 253 | 'customer.state' => [ |
||
| 254 | 'label' => 'Two letter code of the country state', |
||
| 255 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 256 | ], |
||
| 257 | 'customer.countryid' => [ |
||
| 258 | 'label' => 'Two letter ISO country code', |
||
| 259 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 260 | ], |
||
| 261 | 'customer.languageid' => [ |
||
| 262 | 'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"', |
||
| 263 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 264 | ], |
||
| 265 | 'customer.telephone' => [ |
||
| 266 | 'label' => 'Telephone number consisting of option leading "+" and digits without spaces', |
||
| 267 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 268 | ], |
||
| 269 | 'customer.telefax' => [ |
||
| 270 | 'label' => 'Faximile number consisting of option leading "+" and digits without spaces', |
||
| 271 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 272 | ], |
||
| 273 | 'customer.email' => [ |
||
| 274 | 'label' => 'E-mail address', |
||
| 275 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 276 | ], |
||
| 277 | 'customer.website' => [ |
||
| 278 | 'label' => 'Web site including "http://" or "https://"', |
||
| 279 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 280 | ], |
||
| 281 | 'customer.longitude' => [ |
||
| 282 | 'label' => 'Longitude of the customer location as float value', |
||
| 283 | 'type' => 'float', 'default' => '', 'required' => false, |
||
| 284 | ], |
||
| 285 | 'customer.latitude' => [ |
||
| 286 | 'label' => 'Latitude of the customer location as float value', |
||
| 287 | 'type' => 'float', 'default' => '', 'required' => false, |
||
| 288 | ], |
||
| 289 | 'customer.label' => [ |
||
| 290 | 'label' => 'Label to identify the customer, will be firstname, lastname and company if empty', |
||
| 291 | 'type' => 'string', 'default' => '', 'required' => true, |
||
| 292 | ], |
||
| 293 | 'customer.code' => [ |
||
| 294 | 'label' => 'Unique customer identifier, will be the e-mail address if empty', |
||
| 295 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 296 | ], |
||
| 297 | 'customer.password' => [ |
||
| 298 | 'label' => 'Password of the customer, generated if emtpy', |
||
| 299 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 300 | ], |
||
| 301 | 'customer.birthday' => [ |
||
| 302 | 'label' => 'ISO date in YYYY-MM-DD format of the birthday', |
||
| 303 | 'type' => 'string', 'default' => '', 'required' => false, |
||
| 304 | ], |
||
| 305 | 'customer.status' => [ |
||
| 306 | 'label' => 'Customer account status, i.e. "0" for disabled, "1" for enabled and is enabled by default', |
||
| 307 | 'type' => 'integer', 'default' => '1', 'required' => false, |
||
| 308 | ], |
||
| 309 | ]; |
||
| 310 | |||
| 311 | $tplconf = 'client/jsonapi/standard/template-options'; |
||
| 312 | $default = 'options-standard'; |
||
| 313 | |||
| 314 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
| 315 | |||
| 316 | return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
||
| 317 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
| 318 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
| 319 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
| 320 | ->withStatus( 200 ); |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns the response object with the rendered header and body |
||
| 326 | * |
||
| 327 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 328 | * @param \Aimeos\MW\View\Iface $view View instance |
||
| 329 | * @param integer $status HTTP status code |
||
| 330 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 331 | */ |
||
| 332 | protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) |
||
| 363 | } |
||
| 364 | } |
||
| 365 |