Completed
Push — master ( e8440a...f62092 )
by Aimeos
02:50
created

Standard::options()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 112
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 112
rs 8.2857
cc 1
eloc 82
nc 1
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API standard 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
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
41
			$cntl->deleteItem( $view->param( 'id' ) );
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\Factory::createController( $this->getContext(), 'customer' );
84
85
			$view->item = $cntl->getItem( $view->param( 'id' ), $ref );
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 )
116
	{
117
		$view = $this->getView();
118
119
		try
120
		{
121
			$body = (string) $request->getBody();
122
123
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
124
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
125
			}
126
127
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
128
129
			$view->item = $cntl->editItem( $view->param( 'id' ), (array) $payload->data->attributes );
130
			$status = 200;
131
		}
132
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
133
		{
134
			$status = 403;
135
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
136
		}
137
		catch( \Aimeos\MShop\Exception $e )
138
		{
139
			$status = 404;
140
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
141
		}
142
		catch( \Exception $e )
143
		{
144
			$status = 500;
145
			$view->errors = $this->getErrorDetails( $e );
146
		}
147
148
		return $this->render( $response, $view, $status );
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\Factory::createController( $this->getContext(), 'customer' );
172
173
			$view->item = $cntl->addItem( (array) $payload->data->attributes );
174
			$status = 201;
175
		}
176
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
177
		{
178
			$status = 403;
179
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
180
		}
181
		catch( \Aimeos\MShop\Exception $e )
182
		{
183
			$status = 404;
184
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
185
		}
186
		catch( \Exception $e )
187
		{
188
			$status = 500;
189
			$view->errors = $this->getErrorDetails( $e );
190
		}
191
192
		return $this->render( $response, $view, $status );
193
	}
194
195
196
	/**
197
	 * Returns the available REST verbs and the available parameters
198
	 *
199
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
200
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
201
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
202
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
203
	 */
204
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
205
	{
206
		$view = $this->getView();
207
		$view->attributes = [
208
			'customer.salutation' => [
209
				'label' => 'Customer salutation, i.e. "comany" ,"mr", "mrs", "miss" or ""',
210
				'type' => 'string', 'default' => '', 'required' => false,
211
			],
212
			'customer.company' => [
213
				'label' => 'Company name',
214
				'type' => 'string', 'default' => '', 'required' => false,
215
			],
216
			'customer.vatid' => [
217
				'label' => 'VAT ID of the company',
218
				'type' => 'string', 'default' => '', 'required' => false,
219
			],
220
			'customer.title' => [
221
				'label' => 'Title of the customer',
222
				'type' => 'string', 'default' => '', 'required' => false,
223
			],
224
			'customer.firstname' => [
225
				'label' => 'First name of the customer',
226
				'type' => 'string', 'default' => '', 'required' => false,
227
			],
228
			'customer.lastname' => [
229
				'label' => 'Last name of the customer or full name',
230
				'type' => 'string', 'default' => '', 'required' => true,
231
			],
232
			'customer.address1' => [
233
				'label' => 'First address part like street',
234
				'type' => 'string', 'default' => '', 'required' => true,
235
			],
236
			'customer.address2' => [
237
				'label' => 'Second address part like house number',
238
				'type' => 'string', 'default' => '', 'required' => false,
239
			],
240
			'customer.address3' => [
241
				'label' => 'Third address part like flat number',
242
				'type' => 'string', 'default' => '', 'required' => false,
243
			],
244
			'customer.postal' => [
245
				'label' => 'Zip code of the city',
246
				'type' => 'string', 'default' => '', 'required' => false,
247
			],
248
			'customer.city' => [
249
				'label' => 'Name of the town/city',
250
				'type' => 'string', 'default' => '', 'required' => true,
251
			],
252
			'customer.state' => [
253
				'label' => 'Two letter code of the country state',
254
				'type' => 'string', 'default' => '', 'required' => false,
255
			],
256
			'customer.countryid' => [
257
				'label' => 'Two letter ISO country code',
258
				'type' => 'string', 'default' => '', 'required' => true,
259
			],
260
			'customer.languageid' => [
261
				'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"',
262
				'type' => 'string', 'default' => '', 'required' => false,
263
			],
264
			'customer.telephone' => [
265
				'label' => 'Telephone number consisting of option leading "+" and digits without spaces',
266
				'type' => 'string', 'default' => '', 'required' => false,
267
			],
268
			'customer.telefax' => [
269
				'label' => 'Faximile number consisting of option leading "+" and digits without spaces',
270
				'type' => 'string', 'default' => '', 'required' => false,
271
			],
272
			'customer.email' => [
273
				'label' => 'E-mail address',
274
				'type' => 'string', 'default' => '', 'required' => false,
275
			],
276
			'customer.website' => [
277
				'label' => 'Web site including "http://" or "https://"',
278
				'type' => 'string', 'default' => '', 'required' => false,
279
			],
280
			'customer.longitude' => [
281
				'label' => 'Longitude of the customer location as float value',
282
				'type' => 'float', 'default' => '', 'required' => false,
283
			],
284
			'customer.latitude' => [
285
				'label' => 'Latitude of the customer location as float value',
286
				'type' => 'float', 'default' => '', 'required' => false,
287
			],
288
			'customer.label' => [
289
				'label' => 'Label to identify the customer, usually the full name',
290
				'type' => 'string', 'default' => '', 'required' => true,
291
			],
292
			'customer.code' => [
293
				'label' => 'Unique customer identifier, usually e-mail address',
294
				'type' => 'string', 'default' => '', 'required' => true,
295
			],
296
			'customer.birthday' => [
297
				'label' => 'ISO date in YYYY-MM-DD format of the birthday',
298
				'type' => 'string', 'default' => '', 'required' => false,
299
			],
300
			'customer.status' => [
301
				'label' => 'Customer account status, i.e. "0" for disabled, "1" for enabled',
302
				'type' => 'integer', 'default' => '1', 'required' => false,
303
			],
304
		];
305
306
		$tplconf = 'client/jsonapi/standard/template-options';
307
		$default = 'options-standard.php';
308
309
		$body = $view->render( $view->config( $tplconf, $default ) );
310
311
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
312
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
313
			->withBody( $view->response()->createStreamFromString( $body ) )
314
			->withStatus( 200 );
315
	}
316
317
318
	/**
319
	 * Returns the response object with the rendered header and body
320
	 *
321
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
322
	 * @param \Aimeos\MW\View\Iface $view View instance
323
	 * @param integer $status HTTP status code
324
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
325
	 */
326
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
327
	{
328
		/** client/jsonapi/customer/standard/template
329
		 * Relative path to the customer JSON API template
330
		 *
331
		 * The template file contains the code and processing instructions
332
		 * to generate the result shown in the JSON API body. The
333
		 * configuration string is the path to the template file relative
334
		 * to the templates directory (usually in client/jsonapi/templates).
335
		 *
336
		 * You can overwrite the template file configuration in extensions and
337
		 * provide alternative templates. These alternative templates should be
338
		 * named like the default one but with the string "standard" replaced by
339
		 * an unique name. You may use the name of your project for this. If
340
		 * you've implemented an alternative client class as well, "standard"
341
		 * should be replaced by the name of the new class.
342
		 *
343
		 * @param string Relative path to the template creating the body for the JSON API
344
		 * @since 2017.04
345
		 * @category Developer
346
		 */
347
		$tplconf = 'client/jsonapi/customer/standard/template';
348
		$default = 'customer/standard.php';
349
350
		$body = $view->render( $view->config( $tplconf, $default ) );
351
352
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
353
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
354
			->withBody( $view->response()->createStreamFromString( $body ) )
355
			->withStatus( $status );
356
	}
357
}
358