Completed
Push — master ( 270945...1cb1eb )
by Aimeos
03:29
created

Standard::post()   C

Complexity

Conditions 12
Paths 90

Size

Total Lines 50
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 50
rs 6.9666
cc 12
nc 90
nop 2

How to fix   Complexity   

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-2020
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 string $path Name of the client, e.g "basket/address"
35
	 */
36
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, string $path )
37
	{
38
		parent::__construct( $context, $path );
39
40
		$this->controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->getContext() );
41
	}
42
43
44
	/**
45
	 * Deletes the resource or the resource list
46
	 *
47
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
48
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
49
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
50
	 */
51
	public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
52
	{
53
		$view = $this->getView();
54
55
		try
56
		{
57
			$this->clearCache();
58
			$this->controller->setType( $view->param( 'id', 'default' ) );
59
60
			$relId = $view->param( 'relatedid' );
61
			$body = (string) $request->getBody();
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->deleteAddress( $entry->id );
80
				}
81
			}
82
			else
83
			{
84
				$this->controller->deleteAddress( $relId );
85
			}
86
87
88
			$view->item = $this->controller->get();
89
			$status = 200;
90
		}
91
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
92
		{
93
			$status = 409;
94
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
95
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
96
		}
97
		catch( \Aimeos\MShop\Exception $e )
98
		{
99
			$status = 404;
100
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
101
		}
102
		catch( \Exception $e )
103
		{
104
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
105
			$view->errors = $this->getErrorDetails( $e );
106
		}
107
108
		return $this->render( $response, $view, $status );
109
	}
110
111
112
	/**
113
	 * Creates or updates the resource or the resource list
114
	 *
115
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
116
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
117
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
118
	 */
119
	public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
120
	{
121
		$view = $this->getView();
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->addAddress( $entry->id, (array) $entry->attributes );
145
			}
146
147
148
			$view->item = $this->controller->get();
149
			$status = 201;
150
		}
151
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
152
		{
153
			$status = 409;
154
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
155
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
156
		}
157
		catch( \Aimeos\MShop\Exception $e )
158
		{
159
			$status = 404;
160
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
161
		}
162
		catch( \Exception $e )
163
		{
164
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
165
			$view->errors = $this->getErrorDetails( $e );
166
		}
167
168
		return $this->render( $response, $view, $status );
169
	}
170
171
172
	/**
173
	 * Returns the available REST verbs and the available parameters
174
	 *
175
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
176
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
177
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
178
	 */
179
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
180
	{
181
		$view = $this->getView();
182
183
		$view->attributes = [
184
			'addressid' => [
185
				'label' => 'ID of the customer address',
186
				'type' => 'string', 'default' => '', 'required' => false,
187
			],
188
			'salutation' => [
189
				'label' => 'Customer salutation, i.e. "comany" ,"mr", "mrs", "miss" or ""',
190
				'type' => 'string', 'default' => '', 'required' => false,
191
			],
192
			'company' => [
193
				'label' => 'Company name',
194
				'type' => 'string', 'default' => '', 'required' => false,
195
			],
196
			'vatid' => [
197
				'label' => 'VAT ID of the company',
198
				'type' => 'string', 'default' => '', 'required' => false,
199
			],
200
			'title' => [
201
				'label' => 'Title of the customer',
202
				'type' => 'string', 'default' => '', 'required' => false,
203
			],
204
			'firstname' => [
205
				'label' => 'First name of the customer',
206
				'type' => 'string', 'default' => '', 'required' => false,
207
			],
208
			'lastname' => [
209
				'label' => 'Last name of the customer or full name',
210
				'type' => 'string', 'default' => '', 'required' => true,
211
			],
212
			'address1' => [
213
				'label' => 'First address part like street',
214
				'type' => 'string', 'default' => '', 'required' => true,
215
			],
216
			'address2' => [
217
				'label' => 'Second address part like house number',
218
				'type' => 'string', 'default' => '', 'required' => false,
219
			],
220
			'address3' => [
221
				'label' => 'Third address part like flat number',
222
				'type' => 'string', 'default' => '', 'required' => false,
223
			],
224
			'postal' => [
225
				'label' => 'Zip code of the city',
226
				'type' => 'string', 'default' => '', 'required' => false,
227
			],
228
			'city' => [
229
				'label' => 'Name of the town/city',
230
				'type' => 'string', 'default' => '', 'required' => true,
231
			],
232
			'state' => [
233
				'label' => 'Two letter code of the country state',
234
				'type' => 'string', 'default' => '', 'required' => false,
235
			],
236
			'countryid' => [
237
				'label' => 'Two letter ISO country code',
238
				'type' => 'string', 'default' => '', 'required' => true,
239
			],
240
			'languageid' => [
241
				'label' => 'Two or five letter ISO language code, e.g. "de" or "de_CH"',
242
				'type' => 'string', 'default' => '', 'required' => false,
243
			],
244
			'telephone' => [
245
				'label' => 'Telephone number consisting of optional leading "+" and digits without spaces',
246
				'type' => 'string', 'default' => '', 'required' => false,
247
			],
248
			'telefax' => [
249
				'label' => 'Facsimile number consisting of optional leading "+" and digits without spaces',
250
				'type' => 'string', 'default' => '', 'required' => false,
251
			],
252
			'email' => [
253
				'label' => 'E-mail address',
254
				'type' => 'string', 'default' => '', 'required' => false,
255
			],
256
			'website' => [
257
				'label' => 'Web site including "http://" or "https://"',
258
				'type' => 'string', 'default' => '', 'required' => false,
259
			],
260
			'longitude' => [
261
				'label' => 'Longitude of the customer location as float value',
262
				'type' => 'float', 'default' => '', 'required' => false,
263
			],
264
			'latitude' => [
265
				'label' => 'Latitude of the customer location as float value',
266
				'type' => 'float', 'default' => '', 'required' => false,
267
			],
268
		];
269
270
		$tplconf = 'client/jsonapi/standard/template-options';
271
		$default = 'options-standard';
272
273
		$body = $view->render( $view->config( $tplconf, $default ) );
274
275
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
276
			->withHeader( 'Cache-Control', 'max-age=300' )
277
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
278
			->withBody( $view->response()->createStreamFromString( $body ) )
279
			->withStatus( 200 );
280
	}
281
282
283
	/**
284
	 * Returns the response object with the rendered header and body
285
	 *
286
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
287
	 * @param \Aimeos\MW\View\Iface $view View instance
288
	 * @param int $status HTTP status code
289
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
290
	 */
291
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface
292
	{
293
		$tplconf = 'client/jsonapi/basket/standard/template';
294
		$default = 'basket/standard';
295
296
		$body = $view->render( $view->config( $tplconf, $default ) );
297
298
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
299
			->withHeader( 'Cache-Control', 'no-cache, private' )
300
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
301
			->withBody( $view->response()->createStreamFromString( $body ) )
302
			->withStatus( $status );
303
	}
304
}
305