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

Standard::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 3
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\Coupon;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API basket/coupon 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/coupon"
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( 'Type (ID) is missing' ) );
77
					}
78
79
					$this->controller->deleteCoupon( $entry->id );
80
				}
81
			}
82
			else
83
			{
84
				$this->controller->deleteCoupon( $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 = $this->getErrorDetails( $e, 'mshop' );
95
		}
96
		catch( \Exception $e )
97
		{
98
			$status = 500;
99
			$view->errors = $this->getErrorDetails( $e );
100
		}
101
102
		return $this->render( $response, $view, $status );
103
	}
104
105
106
	/**
107
	 * Creates or updates the resource or the resource list
108
	 *
109
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
110
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
111
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
112
	 */
113
	public function post( ServerRequestInterface $request, ResponseInterface $response )
114
	{
115
		$view = $this->getView();
116
117
		try
118
		{
119
			$body = (string) $request->getBody();
120
			$this->controller->setType( $view->param( 'id', 'default' ) );
121
122
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
123
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
124
			}
125
126
			if( !is_array( $payload->data ) ) {
127
				$payload->data = [$payload->data];
128
			}
129
130
			foreach( $payload->data as $entry )
131
			{
132
				if( !isset( $entry->id ) ) {
133
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Coupon code is missing' ) );
134
				}
135
136
				$this->controller->addCoupon( $entry->id );
137
			}
138
139
140
			$view->item = $this->controller->get();
141
			$status = 201;
142
		}
143
		catch( \Aimeos\MShop\Exception $e )
144
		{
145
			$status = 404;
146
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
147
		}
148
		catch( \Exception $e )
149
		{
150
			$status = 500;
151
			$view->errors = $this->getErrorDetails( $e );
152
		}
153
154
		return $this->render( $response, $view, $status );
155
	}
156
157
158
	/**
159
	 * Returns the available REST verbs and the available parameters
160
	 *
161
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
162
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
163
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
164
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
165
	 */
166
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
167
	{
168
		return $this->getOptionsResponse( $request, $response, $prefix, 'DELETE,GET,OPTIONS,PATCH,POST' );
169
	}
170
171
172
	/**
173
	 * Returns the response object with the rendered header and body
174
	 *
175
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
176
	 * @param \Aimeos\MW\View\Iface $view View instance
177
	 * @param integer $status HTTP status code
178
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
179
	 */
180
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
181
	{
182
		$tplconf = 'client/jsonapi/basket/standard/template';
183
		$default = 'basket/standard.php';
184
185
		$body = $view->render( $view->config( $tplconf, $default ) );
186
187
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
188
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
189
			->withBody( $view->response()->createStreamFromString( $body ) )
190
			->withStatus( $status );
191
	}
192
}
193