Completed
Push — master ( ba7de8...b94793 )
by Aimeos
02:24
created

Standard::post()   B

Complexity

Conditions 3
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 14
nc 5
nop 2
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;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API basket 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"
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
			$status = 200;
60
			$type = $view->param( 'id', 'default' );
61
			$view->item = $this->controller->setType( $type )->clear()->get();
62
		}
63
		catch( \Aimeos\MShop\Exception $e )
64
		{
65
			$status = 404;
66
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
67
		}
68
		catch( \Exception $e )
69
		{
70
			$status = 500;
71
			$view->errors = $this->getErrorDetails( $e );
72
		}
73
74
		return $this->render( $response, $view, $status );
75
	}
76
77
78
	/**
79
	 * Returns the resource or the resource list
80
	 *
81
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
82
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
83
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
84
	 */
85
	public function get( ServerRequestInterface $request, ResponseInterface $response )
86
	{
87
		$allow = false;
88
		$view = $this->getView();
89
		$id = $view->param( 'id', 'default' );
90
91
		try
92
		{
93
			try
94
			{
95
				$view->item = $this->controller->load( $id, $this->getParts( $view ) );
96
			}
97
			catch( \Aimeos\MShop\Exception $e )
98
			{
99
				$view->item = $this->controller->setType( $id )->get();
100
				$allow = true;
101
			}
102
103
			$status = 200;
104
		}
105
		catch( \Aimeos\MShop\Exception $e )
106
		{
107
			$status = 404;
108
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
109
		}
110
		catch( \Exception $e )
111
		{
112
			$status = 500;
113
			$view->errors = $this->getErrorDetails( $e );
114
		}
115
116
		return $this->render( $response, $view, $status, $allow );
117
	}
118
119
120
	/**
121
	 * Updates the resource or the resource list partitially
122
	 *
123
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
124
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
125
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
126
	 */
127
	public function patch( ServerRequestInterface $request, ResponseInterface $response )
128
	{
129
		$view = $this->getView();
130
131
		try
132
		{
133
			$body = (string) $request->getBody();
134
135
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
136
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
137
			}
138
139
			$basket = $this->controller->setType( $view->param( 'id', 'default' ) )->get();
140
			$basket->fromArray( (array) $payload->data->attributes );
141
142
			$view->item = $basket;
143
			$status = 200;
144
		}
145
		catch( \Aimeos\MShop\Exception $e )
146
		{
147
			$status = 404;
148
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
149
		}
150
		catch( \Exception $e )
151
		{
152
			$status = 500;
153
			$view->errors = $this->getErrorDetails( $e );
154
		}
155
156
		return $this->render( $response, $view, $status );
157
	}
158
159
160
	/**
161
	 * Creates or updates the resource or the resource list
162
	 *
163
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
164
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
165
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
166
	 */
167
	public function post( ServerRequestInterface $request, ResponseInterface $response )
168
	{
169
		$view = $this->getView();
170
171
		try
172
		{
173
			$item = $this->controller->setType( $view->param( 'id', 'default' ) )->store();
174
			$this->getContext()->getSession()->set( 'aimeos/order.baseid', $item->getId() );
175
176
			$view->item = $item;
177
			$status = 200;
178
		}
179
		catch( \Aimeos\MShop\Exception $e )
180
		{
181
			$status = 404;
182
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
183
		}
184
		catch( \Exception $e )
185
		{
186
			$status = 500;
187
			$view->errors = $this->getErrorDetails( $e );
188
		}
189
190
		return $this->render( $response, $view, $status );
191
	}
192
193
194
	/**
195
	 * Returns the integer constant for the basket parts that should be included
196
	 *
197
	 * @param \Aimeos\MW\View\Iface $view View instance
198
	 * @return integer Constant from Aimeos\MShop\Order\Manager\Base\Base
199
	 */
200
	protected function getParts( \Aimeos\MW\View\Iface $view )
201
	{
202
		$available = array(
203
			'basket/address' => \Aimeos\MShop\Order\Manager\Base\Base::PARTS_ADDRESS,
204
			'basket/coupon' => \Aimeos\MShop\Order\Manager\Base\Base::PARTS_COUPON,
205
			'basket/product' => \Aimeos\MShop\Order\Manager\Base\Base::PARTS_PRODUCT,
206
			'basket/service' => \Aimeos\MShop\Order\Manager\Base\Base::PARTS_SERVICE,
207
		);
208
209
		$included = explode( ',', $view->param( 'included', 'basket/address,basket/coupon,basket/product,basket/service' ) );
210
211
		$parts = \Aimeos\MShop\Order\Manager\Base\Base::PARTS_NONE;
212
		foreach( $included as $type )
213
		{
214
			if( isset( $available[$type] ) ) {
215
				$parts |= $available[$type];
216
			}
217
		}
218
219
		return $parts;
220
	}
221
222
223
	/**
224
	 * Returns the response object with the rendered header and body
225
	 *
226
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
227
	 * @param \Aimeos\MW\View\Iface $view View instance
228
	 * @param integer $status HTTP status code
229
	 * @param boolean $allow True to allow all HTTP methods, false for GET only
230
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
231
	 */
232
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status, $allow = true )
233
	{
234
		/** client/jsonapi/basket/standard/template
235
		 * Relative path to the basket JSON API template
236
		 *
237
		 * The template file contains the code and processing instructions
238
		 * to generate the result shown in the JSON API body. The
239
		 * configuration string is the path to the template file relative
240
		 * to the templates directory (usually in client/jsonapi/templates).
241
		 *
242
		 * You can overwrite the template file configuration in extensions and
243
		 * provide alternative templates. These alternative templates should be
244
		 * named like the default one but with the string "standard" replaced by
245
		 * an unique name. You may use the name of your project for this. If
246
		 * you've implemented an alternative client class as well, "standard"
247
		 * should be replaced by the name of the new class.
248
		 *
249
		 * @param string Relative path to the template creating the body for the JSON API
250
		 * @since 2017.04
251
		 * @category Developer
252
		 */
253
		$tplconf = 'client/jsonapi/basket/standard/template';
254
		$default = 'basket/standard.php';
255
256
		$body = $view->render( $view->config( $tplconf, $default ) );
257
258
		if( $allow === true ) {
259
			$methods = 'DELETE,GET,PATCH,POST';
260
		} else {
261
			$methods = 'GET';
262
		}
263
264
		return $response->withHeader( 'Allow', $methods )
265
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
266
			->withBody( $view->response()->createStreamFromString( $body ) )
267
			->withStatus( $status );
268
	}
269
}
270