Completed
Branch master (7caa83)
by Aimeos
03:01
created

Standard::get()   A

Complexity

Conditions 5
Paths 17

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 39
c 0
b 0
f 0
rs 9.2728
cc 5
nc 17
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Customer\Address;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API customer/address client
19
 *
20
 * @package Client
21
 * @subpackage JsonApi
22
 */
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
Bug introduced by
The method getItem() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
				$view->items = $cntl->/** @scrutinizer ignore-call */ getItem( $view->param( 'id' ), ['customer/address'] )->getAddressItems();
Loading history...
125
				$view->total = count( $view->items );
126
			}
127
			else
128
			{
129
				$view->items = $cntl->getAddressItem( $relId );
1 ignored issue
show
Bug introduced by
The method getAddressItem() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
				/** @scrutinizer ignore-call */ 
130
    $view->items = $cntl->getAddressItem( $relId );
Loading history...
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
Bug introduced by
The method editAddressItem() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
			/** @scrutinizer ignore-call */ 
177
   $view->items = $cntl->editAddressItem( $view->param( 'relatedid' ), (array) $payload->data->attributes );
Loading history...
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 )
208
	{
209
		$view = $this->getView();
210
211
		try
212
		{
213
			$list = [];
214
			$body = (string) $request->getBody();
215
216
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
217
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
218
			}
219
220
			if( !is_array( $payload->data ) ) {
221
				$payload->data = [$payload->data];
222
			}
223
224
			foreach( $payload->data as $entry )
225
			{
226
				if( !isset( $entry->attributes ) ) {
227
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) );
228
				}
229
230
				$list[] = $this->controller->addAddressItem( (array) $entry->attributes );
231
			}
232
233
234
			$view->total = count( $list );
235
			$view->items = $list;
236
			$status = 201;
237
		}
238
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
239
		{
240
			$status = 403;
241
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
242
		}
243
		catch( \Aimeos\MShop\Exception $e )
244
		{
245
			$status = 404;
246
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
247
		}
248
		catch( \Exception $e )
249
		{
250
			$status = 500;
251
			$view->errors = $this->getErrorDetails( $e );
252
		}
253
254
		return $this->render( $response, $view, $status );
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 )
266
	{
267
		$view = $this->getView();
268
269
		$view->attributes = [
270
			'customer.address.salutation' => [
271
				'label' => 'Customer salutation, i.e. "comany" ,"mr", "mrs", "miss" or ""',
272
				'type' => 'string', 'default' => '', 'required' => false,
273
			],
274
			'customer.address.company' => [
275
				'label' => 'Company name',
276
				'type' => 'string', 'default' => '', 'required' => false,
277
			],
278
			'customer.address.vatid' => [
279
				'label' => 'VAT ID of the company',
280
				'type' => 'string', 'default' => '', 'required' => false,
281
			],
282
			'customer.address.title' => [
283
				'label' => 'Title of the customer',
284
				'type' => 'string', 'default' => '', 'required' => false,
285
			],
286
			'customer.address.firstname' => [
287
				'label' => 'First name of the customer',
288
				'type' => 'string', 'default' => '', 'required' => false,
289
			],
290
			'customer.address.lastname' => [
291
				'label' => 'Last name of the customer or full name',
292
				'type' => 'string', 'default' => '', 'required' => true,
293
			],
294
			'customer.address.address1' => [
295
				'label' => 'First address part like street',
296
				'type' => 'string', 'default' => '', 'required' => true,
297
			],
298
			'customer.address.address2' => [
299
				'label' => 'Second address part like house number',
300
				'type' => 'string', 'default' => '', 'required' => false,
301
			],
302
			'customer.address.address3' => [
303
				'label' => 'Third address part like flat number',
304
				'type' => 'string', 'default' => '', 'required' => false,
305
			],
306
			'customer.address.postal' => [
307
				'label' => 'Zip code of the city',
308
				'type' => 'string', 'default' => '', 'required' => false,
309
			],
310
			'customer.address.city' => [
311
				'label' => 'Name of the town/city',
312
				'type' => 'string', 'default' => '', 'required' => true,
313
			],
314
			'customer.address.state' => [
315
				'label' => 'Two letter code of the country state',
316
				'type' => 'string', 'default' => '', 'required' => false,
317
			],
318
			'customer.address.countryid' => [
319
				'label' => 'Two letter ISO country code',
320
				'type' => 'string', 'default' => '', 'required' => true,
321
			],
322
			'customer.address.languageid' => [
323
				'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"',
324
				'type' => 'string', 'default' => '', 'required' => false,
325
			],
326
			'customer.address.telephone' => [
327
				'label' => 'Telephone number consisting of option leading "+" and digits without spaces',
328
				'type' => 'string', 'default' => '', 'required' => false,
329
			],
330
			'customer.address.telefax' => [
331
				'label' => 'Faximile number consisting of option leading "+" and digits without spaces',
332
				'type' => 'string', 'default' => '', 'required' => false,
333
			],
334
			'customer.address.email' => [
335
				'label' => 'E-mail address',
336
				'type' => 'string', 'default' => '', 'required' => false,
337
			],
338
			'customer.address.website' => [
339
				'label' => 'Web site including "http://" or "https://"',
340
				'type' => 'string', 'default' => '', 'required' => false,
341
			],
342
			'customer.address.longitude' => [
343
				'label' => 'Longitude of the customer location as float value',
344
				'type' => 'float', 'default' => '', 'required' => false,
345
			],
346
			'customer.address.latitude' => [
347
				'label' => 'Latitude of the customer location as float value',
348
				'type' => 'float', 'default' => '', 'required' => false,
349
			],
350
			'customer.address.label' => [
351
				'label' => 'Label to identify the customer, usually the full name',
352
				'type' => 'string', 'default' => '', 'required' => true,
353
			],
354
			'customer.address.code' => [
355
				'label' => 'Unique customer identifier, usually e-mail address',
356
				'type' => 'string', 'default' => '', 'required' => true,
357
			],
358
			'customer.address.birthday' => [
359
				'label' => 'ISO date in YYYY-MM-DD format of the birthday',
360
				'type' => 'string', 'default' => '', 'required' => false,
361
			],
362
			'customer.address.status' => [
363
				'label' => 'Customer account status, i.e. "0" for disabled, "1" for enabled',
364
				'type' => 'integer', 'default' => '1', 'required' => false,
365
			],
366
		];
367
368
		$tplconf = 'client/jsonapi/standard/template-options';
369
		$default = 'options-standard';
370
371
		$body = $view->render( $view->config( $tplconf, $default ) );
372
373
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
374
			->withHeader( 'Cache-Control', 'max-age=300' )
375
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
376
			->withBody( $view->response()->createStreamFromString( $body ) )
377
			->withStatus( 200 );
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 )
390
	{
391
		/** client/jsonapi/customer/address/standard/template
392
		 * Relative path to the customer address JSON API template
393
		 *
394
		 * The template file contains the code and processing instructions
395
		 * to generate the result shown in the JSON API body. The
396
		 * configuration string is the path to the template file relative
397
		 * to the templates directory (usually in client/jsonapi/templates).
398
		 *
399
		 * You can overwrite the template file configuration in extensions and
400
		 * provide alternative templates. These alternative templates should be
401
		 * named like the default one but with the string "standard" replaced by
402
		 * an unique name. You may use the name of your project for this. If
403
		 * you've implemented an alternative client class as well, "standard"
404
		 * should be replaced by the name of the new class.
405
		 *
406
		 * @param string Relative path to the template creating the body for the JSON API
407
		 * @since 2017.07
408
		 * @category Developer
409
		 */
410
		$tplconf = 'client/jsonapi/customer/address/standard/template';
411
		$default = 'customer/address/standard';
412
413
		$body = $view->render( $view->config( $tplconf, $default ) );
414
415
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
416
			->withHeader( 'Cache-Control', 'no-cache, private' )
417
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
418
			->withBody( $view->response()->createStreamFromString( $body ) )
419
			->withStatus( $status );
420
	}
421
}
422