Completed
Push — master ( ba7de8...b94793 )
by Aimeos
02:24
created

Standard::getItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 8
nc 2
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\Attribute;
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/attribute/standard/template
60
		 * Relative path to the attribute 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 "standard" 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/attribute/standard/template';
79
		$default = 'attribute/standard.php';
80
81
		$body = $view->render( $view->config( $tplconf, $default ) );
82
83
		return $response->withHeader( 'Allow', 'GET' )
84
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
85
			->withBody( $view->response()->createStreamFromString( $body ) )
86
			->withStatus( $status );
87
	}
88
89
90
	/**
91
	 * Retrieves the item and adds the data to the view
92
	 *
93
	 * @param \Aimeos\MW\View\Iface $view View instance
94
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
95
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
96
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
97
	 */
98
	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...
99
	{
100
		$ref = $view->param( 'include', [] );
101
102
		if( is_string( $ref ) ) {
103
			$ref = explode( ',', $ref );
104
		}
105
106
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'attribute' );
107
108
		$view->items = $cntl->getItem( $view->param( 'id' ), $ref );
109
		$view->total = 1;
110
111
		return $response;
112
	}
113
114
115
	/**
116
	 * Retrieves the items and adds the data to the view
117
	 *
118
	 * @param \Aimeos\MW\View\Iface $view View instance
119
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
120
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
121
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
122
	 */
123
	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...
124
	{
125
		/** client/jsonapi/attribute/types
126
		 * List of attribute types that should be displayed in this order in the catalog filter
127
		 *
128
		 * The attribute section in the catalog filter component can display
129
		 * all attributes a visitor can use to reduce the listed products
130
		 * to those that contains one or more attributes. By default, all
131
		 * available attributes will be displayed and ordered by their
132
		 * attribute type.
133
		 *
134
		 * With this setting, you can limit the attribute types to only thoses
135
		 * whose names are part of the setting value. Furthermore, a particular
136
		 * order for the attribute types can be enforced that is different
137
		 * from the standard order.
138
		 *
139
		 * @param array List of attribute type codes
140
		 * @since 2017.03
141
		 * @category Developer
142
		 */
143
		$attrTypes = $this->getContext()->getConfig()->get( 'client/jsonapi/attribute/types', [] );
144
145
		$total = 0;
146
		$attrMap = [];
147
148
		$ref = $view->param( 'include', [] );
149
150
		if( is_string( $ref ) ) {
151
			$ref = explode( ',', $ref );
152
		}
153
154
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'attribute' );
155
156
		$filter = $cntl->createFilter();
157
		$filter = $this->initCriteriaConditions( $filter, $view->param() );
158
		$filter = $cntl->addFilterTypes( $filter, $attrTypes );
159
160
		$items = $cntl->searchItems( $filter, $ref, $total );
161
162
		foreach( $items as $id => $item ) {
163
			$attrMap[$item->getType()][$id] = $item;
164
		}
165
166
		if( !empty( $attrTypes ) )
167
		{
168
			$sorted = [];
169
170
			foreach( $attrTypes as $type )
171
			{
172
				if( isset( $attrMap[$type] ) ) {
173
					$sorted = array_merge( $sorted, $attrMap[$type] );
174
				}
175
			}
176
177
			$items = $sorted;
178
		}
179
180
		$view->items = $items;
181
		$view->total = $total;
182
183
		return $response;
184
	}
185
}
186