Completed
Push — master ( cd5eb9...af944c )
by Aimeos
02:24
created

Standard   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 7
dl 0
loc 344
rs 9.2
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C delete() 0 54 11
B get() 0 42 5
B patch() 0 37 6
C post() 0 50 9
A options() 0 47 1
B render() 0 32 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
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 \Aimeos\MW\View\Iface $view View object
35
	 * @param array $templatePaths List of file system paths where the templates are stored
36
	 * @param string $path Name of the client, e.g "customer/relationships"
37
	 */
38
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MW\View\Iface $view, array $templatePaths, $path )
39
	{
40
		parent::__construct( $context, $view, $templatePaths, $path );
41
42
		$this->controller = \Aimeos\Controller\Frontend\Customer\Factory::createController( $this->getContext() );
43
	}
44
45
46
	/**
47
	 * Deletes the resource or the resource list
48
	 *
49
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
50
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
51
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
52
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
53
	 */
54
	public function delete( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
55
	{
56
		$view = $this->getView();
57
		$view->prefix = $prefix;
58
59
		try
60
		{
61
			$relId = $view->param( 'relatedid' );
62
			$body = (string) $request->getBody();
63
64
			if( $relId === '' || $relId === null )
65
			{
66
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
67
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
68
				}
69
70
				if( !is_array( $payload->data ) ) {
71
					$payload->data = [$payload->data];
72
				}
73
74
				foreach( $payload->data as $entry )
75
				{
76
					if( !isset( $entry->id ) ) {
77
						throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 );
78
					}
79
80
					$this->controller->deleteListItem( $entry->id );
81
				}
82
			}
83
			else
84
			{
85
				$this->controller->deleteListItem( $relId );
86
			}
87
88
			$status = 200;
89
		}
90
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
91
		{
92
			$status = 403;
93
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
94
		}
95
		catch( \Aimeos\MShop\Exception $e )
96
		{
97
			$status = 404;
98
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
99
		}
100
		catch( \Exception $e )
101
		{
102
			$status = 500;
103
			$view->errors = $this->getErrorDetails( $e );
104
		}
105
106
		return $this->render( $response, $view, $status );
107
	}
108
109
110
	/**
111
	 * Returns the resource or the resource list
112
	 *
113
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
114
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
115
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
116
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
117
	 */
118
	public function get( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
119
	{
120
		$view = $this->getView();
121
		$view->prefix = $prefix;
122
123
		try
124
		{
125
			$total = 1;
126
			$relId = $view->param( 'relatedid' );
127
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
128
129
			if( $relId == null )
130
			{
131
				$filter = $this->initCriteria( $cntl->createListsFilter(), $view->param( 'filter', [] ) );
132
				$view->items = $cntl->searchListItems( $filter, $total );
133
			}
134
			else
135
			{
136
				$view->items = $cntl->getListItem( $relId );
137
			}
138
139
			$view->total = $total;
140
			$status = 200;
141
		}
142
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
143
		{
144
			$status = 403;
145
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
146
		}
147
		catch( \Aimeos\MShop\Exception $e )
148
		{
149
			$status = 404;
150
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
151
		}
152
		catch( \Exception $e )
153
		{
154
			$status = 500;
155
			$view->errors = $this->getErrorDetails( $e );
156
		}
157
158
		return $this->render( $response, $view, $status );
159
	}
160
161
162
	/**
163
	 * Updates the resource or the resource list partitially
164
	 *
165
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
166
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
167
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
168
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
169
	 */
170
	public function patch( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
171
	{
172
		$view = $this->getView();
173
		$view->prefix = $prefix;
174
175
		try
176
		{
177
			$body = (string) $request->getBody();
178
179
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
180
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
181
			}
182
183
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
184
185
			$view->items = $cntl->editListItem( $view->param( 'relatedid' ), (array) $payload->data->attributes );
186
			$view->total = 1;
187
			$status = 200;
188
		}
189
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
190
		{
191
			$status = 403;
192
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
193
		}
194
		catch( \Aimeos\MShop\Exception $e )
195
		{
196
			$status = 404;
197
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
198
		}
199
		catch( \Exception $e )
200
		{
201
			$status = 500;
202
			$view->errors = $this->getErrorDetails( $e );
203
		}
204
205
		return $this->render( $response, $view, $status );
206
	}
207
208
209
	/**
210
	 * Creates or updates the resource or the resource list
211
	 *
212
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
213
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
214
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
215
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
216
	 */
217
	public function post( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
218
	{
219
		$view = $this->getView();
220
		$view->prefix = $prefix;
221
222
		try
223
		{
224
			$list = [];
225
			$body = (string) $request->getBody();
226
227
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
228
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
229
			}
230
231
			if( !is_array( $payload->data ) ) {
232
				$payload->data = [$payload->data];
233
			}
234
235
			foreach( $payload->data as $entry )
236
			{
237
				if( !isset( $entry->attributes ) ) {
238
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) );
239
				}
240
241
				$list[] = $this->controller->addListItem( (array) $entry->attributes );
242
			}
243
244
245
			$view->total = count( $list );
246
			$view->items = $list;
247
			$status = 201;
248
		}
249
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
250
		{
251
			$status = 403;
252
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
253
		}
254
		catch( \Aimeos\MShop\Exception $e )
255
		{
256
			$status = 404;
257
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
258
		}
259
		catch( \Exception $e )
260
		{
261
			$status = 500;
262
			$view->errors = $this->getErrorDetails( $e );
263
		}
264
265
		return $this->render( $response, $view, $status );
266
	}
267
268
269
	/**
270
	 * Returns the available REST verbs and the available parameters
271
	 *
272
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
273
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
274
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
275
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
276
	 */
277
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
278
	{
279
		$view = $this->getView();
280
		$view->prefix = $prefix;
281
282
		$view->attributes = [
283
			'customer.lists.refid' => [
284
				'label' => 'ID of the related domain item',
285
				'type' => 'string', 'default' => '', 'required' => true,
286
			],
287
			'customer.lists.domain' => [
288
				'label' => 'Domain of the related item, e.g. "product"',
289
				'type' => 'string', 'default' => '', 'required' => true,
290
			],
291
			'customer.lists.type' => [
292
				'label' => 'Customer relationship type, e.g. "favorite"',
293
				'type' => 'string', 'default' => '', 'required' => false,
294
			],
295
			'customer.lists.config' => [
296
				'label' => 'Associative list of key/value configuration pairs',
297
				'type' => 'string', 'default' => '[]', 'required' => false,
298
			],
299
			'customer.lists.datestart' => [
300
				'label' => 'Start date when the relationship is valied',
301
				'type' => 'string', 'default' => '', 'required' => false,
302
			],
303
			'customer.lists.dateend' => [
304
				'label' => 'End date until the relationship is valid',
305
				'type' => 'string', 'default' => '', 'required' => false,
306
			],
307
			'customer.lists.status' => [
308
				'label' => 'Status of the relationship (0=disable, 1=enabled)',
309
				'type' => 'string', 'default' => '1', 'required' => false,
310
			],
311
		];
312
313
		$tplconf = 'client/jsonapi/standard/template-options';
314
		$default = 'options-standard.php';
315
316
		$body = $view->render( $view->config( $tplconf, $default ) );
317
318
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
319
			->withHeader( 'Cache-Control', 'max-age=300' )
320
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
321
			->withBody( $view->response()->createStreamFromString( $body ) )
322
			->withStatus( 200 );
323
	}
324
325
326
	/**
327
	 * Returns the response object with the rendered header and body
328
	 *
329
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
330
	 * @param \Aimeos\MW\View\Iface $view View instance
331
	 * @param integer $status HTTP status code
332
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
333
	 */
334
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
335
	{
336
		/** client/jsonapi/customer/relationships/standard/template
337
		 * Relative path to the customer relationships JSON API template
338
		 *
339
		 * The template file contains the code and processing instructions
340
		 * to generate the result shown in the JSON API body. The
341
		 * configuration string is the path to the template file relative
342
		 * to the templates directory (usually in client/jsonapi/templates).
343
		 *
344
		 * You can overwrite the template file configuration in extensions and
345
		 * provide alternative templates. These alternative templates should be
346
		 * named like the default one but with the string "standard" replaced by
347
		 * an unique name. You may use the name of your project for this. If
348
		 * you've implemented an alternative client class as well, "standard"
349
		 * should be replaced by the name of the new class.
350
		 *
351
		 * @param string Relative path to the template creating the body for the JSON API
352
		 * @since 2017.07
353
		 * @category Developer
354
		 */
355
		$tplconf = 'client/jsonapi/customer/relationships/standard/template';
356
		$default = 'customer/relationships/standard.php';
357
358
		$body = $view->render( $view->config( $tplconf, $default ) );
359
360
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
361
			->withHeader( 'Cache-Control', 'no-cache, private' )
362
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
363
			->withBody( $view->response()->createStreamFromString( $body ) )
364
			->withStatus( $status );
365
	}
366
}
367