Passed
Push — master ( 8aaa8e...6a4321 )
by Aimeos
02:10
created

Standard::get()   B

Complexity

Conditions 7
Paths 23

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 39
rs 8.6506
c 0
b 0
f 0
nc 23
nop 2
cc 7
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\Property;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API customer/property 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
	/**
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
			$body = (string) $request->getBody();
41
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
42
			$items = $cntl->uses( ['customer/property'] )->get()->getPropertyItems();
43
44
			if( ( $relId = $view->param( 'relatedid' ) ) === null )
45
			{
46
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
47
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
48
				}
49
50
				if( !is_array( $payload->data ) ) {
51
					$payload->data = [$payload->data];
52
				}
53
54
				foreach( $payload->data as $entry )
55
				{
56
					if( !isset( $entry->id ) ) {
57
						throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 );
58
					}
59
60
					if( isset( $items[$entry->id] ) ) {
61
						$cntl->deletePropertyItem( $items[$entry->id] );
62
					}
63
				}
64
65
				$cntl->store();
66
			}
67
			else
68
			{
69
				if( isset( $items[$relId] ) ) {
70
					$cntl->deletePropertyItem( $items[$relId] )->store();
71
				}
72
			}
73
74
			$status = 200;
75
		}
76
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
77
		{
78
			$status = 403;
79
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
80
		}
81
		catch( \Aimeos\MShop\Exception $e )
82
		{
83
			$status = 404;
84
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
85
		}
86
		catch( \Exception $e )
87
		{
88
			$status = 500;
89
			$view->errors = $this->getErrorDetails( $e );
90
		}
91
92
		return $this->render( $response, $view, $status );
93
	}
94
95
96
	/**
97
	 * Returns the resource or the resource list
98
	 *
99
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
100
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
101
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
102
	 */
103
	public function get( ServerRequestInterface $request, ResponseInterface $response )
104
	{
105
		$view = $this->getView();
106
107
		try
108
		{
109
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
110
			$items = $cntl->uses( ['customer/property'] )->get()->getPropertyItems();
111
112
			if( ( $relId = $view->param( 'relatedid' ) ) == null )
113
			{
114
				$view->items = $items;
115
				$view->total = count( $items );
116
			}
117
			else
118
			{
119
				$view->items = isset( $items[$relId] ) ? $items[$relId] : null;
120
				$view->total = empty( $view->items ) ? 0 : 1;
121
			}
122
123
			$status = 200;
124
		}
125
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
126
		{
127
			$status = 403;
128
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
129
		}
130
		catch( \Aimeos\MShop\Exception $e )
131
		{
132
			$status = 404;
133
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
134
		}
135
		catch( \Exception $e )
136
		{
137
			$status = 500;
138
			$view->errors = $this->getErrorDetails( $e );
139
		}
140
141
		return $this->render( $response, $view, $status );
142
	}
143
144
145
	/**
146
	 * Updates the resource or the resource list partitially
147
	 *
148
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
149
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
150
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
151
	 */
152
	public function patch( ServerRequestInterface $request, ResponseInterface $response )
153
	{
154
		$view = $this->getView();
155
156
		try
157
		{
158
			$body = (string) $request->getBody();
159
160
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
161
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
162
			}
163
164
			$status = 404;
165
			$view->total = 0;
166
			$relId = $view->param( 'relatedid' );
167
168
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
169
			$items = $cntl->uses( ['customer/property'] )->get()->getPropertyItems();
170
171
			if( isset( $items[$relId] ) )
172
			{
173
				$attributes = (array) $payload->data->attributes;
174
				$propItem = $items[$relId]->fromArray( $attributes );
175
				$cntl->addPropertyItem( $propItem )->store();
176
177
				$view->items = $propItem;
178
				$view->total = 1;
179
				$status = 200;
180
			}
181
		}
182
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
183
		{
184
			$status = 403;
185
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
186
		}
187
		catch( \Aimeos\MShop\Exception $e )
188
		{
189
			$status = 404;
190
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
191
		}
192
		catch( \Exception $e )
193
		{
194
			$status = 500;
195
			$view->errors = $this->getErrorDetails( $e );
196
		}
197
198
		return $this->render( $response, $view, $status );
199
	}
200
201
202
	/**
203
	 * Creates or updates the resource or the resource list
204
	 *
205
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
206
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
207
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
208
	 */
209
	public function post( ServerRequestInterface $request, ResponseInterface $response )
210
	{
211
		$view = $this->getView();
212
213
		try
214
		{
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
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' )->uses( ['customer/property'] );
226
227
			foreach( $payload->data as $entry )
228
			{
229
				if( !isset( $entry->attributes ) ) {
230
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) );
231
				}
232
233
				$propItem = $cntl->createPropertyItem( (array) $entry->attributes );
234
				$cntl->addPropertyItem( $propItem );
235
			}
236
237
			$view->items = $cntl->store()->get()->getPropertyItems();
238
			$view->total = count( $view->items );
239
			$status = 201;
240
		}
241
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
242
		{
243
			$status = 403;
244
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
245
		}
246
		catch( \Aimeos\MShop\Exception $e )
247
		{
248
			$status = 404;
249
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
250
		}
251
		catch( \Exception $e )
252
		{
253
			$status = 500;
254
			$view->errors = $this->getErrorDetails( $e );
255
		}
256
257
		return $this->render( $response, $view, $status );
258
	}
259
260
261
	/**
262
	 * Returns the available REST verbs and the available parameters
263
	 *
264
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
265
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
266
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
267
	 */
268
	public function options( ServerRequestInterface $request, ResponseInterface $response )
269
	{
270
		$view = $this->getView();
271
272
		$view->attributes = [
273
			'customer.property.type' => [
274
				'label' => 'Code of the property type',
275
				'type' => 'string', 'default' => '', 'required' => true,
276
			],
277
			'customer.property.languageid' => [
278
				'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"',
279
				'type' => 'string', 'default' => '', 'required' => false,
280
			],
281
			'customer.property.value' => [
282
				'label' => 'Arbitrary property value',
283
				'type' => 'string', 'default' => '', 'required' => false,
284
			],
285
		];
286
287
		$tplconf = 'client/jsonapi/standard/template-options';
288
		$default = 'options-standard';
289
290
		$body = $view->render( $view->config( $tplconf, $default ) );
291
292
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
293
			->withHeader( 'Cache-Control', 'max-age=300' )
294
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
295
			->withBody( $view->response()->createStreamFromString( $body ) )
296
			->withStatus( 200 );
297
	}
298
299
300
	/**
301
	 * Returns the response object with the rendered header and body
302
	 *
303
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
304
	 * @param \Aimeos\MW\View\Iface $view View instance
305
	 * @param integer $status HTTP status code
306
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
307
	 */
308
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
309
	{
310
		/** client/jsonapi/customer/property/standard/template
311
		 * Relative path to the customer property JSON API template
312
		 *
313
		 * The template file contains the code and processing instructions
314
		 * to generate the result shown in the JSON API body. The
315
		 * configuration string is the path to the template file relative
316
		 * to the templates directory (usually in client/jsonapi/templates).
317
		 *
318
		 * You can overwrite the template file configuration in extensions and
319
		 * provide alternative templates. These alternative templates should be
320
		 * named like the default one but with the string "standard" replaced by
321
		 * an unique name. You may use the name of your project for this. If
322
		 * you've implemented an alternative client class as well, "standard"
323
		 * should be replaced by the name of the new class.
324
		 *
325
		 * @param string Relative path to the template creating the body for the JSON API
326
		 * @since 2017.07
327
		 * @category Developer
328
		 */
329
		$tplconf = 'client/jsonapi/customer/property/standard/template';
330
		$default = 'customer/property/standard';
331
332
		$body = $view->render( $view->config( $tplconf, $default ) );
333
334
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
335
			->withHeader( 'Cache-Control', 'no-cache, private' )
336
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
337
			->withBody( $view->response()->createStreamFromString( $body ) )
338
			->withStatus( $status );
339
	}
340
}
341