Standard::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 23
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Admin
7
 * @subpackage JsonAdm
8
 */
9
10
11
namespace Aimeos\Admin\JsonAdm\Order;
12
13
use \Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Bug introduced by
The type \Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use \Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type \Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
17
/**
18
 * JSON API order client
19
 *
20
 * @package Admin
21
 * @subpackage JsonAdm
22
 */
23
class Standard
24
	extends \Aimeos\Admin\JsonAdm\Standard
25
	implements \Aimeos\Admin\JsonAdm\Common\Iface
26
{
27
	/** admin/jsonadm/order/decorators/excludes
28
	 * Excludes decorators added by the "common" option from the JSON API clients
29
	 *
30
	 * Decorators extend the functionality of a class by adding new aspects
31
	 * (e.g. log what is currently done), executing the methods of the underlying
32
	 * class only in certain conditions (e.g. only for logged in users) or
33
	 * modify what is returned to the caller.
34
	 *
35
	 * This option allows you to remove a decorator added via
36
	 * "admin/jsonadm/common/decorators/default" before they are wrapped
37
	 * around the Jsonadm client.
38
	 *
39
	 *  admin/jsonadm/decorators/excludes = array( 'decorator1' )
40
	 *
41
	 * This would remove the decorator named "decorator1" from the list of
42
	 * common decorators ("\Aimeos\Admin\JsonAdm\Common\Decorator\*") added via
43
	 * "admin/jsonadm/common/decorators/default" for the JSON API client.
44
	 *
45
	 * @param array List of decorator names
46
	 * @since 2016.01
47
	 * @category Developer
48
	 * @see admin/jsonadm/common/decorators/default
49
	 * @see admin/jsonadm/order/decorators/global
50
	 * @see admin/jsonadm/order/decorators/local
51
	 */
52
53
	/** admin/jsonadm/order/decorators/global
54
	 * Adds a list of globally available decorators only to the Jsonadm client
55
	 *
56
	 * Decorators extend the functionality of a class by adding new aspects
57
	 * (e.g. log what is currently done), executing the methods of the underlying
58
	 * class only in certain conditions (e.g. only for logged in users) or
59
	 * modify what is returned to the caller.
60
	 *
61
	 * This option allows you to wrap global decorators
62
	 * ("\Aimeos\Admin\Jsonadm\Common\Decorator\*") around the Jsonadm
63
	 * client.
64
	 *
65
	 *  admin/jsonadm/order/decorators/global = array( 'decorator1' )
66
	 *
67
	 * This would add the decorator named "decorator1" defined by
68
	 * "\Aimeos\Admin\Jsonadm\Common\Decorator\Decorator1" only to the
69
	 * "order" Jsonadm client.
70
	 *
71
	 * @param array List of decorator names
72
	 * @since 2016.01
73
	 * @category Developer
74
	 * @see admin/jsonadm/common/decorators/default
75
	 * @see admin/jsonadm/order/decorators/excludes
76
	 * @see admin/jsonadm/order/decorators/local
77
	 */
78
79
	/** admin/jsonadm/order/decorators/local
80
	 * Adds a list of local decorators only to the Jsonadm client
81
	 *
82
	 * Decorators extend the functionality of a class by adding new aspects
83
	 * (e.g. log what is currently done), executing the methods of the underlying
84
	 * class only in certain conditions (e.g. only for logged in users) or
85
	 * modify what is returned to the caller.
86
	 *
87
	 * This option allows you to wrap local decorators
88
	 * ("\Aimeos\Admin\Jsonadm\Order\Decorator\*") around the Jsonadm
89
	 * client.
90
	 *
91
	 *  admin/jsonadm/order/decorators/local = array( 'decorator2' )
92
	 *
93
	 * This would add the decorator named "decorator2" defined by
94
	 * "\Aimeos\Admin\Jsonadm\Order\Decorator\Decorator2" only to the
95
	 * "order" Jsonadm client.
96
	 *
97
	 * @param array List of decorator names
98
	 * @since 2016.01
99
	 * @category Developer
100
	 * @see admin/jsonadm/common/decorators/default
101
	 * @see admin/jsonadm/order/decorators/excludes
102
	 * @see admin/jsonadm/order/decorators/global
103
	 */
104
105
106
	/**
107
	 * Returns the requested resource or the resource list
108
	 *
109
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
110
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
111
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
112
	 */
113
	public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
114
	{
115
		/** admin/jsonadm/partials/order/template-data
116
		 * Relative path to the data partial template file for the order client
117
		 *
118
		 * Partials are templates which are reused in other templates and generate
119
		 * reoccuring blocks filled with data from the assigned values. The data
120
		 * partial creates the "data" part for the JSON API response.
121
		 *
122
		 * The partial template files are usually stored in the templates/partials/ folder
123
		 * of the core or the extensions. The configured path to the partial file must
124
		 * be relative to the templates/ folder, e.g. "partials/data".
125
		 *
126
		 * @param string Relative path to the template file
127
		 * @since 2016.01
128
		 * @category Developer
129
		 */
130
		$this->view()->assign( [
131
			'partial-data' => 'admin/jsonadm/partials/order/template-data',
132
			'partial-included' => 'admin/jsonadm/partials/order/template-included'
133
		] );
134
135
		return parent::get( $request, $response );
136
	}
137
138
139
	/**
140
	 * Retrieves the item or items and adds the data to the view
141
	 *
142
	 * @param \Aimeos\Base\View\Iface $view View instance
143
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
144
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
145
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
146
	 */
147
	protected function getItems( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
148
	{
149
		$context = $this->context();
150
		$manager = \Aimeos\MShop::create( $context, $this->getPath() );
151
		$search = $manager->filter( false, true );
152
153
		if( ( $key = $view->param( 'aggregate' ) ) !== null )
154
		{
155
			$search = $this->initCriteria( $search, $view->param() );
156
			$view->data = $manager->aggregate( $search, explode( ',', $key ), $view->param( 'value' ), $view->param( 'type' ) );
157
			return $response;
158
		}
159
160
		$total = 1;
161
		$include = ( ( $include = $view->param( 'include' ) ) !== null ? explode( ',', $include ) : [] );
162
163
		if( ( $id = $view->param( 'id' ) ) == null ) {
164
			$search = $this->initCriteria( $search, $view->param() );
165
		} else {
166
			$search->setConditions( $search->compare( '==', 'order.id', $id ) );
167
		}
168
169
		$view->data = $manager->search( $search, [], $total );
170
		$view->childItems = $this->getChildItems( $view->data, $include );
171
		$view->listItems = $this->getListItems( $view->data, $include );
172
		$view->refItems = $this->getRefItems( $view->listItems );
173
		$view->total = $total;
174
175
		return $response;
176
	}
177
178
179
	/**
180
	 * Returns the items with parent/child relationships
181
	 *
182
	 * @param \Aimeos\Map $items List of items implementing \Aimeos\MShop\Common\Item\Iface
183
	 * @param array $include List of resource types that should be fetched
184
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface
185
	 */
186
	protected function getChildItems( \Aimeos\Map $items, array $include ) : \Aimeos\Map
187
	{
188
		$list = map();
189
190
		$ids = map( $items )->getId()->toArray();
191
		$manager = \Aimeos\MShop::create( $this->context(), 'order' );
192
193
		$search = $manager->filter( false, true );
194
		$search->setConditions( $search->and( [
195
			$search->compare( '==', 'order.id', $ids ),
196
			$search->getConditions()
197
		] ) );
198
199
		$orders = $manager->search( $search, $include );
200
201
		$list = $list->merge( $orders )
202
			->merge( $orders->getAddresses()->flat() )->merge( $orders->getCoupons()->flat() )
203
			->merge( $orders->getProducts()->flat() )->merge( $orders->getServices()->flat() );
204
205
		foreach( $orders->getProducts()->flat() as $row ) {
206
			$list->merge( $row->getAttributeItems() );
207
		}
208
209
		foreach( $orders->getServices()->flat() as $row ) {
210
			$list->merge( $row->getAttributeItems() );
211
		}
212
213
		if( in_array( 'order/status', $include ) )
214
		{
215
			$ids = $items->keys()->toArray();
216
			$manager = \Aimeos\MShop::create( $this->context(), 'order/status' );
217
218
			$search = $manager->filter();
219
			$search->setConditions( $search->compare( '==', 'order.status.parentid', $ids ) );
220
221
			$list = $list->merge( $manager->search( $search ) );
222
		}
223
224
		return $list;
225
	}
226
}
227