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

Standard::patch()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 35
c 0
b 0
f 0
rs 9.0111
cc 6
nc 16
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\Relationships;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API customer/relationships 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/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
Bug introduced by
The method createListsFilter() 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

125
				$filter = $this->initCriteria( $cntl->/** @scrutinizer ignore-call */ createListsFilter(), $view->param( 'filter', [] ) );
Loading history...
126
				$view->items = $cntl->searchListItems( $filter, $total );
1 ignored issue
show
Bug introduced by
The method searchListItems() 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

126
				/** @scrutinizer ignore-call */ 
127
    $view->items = $cntl->searchListItems( $filter, $total );
Loading history...
127
			}
128
			else
129
			{
130
				$view->items = $cntl->getListItem( $relId );
1 ignored issue
show
Bug introduced by
The method getListItem() 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

130
				/** @scrutinizer ignore-call */ 
131
    $view->items = $cntl->getListItem( $relId );
Loading history...
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
Bug introduced by
The method editListItem() 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

177
			/** @scrutinizer ignore-call */ 
178
   $view->items = $cntl->editListItem( $view->param( 'relatedid' ), (array) $payload->data->attributes );
Loading history...
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 )
267
	{
268
		$view = $this->getView();
269
270
		$view->attributes = [
271
			'customer.lists.refid' => [
272
				'label' => 'ID of the related domain item',
273
				'type' => 'string', 'default' => '', 'required' => true,
274
			],
275
			'customer.lists.domain' => [
276
				'label' => 'Domain of the related item, e.g. "product"',
277
				'type' => 'string', 'default' => '', 'required' => true,
278
			],
279
			'customer.lists.type' => [
280
				'label' => 'Customer relationship type, e.g. "favorite"',
281
				'type' => 'string', 'default' => '', 'required' => false,
282
			],
283
			'customer.lists.config' => [
284
				'label' => 'Associative list of key/value configuration pairs',
285
				'type' => 'string', 'default' => '[]', 'required' => false,
286
			],
287
			'customer.lists.datestart' => [
288
				'label' => 'Start date when the relationship is valied',
289
				'type' => 'string', 'default' => '', 'required' => false,
290
			],
291
			'customer.lists.dateend' => [
292
				'label' => 'End date until the relationship is valid',
293
				'type' => 'string', 'default' => '', 'required' => false,
294
			],
295
			'customer.lists.status' => [
296
				'label' => 'Status of the relationship (0=disable, 1=enabled)',
297
				'type' => 'string', 'default' => '1', 'required' => false,
298
			],
299
		];
300
301
		$tplconf = 'client/jsonapi/standard/template-options';
302
		$default = 'options-standard';
303
304
		$body = $view->render( $view->config( $tplconf, $default ) );
305
306
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
307
			->withHeader( 'Cache-Control', 'max-age=300' )
308
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
309
			->withBody( $view->response()->createStreamFromString( $body ) )
310
			->withStatus( 200 );
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 )
323
	{
324
		/** client/jsonapi/customer/relationships/standard/template
325
		 * Relative path to the customer relationships JSON API template
326
		 *
327
		 * The template file contains the code and processing instructions
328
		 * to generate the result shown in the JSON API body. The
329
		 * configuration string is the path to the template file relative
330
		 * to the templates directory (usually in client/jsonapi/templates).
331
		 *
332
		 * You can overwrite the template file configuration in extensions and
333
		 * provide alternative templates. These alternative templates should be
334
		 * named like the default one but with the string "standard" replaced by
335
		 * an unique name. You may use the name of your project for this. If
336
		 * you've implemented an alternative client class as well, "standard"
337
		 * should be replaced by the name of the new class.
338
		 *
339
		 * @param string Relative path to the template creating the body for the JSON API
340
		 * @since 2017.07
341
		 * @category Developer
342
		 */
343
		$tplconf = 'client/jsonapi/customer/relationships/standard/template';
344
		$default = 'customer/relationships/standard';
345
346
		$body = $view->render( $view->config( $tplconf, $default ) );
347
348
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
349
			->withHeader( 'Cache-Control', 'no-cache, private' )
350
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
351
			->withBody( $view->response()->createStreamFromString( $body ) )
352
			->withStatus( $status );
353
	}
354
}
355