Completed
Push — master ( b4fd6e...445306 )
by Aimeos
02:15
created

Standard::delete()   C

Complexity

Conditions 10
Paths 39

Size

Total Lines 59
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 6.5919
c 0
b 0
f 0
cc 10
eloc 31
nc 39
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Service;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API basket/service 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/service"
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->setService( $entry->id, null );
80
				}
81
			}
82
			else
83
			{
84
				$this->controller->setService( $relId, null );
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( 'Service type (ID) is missing' ) );
142
				}
143
144
				if( !isset( $entry->attributes ) ) {
145
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Service attributes are missing' ) );
146
				}
147
148
				if( !isset( $entry->attributes->{'service.id'} ) ) {
149
					throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Service ID in attributes is missing' ) );
150
				}
151
152
				$serviceId = $entry->attributes->{'service.id'};
153
				unset( $entry->attributes->{'service.id'} );
154
155
				$this->controller->setService( $entry->id, $serviceId, (array) $entry->attributes );
156
			}
157
158
159
			$view->items = $this->controller->get();
160
			$view->total = 1;
161
162
			$status = 201;
163
		}
164
		catch( \Aimeos\MShop\Exception $e )
165
		{
166
			$status = 404;
167
			$view->errors = array( array(
168
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
169
				'detail' => $e->getTraceAsString(),
170
			) );
171
		}
172
		catch( \Exception $e )
173
		{
174
			$status = 500;
175
			$view->errors = array( array(
176
				'title' => $e->getMessage(),
177
				'detail' => $e->getTraceAsString(),
178
			) );
179
		}
180
181
		return $this->render( $response, $view, $status );
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 integer $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, $status )
194
	{
195
		$tplconf = 'client/jsonapi/basket/standard/template';
196
		$default = 'basket/default.php';
197
198
		$body = $view->render( $view->config( $tplconf, $default ) );
199
200
		return $response->withHeader( 'Allow', 'DELETE,GET,PATCH,POST' )
201
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
202
			->withBody( $view->response()->createStreamFromString( $body ) )
203
			->withStatus( $status );
204
	}
205
}
206