Completed
Push — master ( 0324bb...38783b )
by Aimeos
02:05
created

Standard::options()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 98
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 98
rs 8.3352
cc 1
eloc 71
nc 1
nop 2

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