Completed
Push — master ( 3272b0...853e83 )
by Aimeos
02:24 queued 12s
created

Standard::render()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Subscription;
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
	 * Cancels the subscription
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(), 'subscription' );
41
42
			$view->items = $cntl->cancel( $view->param( 'id' ) );
43
			$view->total = 1;
44
45
			$status = 200;
46
		}
47
		catch( \Aimeos\Controller\Frontend\Exception $e )
48
		{
49
			$status = 403;
50
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
51
		}
52
		catch( \Aimeos\MShop\Exception $e )
53
		{
54
			$status = 404;
55
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
56
		}
57
		catch( \Exception $e )
58
		{
59
			$status = 500;
60
			$view->errors = $this->getErrorDetails( $e );
61
		}
62
63
		return $this->render( $response, $view, $status );
64
	}
65
66
67
	/**
68
	 * Returns the resource or the resource list
69
	 *
70
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
71
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
72
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
73
	 */
74
	public function get( ServerRequestInterface $request, ResponseInterface $response )
75
	{
76
		$view = $this->getView();
77
78
		try
79
		{
80
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'subscription' );
81
82
			if( ( $id = $view->param( 'id' ) ) != '' )
83
			{
84
				$view->items = $cntl->getItem( $id );
85
				$view->total = 1;
86
			}
87
			else
88
			{
89
				$total = 0;
90
				$filter = $cntl->createFilter();
91
				$this->initCriteria( $filter, $view->param() );
92
93
				$view->items = $cntl->searchItems( $filter, $total );
94
				$view->total = $total;
95
			}
96
97
			$status = 200;
98
		}
99
		catch( \Aimeos\Controller\Frontend\Exception $e )
100
		{
101
			$status = 403;
102
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
103
		}
104
		catch( \Aimeos\MShop\Exception $e )
105
		{
106
			$status = 404;
107
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
108
		}
109
		catch( \Exception $e )
110
		{
111
			$status = 500;
112
			$view->errors = $this->getErrorDetails( $e );
113
		}
114
115
		return $this->render( $response, $view, $status );
116
	}
117
118
119
	/**
120
	 * Returns the available REST verbs and the available parameters
121
	 *
122
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
123
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
124
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
125
	 */
126
	public function options( ServerRequestInterface $request, ResponseInterface $response )
127
	{
128
		$view = $this->getView();
129
		$view->attributes = [];
130
131
		$tplconf = 'client/jsonapi/standard/template-options';
132
		$default = 'options-standard.php';
133
134
		$body = $view->render( $view->config( $tplconf, $default ) );
135
136
		return $response->withHeader( 'Allow', 'GET,OPTIONS,POST' )
137
			->withHeader( 'Cache-Control', 'max-age=300' )
138
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
139
			->withBody( $view->response()->createStreamFromString( $body ) )
140
			->withStatus( 200 );
141
	}
142
143
144
	/**
145
	 * Adds and returns a new subscription item for the given subscription base ID
146
	 *
147
	 * @param string $baseId Unique subscription base ID
148
	 * @return \Aimeos\MShop\Subscription\Item\Iface New subscription item
149
	 */
150
	protected function createSubscription( $baseId )
151
	{
152
		$context = $this->getContext();
153
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $context, 'subscription' );
154
155
		$item = $cntl->addItem( $baseId, 'jsonapi' );
156
		$cntl->block( $item );
157
158
		$context->getSession()->set( 'aimeos/subscriptionid', $item->getId() );
159
160
		return $item;
161
	}
162
163
164
	/**
165
	 * Returns the response object with the rendered header and body
166
	 *
167
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
168
	 * @param \Aimeos\MW\View\Iface $view View instance
169
	 * @param integer $status HTTP status code
170
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
171
	 */
172
	protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status )
173
	{
174
		/** client/jsonapi/subscription/standard/template
175
		 * Relative path to the subscription JSON API template
176
		 *
177
		 * The template file contains the code and processing instructions
178
		 * to generate the result shown in the JSON API body. The
179
		 * configuration string is the path to the template file relative
180
		 * to the templates directory (usually in client/jsonapi/templates).
181
		 *
182
		 * You can overwrite the template file configuration in extensions and
183
		 * provide alternative templates. These alternative templates should be
184
		 * named like the default one but with the string "standard" replaced by
185
		 * an unique name. You may use the name of your project for this. If
186
		 * you've implemented an alternative client class as well, "standard"
187
		 * should be replaced by the name of the new class.
188
		 *
189
		 * @param string Relative path to the template creating the body of the JSON API
190
		 * @since 2017.03
191
		 * @category Developer
192
		 */
193
		$tplconf = 'client/jsonapi/subscription/standard/template';
194
		$default = 'subscription/standard.php';
195
196
		$body = $view->render( $view->config( $tplconf, $default ) );
197
198
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
199
			->withHeader( 'Cache-Control', 'no-cache, private' )
200
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
201
			->withBody( $view->response()->createStreamFromString( $body ) )
202
			->withStatus( $status );
203
	}
204
}
205