Standard   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 215
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 37 1
A getItems() 0 31 4
B saveEntry() 0 36 9
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2025
6
 * @package Admin
7
 * @subpackage JsonAdm
8
 */
9
10
11
namespace Aimeos\Admin\JsonAdm\Locale\Site;
12
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
17
/**
18
 * JSON API locale site  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/locale/site/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/locale/site/decorators/global
50
	 * @see admin/jsonadm/locale/site/decorators/local
51
	 */
52
53
	/** admin/jsonadm/locale/site/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/locale/site/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
	 * "locale/site " 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/locale/site/decorators/excludes
76
	 * @see admin/jsonadm/locale/site/decorators/local
77
	 */
78
79
	/** admin/jsonadm/locale/site/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\Catalog\Decorator\*") around the Jsonadm
89
	 * client.
90
	 *
91
	 *  admin/jsonadm/locale/site/decorators/local = array( 'decorator2' )
92
	 *
93
	 * This would add the decorator named "decorator2" defined by
94
	 * "\Aimeos\Admin\Jsonadm\Catalog\Decorator\Decorator2" only to the
95
	 * "locale/site " 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/locale/site/decorators/excludes
102
	 * @see admin/jsonadm/locale/site/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/locale/site/template-data
116
		 * Relative path to the data partial template file for the locale site  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.07
128
		 * @category Developer
129
		 */
130
		$this->view()->assign( array( 'partial-data' => 'admin/jsonadm/partials/locale/site/template-data' ) );
131
132
		/** admin/jsonadm/partials/locale/site/template-included
133
		 * Relative path to the included partial template file for the locale site  client
134
		 *
135
		 * Partials are templates which are reused in other templates and generate
136
		 * reoccuring blocks filled with data from the assigned values. The included
137
		 * partial creates the "included" part for the JSON API response.
138
		 *
139
		 * The partial template files are usually stored in the templates/partials/ folder
140
		 * of the core or the extensions. The configured path to the partial file must
141
		 * be relative to the templates/ folder, e.g. "partials/included".
142
		 *
143
		 * @param string Relative path to the template file
144
		 * @since 2021.04
145
		 * @category Developer
146
		 */
147
		$this->view()->assign( array( 'partial-included' => 'admin/jsonadm/partials/locale/site/template-included' ) );
148
149
		return parent::get( $request, $response );
150
	}
151
152
153
	/**
154
	 * Retrieves the item or items and adds the data to the view
155
	 *
156
	 * @param \Aimeos\Base\View\Iface $view View instance
157
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
158
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
159
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
160
	 */
161
	protected function getItems( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
162
	{
163
		$context = $this->context();
164
		$manager = \Aimeos\MShop::create( $context, 'locale/site' );
165
		$search = $this->initCriteria( $manager->filter(), $view->param() );
166
		$total = 1;
167
168
		if( !$view->access( ['super'] ) )
169
		{
170
			$customerManager = \Aimeos\MShop::create( $context, 'customer' );
171
			$siteid = $customerManager->get( $context->user() )->getSiteId();
0 ignored issues
show
Bug introduced by
It seems like $context->user() can also be of type null; however, parameter $id of Aimeos\MShop\Common\Manager\Iface::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

171
			$siteid = $customerManager->get( /** @scrutinizer ignore-type */ $context->user() )->getSiteId();
Loading history...
172
			$search->add( $search->is( 'locale.site.siteid', $view->access( ['admin'] ) ? '=~' : '==', $siteid ) );
173
		}
174
175
		if( $id = $view->param( 'id' ) )
176
		{
177
			$search->add( $search->is( 'locale.site.id', '==', $id ) )->slice( 0, 1 );
178
			$view->data = $manager->search( $search, [], $total )
179
				->first( new \Aimeos\MShop\Exception( $view->translate( 'admin', 'Item not available' ) ) );
180
		}
181
		else
182
		{
183
			$view->data = $manager->search( $search, [], $total );
184
		}
185
186
		$view->childItems = map();
187
		$view->listItems = map();
188
		$view->refItems = map();
189
		$view->total = $total;
190
191
		return $response;
192
	}
193
194
195
	/**
196
	 * Saves and returns the new or updated item
197
	 *
198
	 * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items
199
	 * @param \stdClass $entry Object including "id" and "attributes" elements
200
	 * @return \Aimeos\MShop\Common\Item\Iface New or updated item
201
	 */
202
	protected function saveEntry( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $entry ) : \Aimeos\MShop\Common\Item\Iface
203
	{
204
		if( !$this->view()->access( ['super', 'admin'] ) ) {
205
			throw new \Aimeos\Admin\JsonAdm\Exception( 'Not allowed' );
206
		}
207
208
		if( isset( $entry->id ) )
209
		{
210
			$item = $manager->get( $entry->id );
211
212
			if( isset( $entry->attributes ) && ( $attr = (array) $entry->attributes ) ) {
213
				$item = $item->fromArray( $attr, true );
214
			}
215
216
			$item = $manager->save( $item );
217
		}
218
		else
219
		{
220
			if( !$this->view()->access( ['super'] ) ) {
221
				throw new \Aimeos\Admin\JsonAdm\Exception( 'Not allowed' );
222
			}
223
224
			$item = $manager->create();
225
226
			if( isset( $entry->attributes ) && ( $attr = (array) $entry->attributes ) ) {
227
				$item = $item->fromArray( $attr, true );
228
			}
229
230
			$item = $manager->insert( $item );
231
		}
232
233
		if( isset( $entry->relationships ) ) {
234
			$this->saveRelationships( $manager, $item, $entry->relationships );
235
		}
236
237
		return $manager->get( $item->getId() );
238
	}
239
}
240