Completed
Push — master ( 81113d...22e795 )
by Aimeos
02:42
created

Standard   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 9
dl 0
loc 272
rs 9.8
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C delete() 0 54 10
B get() 0 44 5
B patch() 0 44 6
C post() 0 50 8
A render() 0 12 1
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\Address;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API customer/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 "customer/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\Customer\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
62
			if( $relId === '' || $relId === null )
63
			{
64
				if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
65
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
66
				}
67
68
				if( !is_array( $payload->data ) ) {
69
					$payload->data = [$payload->data];
70
				}
71
72
				foreach( $payload->data as $entry )
73
				{
74
					if( !isset( $entry->id ) ) {
75
						throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 );
76
					}
77
78
					$this->controller->deleteAddressItem( $entry->id );
79
				}
80
			}
81
			else
82
			{
83
				$this->controller->deleteAddressItem( $relId );
84
			}
85
86
			$status = 200;
87
		}
88
		catch( \Aimeos\MShop\Exception $e )
89
		{
90
			$status = 404;
91
			$view->errors = array( array(
92
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
93
				'detail' => $e->getTraceAsString(),
94
			) );
95
		}
96
		catch( \Exception $e )
97
		{
98
			$status = 500;
99
			$view->errors = array( array(
100
				'title' => $e->getMessage(),
101
				'detail' => $e->getTraceAsString(),
102
			) );
103
		}
104
105
		return $this->render( $response, $view, $status );
106
	}
107
108
109
	/**
110
	 * Returns the resource or the resource list
111
	 *
112
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
113
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
114
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
115
	 */
116
	public function get( ServerRequestInterface $request, ResponseInterface $response )
117
	{
118
		$view = $this->getView();
119
120
		try
121
		{
122
			$relId = $view->param( 'relid' );
123
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
124
125
			if( $relId === null ) {
126
				$view->items = $cntl->getItem( $view->param( 'id' ) )->getAddressItems();
127
			} else {
128
				$view->items = $cntl->getAddressItem( $relId );
1 ignored issue
show
Bug introduced by
The method getAddressItem() does not seem to exist on object<Aimeos\Controller\Frontend\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
			}
130
131
			$status = 200;
132
		}
133
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
0 ignored issues
show
Bug introduced by
The class Aimeos\Controller\Frontend\Customer\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
134
		{
135
			$status = 403;
136
			$view->errors = array( array(
137
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
138
				'detail' => $e->getTraceAsString(),
139
			) );
140
		}
141
		catch( \Aimeos\MShop\Exception $e )
142
		{
143
			$status = 404;
144
			$view->errors = array( array(
145
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
146
				'detail' => $e->getTraceAsString(),
147
			) );
148
		}
149
		catch( \Exception $e )
150
		{
151
			$status = 500;
152
			$view->errors = array( array(
153
				'title' => $e->getMessage(),
154
				'detail' => $e->getTraceAsString(),
155
			) );
156
		}
157
158
		return $this->render( $response, $view, $status );
159
	}
160
161
162
	/**
163
	 * Updates the resource or the resource list partitially
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 patch( ServerRequestInterface $request, ResponseInterface $response )
170
	{
171
		$view = $this->getView();
172
173
		try
174
		{
175
			$body = (string) $request->getBody();
176
177
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
178
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
179
			}
180
181
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
182
183
			$view->items = $cntl->editAddressItem( $view->param( 'relid' ), (array) $payload->data->attributes );
1 ignored issue
show
Bug introduced by
The method editAddressItem() does not seem to exist on object<Aimeos\Controller\Frontend\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
			$status = 200;
185
		}
186
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
0 ignored issues
show
Bug introduced by
The class Aimeos\Controller\Frontend\Customer\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
187
		{
188
			$status = 403;
189
			$view->errors = array( array(
190
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
191
				'detail' => $e->getTraceAsString(),
192
			) );
193
		}
194
		catch( \Aimeos\MShop\Exception $e )
195
		{
196
			$status = 404;
197
			$view->errors = array( array(
198
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
199
				'detail' => $e->getTraceAsString(),
200
			) );
201
		}
202
		catch( \Exception $e )
203
		{
204
			$status = 500;
205
			$view->errors = array( array(
206
				'title' => $e->getMessage(),
207
				'detail' => $e->getTraceAsString(),
208
			) );
209
		}
210
211
		return $this->render( $response, $view, $status );
212
	}
213
214
215
	/**
216
	 * Creates or updates the resource or the resource list
217
	 *
218
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
219
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
220
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
221
	 */
222
	public function post( ServerRequestInterface $request, ResponseInterface $response )
223
	{
224
		$view = $this->getView();
225
226
		try
227
		{
228
			$list = [];
229
			$id = $view->param( 'id' );
230
			$body = (string) $request->getBody();
231
232
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
233
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
234
			}
235
236
			if( !is_array( $payload->data ) ) {
237
				$payload->data = [$payload->data];
238
			}
239
240
			foreach( $payload->data as $entry )
241
			{
242
				if( !isset( $entry->attributes ) ) {
243
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) );
244
				}
245
246
				$list[] = $this->controller->addAddressItem( $id, (array) $entry->attributes );
247
			}
248
249
250
			$view->items = $list;
251
			$status = 201;
252
		}
253
		catch( \Aimeos\MShop\Exception $e )
254
		{
255
			$status = 404;
256
			$view->errors = array( array(
257
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
258
				'detail' => $e->getTraceAsString(),
259
			) );
260
		}
261
		catch( \Exception $e )
262
		{
263
			$status = 500;
264
			$view->errors = array( array(
265
				'title' => $e->getMessage(),
266
				'detail' => $e->getTraceAsString(),
267
			) );
268
		}
269
270
		return $this->render( $response, $view, $status );
271
	}
272
273
274
	/**
275
	 * Returns the response object with the rendered header and body
276
	 *
277
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
278
	 * @param \Aimeos\MW\View\Iface $view View instance
279
	 * @param integer $status HTTP status code
280
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
281
	 */
282
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
283
	{
284
		$tplconf = 'client/jsonapi/customer/address/standard/template';
285
		$default = 'customer/address/standard.php';
286
287
		$body = $view->render( $view->config( $tplconf, $default ) );
288
289
		return $response->withHeader( 'Allow', 'DELETE,GET,PATCH,POST' )
290
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
291
			->withBody( $view->response()->createStreamFromString( $body ) )
292
			->withStatus( $status );
293
	}
294
}
295