Completed
Push — master ( d148de...222791 )
by Aimeos
02:25
created

Standard::options()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 99
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 99
rs 8.3103
cc 1
eloc 72
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\Basket\Address;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API basket/address client
19
 *
20
 * @package Client
21
 * @subpackage JsonApi
22
 */
23
class Standard
24
	extends \Aimeos\Client\JsonApi\Basket\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 "basket/address"
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\Basket\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
			$this->clearCache();
62
			$this->controller->setType( $view->param( 'id', 'default' ) );
63
64
			$relId = $view->param( 'relatedid' );
65
			$body = (string) $request->getBody();
66
67
			if( $relId === '' || $relId === null )
68
			{
69
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
70
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
71
				}
72
73
				if( !is_array( $payload->data ) ) {
74
					$payload->data = [$payload->data];
75
				}
76
77
				foreach( $payload->data as $entry )
78
				{
79
					if( !isset( $entry->id ) ) {
80
						throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Type (ID) is missing' ) );
81
					}
82
83
					$this->controller->setAddress( $entry->id, null );
84
				}
85
			}
86
			else
87
			{
88
				$this->controller->setAddress( $relId, null );
89
			}
90
91
92
			$view->item = $this->controller->get();
93
			$status = 200;
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
	 * Creates or updates 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 post( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
119
	{
120
		$view = $this->getView();
121
		$view->prefix = $prefix;
122
123
		try
124
		{
125
			$this->clearCache();
126
			$this->controller->setType( $view->param( 'id', 'default' ) );
127
128
			$body = (string) $request->getBody();
129
130
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
131
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
132
			}
133
134
			if( !is_array( $payload->data ) ) {
135
				$payload->data = [$payload->data];
136
			}
137
138
			foreach( $payload->data as $entry )
139
			{
140
				if( !isset( $entry->id ) || !isset( $entry->attributes ) ) {
141
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Address type or attributes are missing' ) );
142
				}
143
144
				$this->controller->setAddress( $entry->id, (array) $entry->attributes );
145
			}
146
147
148
			$view->item = $this->controller->get();
149
			$status = 201;
150
		}
151
		catch( \Aimeos\MShop\Exception $e )
152
		{
153
			$status = 404;
154
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
155
		}
156
		catch( \Exception $e )
157
		{
158
			$status = 500;
159
			$view->errors = $this->getErrorDetails( $e );
160
		}
161
162
		return $this->render( $response, $view, $status );
163
	}
164
165
166
	/**
167
	 * Returns the available REST verbs and the available parameters
168
	 *
169
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
170
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
171
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
172
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
173
	 */
174
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
175
	{
176
		$view = $this->getView();
177
		$view->prefix = $prefix;
178
179
		$view->attributes = [
180
			'order.address.salutation' => [
181
				'label' => 'Customer salutation, i.e. "comany" ,"mr", "mrs", "miss" or ""',
182
				'type' => 'string', 'default' => '', 'required' => false,
183
			],
184
			'order.address.company' => [
185
				'label' => 'Company name',
186
				'type' => 'string', 'default' => '', 'required' => false,
187
			],
188
			'order.address.vatid' => [
189
				'label' => 'VAT ID of the company',
190
				'type' => 'string', 'default' => '', 'required' => false,
191
			],
192
			'order.address.title' => [
193
				'label' => 'Title of the customer',
194
				'type' => 'string', 'default' => '', 'required' => false,
195
			],
196
			'order.address.firstname' => [
197
				'label' => 'First name of the customer',
198
				'type' => 'string', 'default' => '', 'required' => false,
199
			],
200
			'order.address.lastname' => [
201
				'label' => 'Last name of the customer or full name',
202
				'type' => 'string', 'default' => '', 'required' => true,
203
			],
204
			'order.address.address1' => [
205
				'label' => 'First address part like street',
206
				'type' => 'string', 'default' => '', 'required' => true,
207
			],
208
			'order.address.address2' => [
209
				'label' => 'Second address part like house number',
210
				'type' => 'string', 'default' => '', 'required' => false,
211
			],
212
			'order.address.address3' => [
213
				'label' => 'Third address part like flat number',
214
				'type' => 'string', 'default' => '', 'required' => false,
215
			],
216
			'order.address.postal' => [
217
				'label' => 'Zip code of the city',
218
				'type' => 'string', 'default' => '', 'required' => false,
219
			],
220
			'order.address.city' => [
221
				'label' => 'Name of the town/city',
222
				'type' => 'string', 'default' => '', 'required' => true,
223
			],
224
			'order.address.state' => [
225
				'label' => 'Two letter code of the country state',
226
				'type' => 'string', 'default' => '', 'required' => false,
227
			],
228
			'order.address.countryid' => [
229
				'label' => 'Two letter ISO country code',
230
				'type' => 'string', 'default' => '', 'required' => true,
231
			],
232
			'order.address.languageid' => [
233
				'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"',
234
				'type' => 'string', 'default' => '', 'required' => false,
235
			],
236
			'order.address.telephone' => [
237
				'label' => 'Telephone number consisting of option leading "+" and digits without spaces',
238
				'type' => 'string', 'default' => '', 'required' => false,
239
			],
240
			'order.address.telefax' => [
241
				'label' => 'Faximile number consisting of option leading "+" and digits without spaces',
242
				'type' => 'string', 'default' => '', 'required' => false,
243
			],
244
			'order.address.email' => [
245
				'label' => 'E-mail address',
246
				'type' => 'string', 'default' => '', 'required' => false,
247
			],
248
			'order.address.website' => [
249
				'label' => 'Web site including "http://" or "https://"',
250
				'type' => 'string', 'default' => '', 'required' => false,
251
			],
252
			'order.address.longitude' => [
253
				'label' => 'Longitude of the customer location as float value',
254
				'type' => 'float', 'default' => '', 'required' => false,
255
			],
256
			'order.address.latitude' => [
257
				'label' => 'Latitude of the customer location as float value',
258
				'type' => 'float', 'default' => '', 'required' => false,
259
			],
260
		];
261
262
		$tplconf = 'client/jsonapi/standard/template-options';
263
		$default = 'options-standard.php';
264
265
		$body = $view->render( $view->config( $tplconf, $default ) );
266
267
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
268
			->withHeader( 'Cache-Control', 'max-age=300' )
269
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
270
			->withBody( $view->response()->createStreamFromString( $body ) )
271
			->withStatus( 200 );
272
	}
273
274
275
	/**
276
	 * Returns the response object with the rendered header and body
277
	 *
278
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
279
	 * @param \Aimeos\MW\View\Iface $view View instance
280
	 * @param integer $status HTTP status code
281
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
282
	 */
283
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
284
	{
285
		$tplconf = 'client/jsonapi/basket/standard/template';
286
		$default = 'basket/standard.php';
287
288
		$body = $view->render( $view->config( $tplconf, $default ) );
289
290
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
291
			->withHeader( 'Cache-Control', 'no-cache, private' )
292
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
293
			->withBody( $view->response()->createStreamFromString( $body ) )
294
			->withStatus( $status );
295
	}
296
}
297