Completed
Push — master ( e8440a...f62092 )
by Aimeos
02:50
created

Standard::options()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 16
nc 1
nop 3
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\Stock;
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
	 * Returns 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 get( ServerRequestInterface $request, ResponseInterface $response )
35
	{
36
		$view = $this->getView();
37
38
		try
39
		{
40
			if( $view->param( 'id' ) != '' ) {
41
				$response = $this->getItem( $view, $request, $response );
42
			} else {
43
				$response = $this->getItems( $view, $request, $response );
44
			}
45
46
			$status = 200;
47
		}
48
		catch( \Aimeos\MShop\Exception $e )
49
		{
50
			$status = 404;
51
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
52
		}
53
		catch( \Exception $e )
54
		{
55
			$status = 500;
56
			$view->errors = $this->getErrorDetails( $e );
57
		}
58
59
		/** client/jsonapi/stock/standard/template
60
		 * Relative path to the catalog lists JSON API template
61
		 *
62
		 * The template file contains the code and processing instructions
63
		 * to generate the result shown in the JSON API body. The
64
		 * configuration string is the path to the template file relative
65
		 * to the templates directory (usually in client/jsonapi/templates).
66
		 *
67
		 * You can overwrite the template file configuration in extensions and
68
		 * provide alternative templates. These alternative templates should be
69
		 * named like the default one but with the string "default" replaced by
70
		 * an unique name. You may use the name of your project for this. If
71
		 * you've implemented an alternative client class as well, "standard"
72
		 * should be replaced by the name of the new class.
73
		 *
74
		 * @param string Relative path to the template creating the body for the GET method of the JSON API
75
		 * @since 2017.03
76
		 * @category Developer
77
		 */
78
		$tplconf = 'client/jsonapi/stock/standard/template';
79
		$default = 'stock/standard.php';
80
81
		$body = $view->render( $view->config( $tplconf, $default ) );
82
83
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
84
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
85
			->withBody( $view->response()->createStreamFromString( $body ) )
86
			->withStatus( $status );
87
	}
88
89
90
	/**
91
	 * Returns the available REST verbs and the available parameters
92
	 *
93
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
94
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
95
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
96
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
97
	 */
98
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
99
	{
100
		$view = $this->getView();
101
		$view->filter = [
102
			's_prodcode' => [
103
				'label' => 'List of product codes for which the stock level should be returned',
104
				'type' => 'array', 'default' => '[]', 'required' => false,
105
			],
106
			's_typecode' => [
107
				'label' => 'List of warehouse/location codes (stock type)',
108
				'type' => 'array', 'default' => '[]', 'required' => false,
109
			],
110
		];
111
112
		$tplconf = 'client/jsonapi/standard/template-options';
113
		$default = 'options-standard.php';
114
115
		$body = $view->render( $view->config( $tplconf, $default ) );
116
117
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
118
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
119
			->withBody( $view->response()->createStreamFromString( $body ) )
120
			->withStatus( 200 );
121
	}
122
123
124
	/**
125
	 * Retrieves the item and adds the data to the view
126
	 *
127
	 * @param \Aimeos\MW\View\Iface $view View instance
128
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
129
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
130
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
131
	 */
132
	protected function getItem( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response )
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
	{
134
		$context = $this->getContext();
135
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $context, 'stock' );
136
137
		$view->items = $cntl->getItem( $view->param( 'id' ) );
138
		$view->total = 1;
139
140
		return $response;
141
	}
142
143
144
	/**
145
	 * Retrieves the items and adds the data to the view
146
	 *
147
	 * @param \Aimeos\MW\View\Iface $view View instance
148
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
149
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
150
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
151
	 */
152
	protected function getItems( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response )
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
153
	{
154
		$total = 0;
155
		$params = $view->param( 'filter', [] );
156
		unset( $params['s_prodcode'], $params['s_typecode'] );
157
158
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'stock' );
159
160
		$filter = $cntl->createFilter();
161
		$filter = $this->initCriteria( $filter, ['filter' => $params] );
162
		$filter = $cntl->addFilterCodes( $filter, $view->param( 'filter/s_prodcode', [] ) );
163
		$filter = $cntl->addFilterTypes( $filter, $view->param( 'filter/s_typecode', [] ) );
164
165
		$view->items = $cntl->searchItems( $filter, $total );
166
		$view->total = $total;
167
168
		return $response;
169
	}
170
}
171