Completed
Push — master ( 270945...1cb1eb )
by Aimeos
03:29
created

Standard   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
eloc 71
c 1
b 0
f 0
dl 0
loc 182
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 3 1
A __construct() 0 5 1
A render() 0 12 1
B post() 0 50 11
C delete() 0 58 13
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
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\Basket\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 string $path Name of the client, e.g "basket/coupon"
35
	 */
36
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, string $path )
37
	{
38
		parent::__construct( $context, $path );
39
40
		$this->controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->getContext() );
41
	}
42
43
44
	/**
45
	 * Deletes the resource or the resource list
46
	 *
47
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
48
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
49
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
50
	 */
51
	public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
52
	{
53
		$view = $this->getView();
54
55
		try
56
		{
57
			$this->clearCache();
58
			$this->controller->setType( $view->param( 'id', 'default' ) );
59
60
			$relId = $view->param( 'relatedid' );
61
			$body = (string) $request->getBody();
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\Plugin\Provider\Exception $e )
92
		{
93
			$status = 409;
94
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
95
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
96
		}
97
		catch( \Aimeos\MShop\Exception $e )
98
		{
99
			$status = 404;
100
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
101
		}
102
		catch( \Exception $e )
103
		{
104
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
105
			$view->errors = $this->getErrorDetails( $e );
106
		}
107
108
		return $this->render( $response, $view, $status );
109
	}
110
111
112
	/**
113
	 * Creates or updates the resource or the resource list
114
	 *
115
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
116
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
117
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
118
	 */
119
	public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
120
	{
121
		$view = $this->getView();
122
123
		try
124
		{
125
			$this->clearCache();
126
			$this->controller->setType( $view->param( 'id', 'default' ) );
127
128
			$body = (string) $request->getBody();
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->item = $this->controller->get();
149
			$status = 201;
150
		}
151
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
152
		{
153
			$status = 409;
154
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
155
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
156
		}
157
		catch( \Aimeos\MShop\Exception $e )
158
		{
159
			$status = 404;
160
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
161
		}
162
		catch( \Exception $e )
163
		{
164
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
165
			$view->errors = $this->getErrorDetails( $e );
166
		}
167
168
		return $this->render( $response, $view, $status );
169
	}
170
171
172
	/**
173
	 * Returns the available REST verbs and the available parameters
174
	 *
175
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
176
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
177
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
178
	 */
179
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
180
	{
181
		return $this->getOptionsResponse( $request, $response, 'DELETE,GET,OPTIONS,PATCH,POST' );
182
	}
183
184
185
	/**
186
	 * Returns the response object with the rendered header and body
187
	 *
188
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
189
	 * @param \Aimeos\MW\View\Iface $view View instance
190
	 * @param int $status HTTP status code
191
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
192
	 */
193
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface
194
	{
195
		$tplconf = 'client/jsonapi/basket/standard/template';
196
		$default = 'basket/standard';
197
198
		$body = $view->render( $view->config( $tplconf, $default ) );
199
200
		return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' )
201
			->withHeader( 'Cache-Control', 'no-cache, private' )
202
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
203
			->withBody( $view->response()->createStreamFromString( $body ) )
204
			->withStatus( $status );
205
	}
206
}
207