Completed
Push — master ( 46b74c...362497 )
by Aimeos
02:26
created

Standard::get()   B

Complexity

Conditions 5
Paths 20

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 8.6868
c 0
b 0
f 0
cc 5
eloc 30
nc 20
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\Order;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API standard 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
	/**
28
	 * Returns the resource or the resource list
29
	 *
30
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
31
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
32
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
33
	 */
34
	public function get( ServerRequestInterface $request, ResponseInterface $response )
35
	{
36
		$view = $this->getView();
37
38
		try
39
		{
40
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'order' );
41
42
			if( ( $id = $view->param( 'id' ) ) != '' )
43
			{
44
				$view->items = $cntl->getItem( $id );
45
				$view->total = 1;
46
			}
47
			else
48
			{
49
				$total = 0;
50
				$filter = $cntl->createFilter();
51
				$this->initCriteria( $filter, $view->param() );
52
53
				$view->items = $cntl->searchItems( $filter, $total );
54
				$view->total = $total;
55
			}
56
57
			$status = 200;
58
		}
59
		catch( \Aimeos\Controller\Frontend\Exception $e )
60
		{
61
			$status = 403;
62
			$view->errors = array( array(
63
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
64
				'detail' => $e->getTraceAsString(),
65
			) );
66
		}
67
		catch( \Aimeos\MShop\Exception $e )
68
		{
69
			$status = 404;
70
			$view->errors = array( array(
71
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
72
				'detail' => $e->getTraceAsString(),
73
			) );
74
		}
75
		catch( \Exception $e )
76
		{
77
			$status = 500;
78
			$view->errors = array( array(
79
				'title' => $e->getMessage(),
80
				'detail' => $e->getTraceAsString(),
81
			) );
82
		}
83
84
		return $this->render( $response, $view, $status );
85
	}
86
87
88
	/**
89
	 * Creates or updates the resource or the resource list
90
	 *
91
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
92
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
93
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
94
	 */
95
	public function post( ServerRequestInterface $request, ResponseInterface $response )
96
	{
97
		$view = $this->getView();
98
99
		try
100
		{
101
			$body = (string) $request->getBody();
102
103
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
104
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
105
			}
106
107
			if( !isset( $payload->data->attributes->{'order.baseid'} ) ) {
108
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Required attribute "order.baseid" is missing' ), 400 );
109
			}
110
111
			$basket = $this->getBasket( $payload->data->attributes->{'order.baseid'} );
112
			$item = $this->createOrder( $payload->data->attributes->{'order.baseid'} );
113
114
			$view->form = $this->getPaymentForm( $basket, $item, (array) $payload->data->attributes );
115
			$view->items = $item;
116
			$view->total = 1;
117
118
			$status = 201;
119
		}
120
		catch( \Aimeos\Client\JsonApi\Exception $e )
121
		{
122
			$status = $e->getCode();
123
			$view->errors = array( array(
124
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
125
				'detail' => $e->getTraceAsString(),
126
			) );
127
		}
128
		catch( \Aimeos\Controller\Frontend\Exception $e )
129
		{
130
			$status = 403;
131
			$view->errors = array( array(
132
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
133
				'detail' => $e->getTraceAsString(),
134
			) );
135
		}
136
		catch( \Aimeos\MShop\Exception $e )
137
		{
138
			$status = 404;
139
			$view->errors = array( array(
140
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
141
				'detail' => $e->getTraceAsString(),
142
			) );
143
		}
144
		catch( \Exception $e )
145
		{
146
			$status = 500;
147
			$view->errors = array( array(
148
				'title' => $e->getMessage(),
149
				'detail' => $e->getTraceAsString(),
150
			) );
151
		}
152
153
		return $this->render( $response, $view, $status );
154
	}
155
156
157
	/**
158
	 * Adds and returns a new order item for the given order base ID
159
	 *
160
	 * @param string $baseId Unique order base ID
161
	 * @return \Aimeos\MShop\Order\Item\Iface New order item
162
	 */
163
	protected function createOrder( $baseId )
164
	{
165
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'order' );
166
		$item = $cntl->addItem( $baseId, 'jsonapi' );
167
		$cntl->block( $item );
168
169
		return $item;
170
	}
171
172
173
	/**
174
	 * Returns the basket object for the given ID
175
	 *
176
	 * @param string $baseId Unique order base ID
0 ignored issues
show
Bug introduced by
There is no parameter named $baseId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
177
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Basket object including only the services
178
	 * @throws \Aimeos\Client\JsonApi\Exception If basket ID is not the same as stored before in the current session
179
	 */
180
	protected function getBasket( $basketId )
181
	{
182
		$baseId = $this->getContext()->getSession()->get( 'aimeos/order.baseid' );
183
184
		$parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE;
185
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'basket' );
186
		$basket = $cntl->load( $basketId, $parts, false );
187
188
		if( $baseId != $basket->getId() )
189
		{
190
			$msg = sprintf( 'No basket for the "order.baseid" ("%1$s") found', $baseId );
191
			throw new \Aimeos\Client\JsonApi\Exception( $msg, 403 );
192
		}
193
194
		return $basket;
195
	}
196
197
198
	/**
199
	 * Returns the form helper object for building the payment form in the frontend
200
	 *
201
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Saved basket object including payment service object
202
	 * @param \Aimeos\MShop\Order\Item\Iface $orderItem Saved order item created for the basket object
203
	 * @param array $attributes Associative list of payment data pairs
204
	 * @return \Aimeos\MShop\Common\Item\Helper\Form\Iface|null Form object with URL, parameters, etc.
205
	 * 	or null if no form data is required
206
	 */
207
	protected function getPaymentForm( \Aimeos\MShop\Order\Item\Base\Iface $basket,
208
		\Aimeos\MShop\Order\Item\Iface $orderItem, array $attributes )
209
	{
210
		$view = $this->getView();
211
		$service = $basket->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT );
212
213
		$config = array( 'absoluteUri' => true, 'namespace' => false );
214
		$args = array( 'code' => $service->getCode(), 'orderid' => $orderItem->getId() );
215
		$urls = array(
216
			'payment.url-success' => $this->getUrlConfirm( $view, $args, $config ),
217
			'payment.url-update' => $this->getUrlUpdate( $view, $args, $config ),
218
		);
219
220
		foreach( $service->getAttributes() as $item ) {
221
			$attributes[$item->getCode()] = $item->getValue();
222
		}
223
224
		$serviceCntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'service' );
225
		return $serviceCntl->process( $orderItem, $service->getServiceId(), $urls, $attributes );
226
	}
227
228
229
	/**
230
	 * Returns the URL to the confirm page.
231
	 *
232
	 * @param \Aimeos\MW\View\Iface $view View object
233
	 * @param array $params Parameters that should be part of the URL
234
	 * @param array $config Default URL configuration
235
	 * @return string URL string
236
	 */
237
	protected function getUrlConfirm( \Aimeos\MW\View\Iface $view, array $params, array $config )
238
	{
239
		$target = $view->config( 'client/html/checkout/confirm/url/target' );
240
		$cntl = $view->config( 'client/html/checkout/confirm/url/controller', 'checkout' );
241
		$action = $view->config( 'client/html/checkout/confirm/url/action', 'confirm' );
242
		$config = $view->config( 'client/html/checkout/confirm/url/config', $config );
243
244
		return $view->url( $target, $cntl, $action, $params, array(), $config );
245
	}
246
247
248
	/**
249
	 * Returns the URL to the update page.
250
	 *
251
	 * @param \Aimeos\MW\View\Iface $view View object
252
	 * @param array $params Parameters that should be part of the URL
253
	 * @param array $config Default URL configuration
254
	 * @return string URL string
255
	 */
256
	protected function getUrlUpdate( \Aimeos\MW\View\Iface $view, array $params, array $config )
257
	{
258
		$target = $view->config( 'client/html/checkout/update/url/target' );
259
		$cntl = $view->config( 'client/html/checkout/update/url/controller', 'checkout' );
260
		$action = $view->config( 'client/html/checkout/update/url/action', 'update' );
261
		$config = $view->config( 'client/html/checkout/update/url/config', $config );
262
263
		return $view->url( $target, $cntl, $action, $params, array(), $config );
264
	}
265
266
267
	/**
268
	 * Returns the response object with the rendered header and body
269
	 *
270
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
271
	 * @param \Aimeos\MW\View\Iface $view View instance
272
	 * @param integer $status HTTP status code
273
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
274
	 */
275
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
276
	{
277
		/** client/jsonapi/order/standard/template
278
		 * Relative path to the order JSON API template
279
		 *
280
		 * The template file contains the code and processing instructions
281
		 * to generate the result shown in the JSON API body. The
282
		 * configuration string is the path to the template file relative
283
		 * to the templates directory (usually in client/jsonapi/templates).
284
		 *
285
		 * You can overwrite the template file configuration in extensions and
286
		 * provide alternative templates. These alternative templates should be
287
		 * named like the default one but with the string "standard" replaced by
288
		 * an unique name. You may use the name of your project for this. If
289
		 * you've implemented an alternative client class as well, "standard"
290
		 * should be replaced by the name of the new class.
291
		 *
292
		 * @param string Relative path to the template creating the body of the JSON API
293
		 * @since 2017.03
294
		 * @category Developer
295
		 */
296
		$tplconf = 'client/jsonapi/order/standard/template';
297
		$default = 'order/standard.php';
298
299
		$body = $view->render( $view->config( $tplconf, $default ) );
300
301
		return $response->withHeader( 'Allow', 'GET,POST' )
302
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
303
			->withBody( $view->response()->createStreamFromString( $body ) )
304
			->withStatus( $status );
305
	}
306
}
307