Completed
Push — master ( 81113d...22e795 )
by Aimeos
02:42
created

Standard::patch()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.439
c 0
b 0
f 0
cc 6
eloc 25
nc 16
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\Customer;
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
	 * Deletes 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 delete( ServerRequestInterface $request, ResponseInterface $response )
35
	{
36
		$view = $this->getView();
37
38
		try
39
		{
40
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
41
			$cntl->deleteItem( $view->param( 'id' ) );
1 ignored issue
show
Bug introduced by
The method deleteItem() does not seem to exist on object<Aimeos\Controller\Frontend\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
			$status = 200;
43
		}
44
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
0 ignored issues
show
Bug introduced by
The class Aimeos\Controller\Frontend\Customer\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
45
		{
46
			$status = 403;
47
			$view->errors = array( array(
48
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
49
				'detail' => $e->getTraceAsString(),
50
			) );
51
		}
52
		catch( \Aimeos\MShop\Exception $e )
53
		{
54
			$status = 404;
55
			$view->errors = array( array(
56
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
57
				'detail' => $e->getTraceAsString(),
58
			) );
59
		}
60
		catch( \Exception $e )
61
		{
62
			$status = 500;
63
			$view->errors = array( array(
64
				'title' => $e->getMessage(),
65
				'detail' => $e->getTraceAsString(),
66
			) );
67
		}
68
69
		return $this->render( $response, $view, $status );
70
	}
71
72
73
	/**
74
	 * Returns the resource or the resource list
75
	 *
76
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
77
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
78
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
79
	 */
80
	public function get( ServerRequestInterface $request, ResponseInterface $response )
81
	{
82
		$view = $this->getView();
83
84
		try
85
		{
86
			$ref = $view->param( 'include', array() );
87
88
			if( is_string( $ref ) ) {
89
				$ref = explode( ',', $ref );
90
			}
91
92
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
93
94
			$view->item = $cntl->getItem( $view->param( 'id' ), $ref );
95
			$status = 200;
96
		}
97
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
0 ignored issues
show
Bug introduced by
The class Aimeos\Controller\Frontend\Customer\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
98
		{
99
			$status = 403;
100
			$view->errors = array( array(
101
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
102
				'detail' => $e->getTraceAsString(),
103
			) );
104
		}
105
		catch( \Aimeos\MShop\Exception $e )
106
		{
107
			$status = 404;
108
			$view->errors = array( array(
109
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
110
				'detail' => $e->getTraceAsString(),
111
			) );
112
		}
113
		catch( \Exception $e )
114
		{
115
			$status = 500;
116
			$view->errors = array( array(
117
				'title' => $e->getMessage(),
118
				'detail' => $e->getTraceAsString(),
119
			) );
120
		}
121
122
		return $this->render( $response, $view, $status );
123
	}
124
125
126
	/**
127
	 * Updates the resource or the resource list partitially
128
	 *
129
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
130
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
131
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
132
	 */
133
	public function patch( ServerRequestInterface $request, ResponseInterface $response )
134
	{
135
		$view = $this->getView();
136
137
		try
138
		{
139
			$body = (string) $request->getBody();
140
141
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
142
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
143
			}
144
145
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
146
147
			$view->item = $cntl->editItem( $view->param( 'id' ), (array) $payload->data->attributes );
1 ignored issue
show
Bug introduced by
The method editItem() does not seem to exist on object<Aimeos\Controller\Frontend\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
			$status = 200;
149
		}
150
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
0 ignored issues
show
Bug introduced by
The class Aimeos\Controller\Frontend\Customer\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
151
		{
152
			$status = 403;
153
			$view->errors = array( array(
154
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
155
				'detail' => $e->getTraceAsString(),
156
			) );
157
		}
158
		catch( \Aimeos\MShop\Exception $e )
159
		{
160
			$status = 404;
161
			$view->errors = array( array(
162
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
163
				'detail' => $e->getTraceAsString(),
164
			) );
165
		}
166
		catch( \Exception $e )
167
		{
168
			$status = 500;
169
			$view->errors = array( array(
170
				'title' => $e->getMessage(),
171
				'detail' => $e->getTraceAsString(),
172
			) );
173
		}
174
175
		return $this->render( $response, $view, $status );
176
	}
177
178
179
	/**
180
	 * Creates or updates the resource or the resource list
181
	 *
182
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
183
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
184
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
185
	 */
186
	public function post( ServerRequestInterface $request, ResponseInterface $response )
187
	{
188
		$view = $this->getView();
189
190
		try
191
		{
192
			$body = (string) $request->getBody();
193
194
			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) {
195
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 );
196
			}
197
198
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' );
199
200
			$view->item = $cntl->addItem( (array) $payload->data->attributes );
1 ignored issue
show
Bug introduced by
The method addItem() does not seem to exist on object<Aimeos\Controller\Frontend\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
201
			$status = 201;
202
		}
203
		catch( \Aimeos\Controller\Frontend\Customer\Exception $e )
0 ignored issues
show
Bug introduced by
The class Aimeos\Controller\Frontend\Customer\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
204
		{
205
			$status = 403;
206
			$view->errors = array( array(
207
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
208
				'detail' => $e->getTraceAsString(),
209
			) );
210
		}
211
		catch( \Aimeos\MShop\Exception $e )
212
		{
213
			$status = 404;
214
			$view->errors = array( array(
215
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
216
				'detail' => $e->getTraceAsString(),
217
			) );
218
		}
219
		catch( \Exception $e )
220
		{
221
			$status = 500;
222
			$view->errors = array( array(
223
				'title' => $e->getMessage(),
224
				'detail' => $e->getTraceAsString(),
225
			) );
226
		}
227
228
		return $this->render( $response, $view, $status );
229
	}
230
231
232
	/**
233
	 * Returns the response object with the rendered header and body
234
	 *
235
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
236
	 * @param \Aimeos\MW\View\Iface $view View instance
237
	 * @param integer $status HTTP status code
238
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
239
	 */
240
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
241
	{
242
		/** client/jsonapi/customer/standard/template
243
		 * Relative path to the customer JSON API template
244
		 *
245
		 * The template file contains the code and processing instructions
246
		 * to generate the result shown in the JSON API body. The
247
		 * configuration string is the path to the template file relative
248
		 * to the templates directory (usually in client/jsonapi/templates).
249
		 *
250
		 * You can overwrite the template file configuration in extensions and
251
		 * provide alternative templates. These alternative templates should be
252
		 * named like the default one but with the string "standard" replaced by
253
		 * an unique name. You may use the name of your project for this. If
254
		 * you've implemented an alternative client class as well, "standard"
255
		 * should be replaced by the name of the new class.
256
		 *
257
		 * @param string Relative path to the template creating the body for the JSON API
258
		 * @since 2017.04
259
		 * @category Developer
260
		 */
261
		$tplconf = 'client/jsonapi/customer/standard/template';
262
		$default = 'customer/standard.php';
263
264
		$body = $view->render( $view->config( $tplconf, $default ) );
265
266
		return $response->withHeader( 'Allow', 'DELETE,GET,PATCH,POST' )
267
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
268
			->withBody( $view->response()->createStreamFromString( $body ) )
269
			->withStatus( $status );
270
	}
271
}
272