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

Standard   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 1
cbo 11
dl 0
loc 293
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 43 5
C post() 0 48 8
A options() 0 20 1
A createOrder() 0 8 1
A getBasket() 0 16 2
A getPaymentForm() 0 20 2
A getUrlConfirm() 0 9 1
A getUrlUpdate() 0 9 1
B render() 0 31 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\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 = $this->getErrorDetails( $e, 'controller/frontend' );
63
		}
64
		catch( \Aimeos\MShop\Exception $e )
65
		{
66
			$status = 404;
67
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
68
		}
69
		catch( \Exception $e )
70
		{
71
			$status = 500;
72
			$view->errors = $this->getErrorDetails( $e );
73
		}
74
75
		return $this->render( $response, $view, $status );
76
	}
77
78
79
	/**
80
	 * Creates or updates the resource or the resource list
81
	 *
82
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
83
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
84
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
85
	 */
86
	public function post( ServerRequestInterface $request, ResponseInterface $response )
87
	{
88
		$view = $this->getView();
89
90
		try
91
		{
92
			$body = (string) $request->getBody();
93
94
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
95
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
96
			}
97
98
			if( !isset( $payload->data->attributes->{'order.baseid'} ) ) {
99
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Required attribute "order.baseid" is missing' ), 400 );
100
			}
101
102
			$basket = $this->getBasket( $payload->data->attributes->{'order.baseid'} );
103
			$item = $this->createOrder( $payload->data->attributes->{'order.baseid'} );
104
105
			$view->form = $this->getPaymentForm( $basket, $item, (array) $payload->data->attributes );
106
			$view->items = $item;
107
			$view->total = 1;
108
109
			$status = 201;
110
		}
111
		catch( \Aimeos\Client\JsonApi\Exception $e )
112
		{
113
			$status = $e->getCode();
114
			$view->errors = $this->getErrorDetails( $e, 'client/jsonapi' );
115
		}
116
		catch( \Aimeos\Controller\Frontend\Exception $e )
117
		{
118
			$status = 403;
119
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
120
		}
121
		catch( \Aimeos\MShop\Exception $e )
122
		{
123
			$status = 404;
124
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
125
		}
126
		catch( \Exception $e )
127
		{
128
			$status = 500;
129
			$view->errors = $this->getErrorDetails( $e );
130
		}
131
132
		return $this->render( $response, $view, $status );
133
	}
134
135
136
	/**
137
	 * Returns the available REST verbs and the available parameters
138
	 *
139
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
140
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
141
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
142
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
143
	 */
144
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
145
	{
146
		$view = $this->getView();
147
		$view->attributes = [
148
			'order.baseid' => [
149
				'label' => 'ID of the stored basket (POST only)',
150
				'type' => 'string', 'default' => '', 'required' => true,
151
			],
152
		];
153
154
		$tplconf = 'client/jsonapi/standard/template-options';
155
		$default = 'options-standard.php';
156
157
		$body = $view->render( $view->config( $tplconf, $default ) );
158
159
		return $response->withHeader( 'Allow', 'GET,OPTIONS,POST' )
160
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
161
			->withBody( $view->response()->createStreamFromString( $body ) )
162
			->withStatus( 200 );
163
	}
164
165
166
	/**
167
	 * Adds and returns a new order item for the given order base ID
168
	 *
169
	 * @param string $baseId Unique order base ID
170
	 * @return \Aimeos\MShop\Order\Item\Iface New order item
171
	 */
172
	protected function createOrder( $baseId )
173
	{
174
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'order' );
175
		$item = $cntl->addItem( $baseId, 'jsonapi' );
176
		$cntl->block( $item );
177
178
		return $item;
179
	}
180
181
182
	/**
183
	 * Returns the basket object for the given ID
184
	 *
185
	 * @param string $basketId Unique order base ID
186
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Basket object including only the services
187
	 * @throws \Aimeos\Client\JsonApi\Exception If basket ID is not the same as stored before in the current session
188
	 */
189
	protected function getBasket( $basketId )
190
	{
191
		$baseId = $this->getContext()->getSession()->get( 'aimeos/order.baseid' );
192
193
		$parts = \Aimeos\MShop\Order\Manager\Base\Base::PARTS_SERVICE;
194
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'basket' );
195
		$basket = $cntl->load( $basketId, $parts, false );
196
197
		if( $baseId != $basket->getId() )
198
		{
199
			$msg = sprintf( 'No basket for the "order.baseid" ("%1$s") found', $baseId );
200
			throw new \Aimeos\Client\JsonApi\Exception( $msg, 403 );
201
		}
202
203
		return $basket;
204
	}
205
206
207
	/**
208
	 * Returns the form helper object for building the payment form in the frontend
209
	 *
210
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Saved basket object including payment service object
211
	 * @param \Aimeos\MShop\Order\Item\Iface $orderItem Saved order item created for the basket object
212
	 * @param array $attributes Associative list of payment data pairs
213
	 * @return \Aimeos\MShop\Common\Item\Helper\Form\Iface|null Form object with URL, parameters, etc.
214
	 * 	or null if no form data is required
215
	 */
216
	protected function getPaymentForm( \Aimeos\MShop\Order\Item\Base\Iface $basket,
217
		\Aimeos\MShop\Order\Item\Iface $orderItem, array $attributes )
218
	{
219
		$view = $this->getView();
220
		$service = $basket->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT );
221
222
		$config = array( 'absoluteUri' => true, 'namespace' => false );
223
		$args = array( 'code' => $service->getCode(), 'orderid' => $orderItem->getId() );
224
		$urls = array(
225
			'payment.url-success' => $this->getUrlConfirm( $view, $args, $config ),
226
			'payment.url-update' => $this->getUrlUpdate( $view, $args, $config ),
227
		);
228
229
		foreach( $service->getAttributes() as $item ) {
230
			$attributes[$item->getCode()] = $item->getValue();
231
		}
232
233
		$serviceCntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'service' );
234
		return $serviceCntl->process( $orderItem, $service->getServiceId(), $urls, $attributes );
235
	}
236
237
238
	/**
239
	 * Returns the URL to the confirm page.
240
	 *
241
	 * @param \Aimeos\MW\View\Iface $view View object
242
	 * @param array $params Parameters that should be part of the URL
243
	 * @param array $config Default URL configuration
244
	 * @return string URL string
245
	 */
246
	protected function getUrlConfirm( \Aimeos\MW\View\Iface $view, array $params, array $config )
247
	{
248
		$target = $view->config( 'client/html/checkout/confirm/url/target' );
249
		$cntl = $view->config( 'client/html/checkout/confirm/url/controller', 'checkout' );
250
		$action = $view->config( 'client/html/checkout/confirm/url/action', 'confirm' );
251
		$config = $view->config( 'client/html/checkout/confirm/url/config', $config );
252
253
		return $view->url( $target, $cntl, $action, $params, [], $config );
254
	}
255
256
257
	/**
258
	 * Returns the URL to the update page.
259
	 *
260
	 * @param \Aimeos\MW\View\Iface $view View object
261
	 * @param array $params Parameters that should be part of the URL
262
	 * @param array $config Default URL configuration
263
	 * @return string URL string
264
	 */
265
	protected function getUrlUpdate( \Aimeos\MW\View\Iface $view, array $params, array $config )
266
	{
267
		$target = $view->config( 'client/html/checkout/update/url/target' );
268
		$cntl = $view->config( 'client/html/checkout/update/url/controller', 'checkout' );
269
		$action = $view->config( 'client/html/checkout/update/url/action', 'update' );
270
		$config = $view->config( 'client/html/checkout/update/url/config', $config );
271
272
		return $view->url( $target, $cntl, $action, $params, [], $config );
273
	}
274
275
276
	/**
277
	 * Returns the response object with the rendered header and body
278
	 *
279
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
280
	 * @param \Aimeos\MW\View\Iface $view View instance
281
	 * @param integer $status HTTP status code
282
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
283
	 */
284
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
285
	{
286
		/** client/jsonapi/order/standard/template
287
		 * Relative path to the order JSON API template
288
		 *
289
		 * The template file contains the code and processing instructions
290
		 * to generate the result shown in the JSON API body. The
291
		 * configuration string is the path to the template file relative
292
		 * to the templates directory (usually in client/jsonapi/templates).
293
		 *
294
		 * You can overwrite the template file configuration in extensions and
295
		 * provide alternative templates. These alternative templates should be
296
		 * named like the default one but with the string "standard" replaced by
297
		 * an unique name. You may use the name of your project for this. If
298
		 * you've implemented an alternative client class as well, "standard"
299
		 * should be replaced by the name of the new class.
300
		 *
301
		 * @param string Relative path to the template creating the body of the JSON API
302
		 * @since 2017.03
303
		 * @category Developer
304
		 */
305
		$tplconf = 'client/jsonapi/order/standard/template';
306
		$default = 'order/standard.php';
307
308
		$body = $view->render( $view->config( $tplconf, $default ) );
309
310
		return $response->withHeader( 'Allow', 'GET,OPTIONS,POST' )
311
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
312
			->withBody( $view->response()->createStreamFromString( $body ) )
313
			->withStatus( $status );
314
	}
315
}
316