Completed
Push — master ( 8c9c9d...8ff506 )
by Aimeos
04:08
created

Standard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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