Completed
Push — master ( 445306...7875eb )
by Aimeos
02:41
created

Standard   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 9
dl 0
loc 241
rs 8.2608
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C delete() 0 57 10
C patch() 0 56 13
F post() 0 57 15
A render() 0 12 1

How to fix   Complexity   

Complex Class

Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.

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\Product;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API basket/product 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/product"
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( 'Position (ID) is missing' ) );
77
					}
78
79
					$this->controller->deleteProduct( $entry->id );
80
				}
81
			}
82
			else
83
			{
84
				$this->controller->deleteProduct( $relId );
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 = array( array(
95
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
96
				'detail' => $e->getTraceAsString(),
97
			) );
98
		}
99
		catch( \Exception $e )
100
		{
101
			$status = 500;
102
			$view->errors = array( array(
103
				'title' => $e->getMessage(),
104
				'detail' => $e->getTraceAsString(),
105
			) );
106
		}
107
108
		return $this->render( $response, $view, $status );
109
	}
110
111
112
	/**
113
	 * Updates the resource or the resource list partitially
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 patch( ServerRequestInterface $request, ResponseInterface $response )
120
	{
121
		$view = $this->getView();
122
123
		try
124
		{
125
			$body = (string) $request->getBody();
126
			$relId = $view->param( 'relatedid' );
127
			$this->controller->setType( $view->param( 'id', 'default' ) );
128
129
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) || !isset( $payload->data->attributes ) ) {
130
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
131
			}
132
133
			if( !is_array( $payload->data ) ) {
134
				$payload->data = [$payload->data];
135
			}
136
137
			foreach( $payload->data as $entry )
138
			{
139
				if( $relId !== '' && $relId !== null ) {
140
					$entry->id = $relId;
141
				}
142
143
				if( !isset( $entry->id ) ) {
144
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Position (ID) is missing' ) );
145
				}
146
147
				$qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 );
148
				$cfgAttrCodes = ( isset( $entry->attributes->codes ) ? (array) $entry->attributes->codes : [] );
149
150
				$this->controller->editProduct( $entry->id, $qty, $cfgAttrCodes );
151
			}
152
153
			$view->item = $this->controller->get();
154
			$status = 200;
155
		}
156
		catch( \Aimeos\MShop\Exception $e )
157
		{
158
			$status = 404;
159
			$view->errors = array( array(
160
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
161
				'detail' => $e->getTraceAsString(),
162
			) );
163
		}
164
		catch( \Exception $e )
165
		{
166
			$status = 500;
167
			$view->errors = array( array(
168
				'title' => $e->getMessage(),
169
				'detail' => $e->getTraceAsString(),
170
			) );
171
		}
172
173
		return $this->render( $response, $view, $status );
174
	}
175
176
177
	/**
178
	 * Creates or updates the resource or the resource list
179
	 *
180
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
181
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
182
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
183
	 */
184
	public function post( ServerRequestInterface $request, ResponseInterface $response )
185
	{
186
		$view = $this->getView();
187
188
		try
189
		{
190
			$body = (string) $request->getBody();
191
			$this->controller->setType( $view->param( 'id', 'default' ) );
192
193
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
194
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
195
			}
196
197
			if( !is_array( $payload->data ) ) {
198
				$payload->data = [$payload->data];
199
			}
200
201
			foreach( $payload->data as $entry )
202
			{
203
				if( !isset( $entry->attributes ) || !isset( $entry->attributes->productid ) ) {
204
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Product ID is missing' ) );
205
				}
206
207
				$qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 );
208
				$stocktype = ( isset( $entry->attributes->stocktype ) ? $entry->attributes->stocktype : 'default' );
209
				$variantAttrIds = ( isset( $entry->attributes->variant ) ? (array) $entry->attributes->variant : [] );
210
				$configAttrIds = ( isset( $entry->attributes->config ) ? (array) $entry->attributes->config : [] );
211
				$hiddenAttrIds = ( isset( $entry->attributes->hidden ) ? (array) $entry->attributes->hidden : [] );
212
				$customAttrIds = ( isset( $entry->attributes->custom ) ? (array) $entry->attributes->custom : [] );
213
214
				$this->controller->addProduct( $entry->attributes->productid, $qty, [],
215
					$variantAttrIds, $configAttrIds, $hiddenAttrIds, $customAttrIds, $stocktype );
216
			}
217
218
219
			$view->item = $this->controller->get();
220
			$status = 201;
221
		}
222
		catch( \Aimeos\MShop\Exception $e )
223
		{
224
			$status = 404;
225
			$view->errors = array( array(
226
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
227
				'detail' => $e->getTraceAsString(),
228
			) );
229
		}
230
		catch( \Exception $e )
231
		{
232
			$status = 500;
233
			$view->errors = array( array(
234
				'title' => $e->getMessage(),
235
				'detail' => $e->getTraceAsString(),
236
			) );
237
		}
238
239
		return $this->render( $response, $view, $status );
240
	}
241
242
243
	/**
244
	 * Returns the response object with the rendered header and body
245
	 *
246
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
247
	 * @param \Aimeos\MW\View\Iface $view View instance
248
	 * @param integer $status HTTP status code
249
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
250
	 */
251
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
252
	{
253
		$tplconf = 'client/jsonapi/basket/standard/template';
254
		$default = 'basket/default.php';
255
256
		$body = $view->render( $view->config( $tplconf, $default ) );
257
258
		return $response->withHeader( 'Allow', 'DELETE,GET,PATCH,POST' )
259
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
260
			->withBody( $view->response()->createStreamFromString( $body ) )
261
			->withStatus( $status );
262
	}
263
}
264