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

Standard::options()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

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