Completed
Push — master ( f48078...c7b4d8 )
by Aimeos
02:30
created

Extadm::indexAction()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 57
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 57
rs 7.6759
cc 7
eloc 39
nc 32
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Slim\Controller;
12
13
use Interop\Container\ContainerInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
18
/**
19
 * Aimeos controller for ExtJS admin interface
20
 *
21
 * @package Slim
22
 * @subpackage Controller
23
 */
24
class Extadm
25
{
26
	/**
27
	 * Returns the view for the ExtJS admin interface
28
	 *
29
	 * @param ContainerInterface $container Dependency injection container
30
	 * @param ServerRequestInterface $request Request object
31
	 * @param ResponseInterface $response Response object
32
	 * @param array $args Associative list of route parameters
33
	 * @return ResponseInterface $response Modified response object with generated output
34
	 */
35
	public static function indexAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
36
	{
37
		$site = ( isset( $args['site'] ) ? $args['site'] : 'default' );
38
		$lang = ( isset( $args['lang'] ) ? $args['lang'] : 'en' );
39
		$tab = ( isset( $args['tab'] ) ? $args['tab'] : 0 );
40
41
		$aimeos = $container->get( 'aimeos' );
42
		$cntlPaths = $aimeos->getCustomPaths( 'controller/extjs' );
43
44
		$context = $container->get( 'aimeos_context' )->get( false, $args );
45
		$context = self::setLocale( $context, $site, $lang );
46
47
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
48
		$cssFiles = array();
49
50
		foreach( $aimeos->getCustomPaths( 'admin/extjs' ) as $base => $paths )
51
		{
52
			foreach( $paths as $path )
53
			{
54
				$jsbAbsPath = $base . '/' . $path;
55
56
				if( !is_file( $jsbAbsPath ) ) {
57
					throw new \Exception( sprintf( 'JSB2 file "%1$s" not found', $jsbAbsPath ) );
58
				}
59
60
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $path ) );
61
				$cssFiles = array_merge( $cssFiles, $jsb2->getUrls( 'css' ) );
62
			}
63
		}
64
65
		$csrfname = $request->getAttribute( 'csrf_name' );
66
		$csrfvalue = $request->getAttribute( 'csrf_value' );
67
68
		$router = $container->get( 'router' );
69
		$jqadmUrl = $router->pathFor( 'aimeos_shop_jqadm_search', array( 'site' => $site, 'resource' => 'product' ) );
70
		$jsonUrl = $router->pathFor( 'aimeos_shop_extadm_json', array( 'site' => $site, $csrfname => $csrfvalue ) );
71
		$adminUrl = $router->pathFor( 'aimeos_shop_extadm', array( 'site' => '<site>', 'lang' => '<lang>', 'tab' => '<tab>' ) );
72
73
		$vars = array(
74
			'lang' => $lang,
75
			'cssFiles' => $cssFiles,
76
			'languages' => self::getJsonLanguages( $aimeos ),
77
			'config' => self::getJsonClientConfig( $context ),
78
			'site' => self::getJsonSiteItem( $context, $site ),
79
			'i18nContent' => self::getJsonClientI18n( $aimeos->getI18nPaths(), $lang ),
80
			'uploaddir' => $context->getConfig()->get( 'uploaddir', '/' ),
81
			'searchSchemas' => $controller->getJsonSearchSchemas(),
82
			'itemSchemas' => $controller->getJsonItemSchemas(),
83
			'smd' => $controller->getJsonSmd( $jsonUrl ),
84
			'urlTemplate' => $adminUrl,
85
			'jqadmurl' => $jqadmUrl,
86
			'activeTab' => $tab,
87
			'version' => \Aimeos\Slim\Bootstrap::getVersion(),
88
		);
89
90
		return $container->get( 'view' )->render( $response, 'Extadm/index.html.twig', $vars );
91
	}
92
93
94
	/**
95
	 * Single entry point for all JSON admin requests
96
	 *
97
	 * @param ContainerInterface $container Dependency injection container
98
	 * @param ServerRequestInterface $request Request object
99
	 * @param ResponseInterface $response Response object
100
	 * @param array $args Associative list of route parameters
101
	 * @return ResponseInterface $response Modified response object with generated output
102
	 */
103
	public static function doAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
104
	{
105
		$cntlPaths = $container->get( 'aimeos' )->getCustomPaths( 'controller/extjs' );
106
107
		$context = $container->get( 'aimeos_context' )->get( false, $args );
108
		$context = self::setLocale( $context );
109
110
		$params = $request->getQueryParams();
111
		if( ( $post = $request->getParsedBody() ) !== null ) {
112
			$params = array_merge( $params, (array) $post );
113
		}
114
115
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
116
		$output = $controller->process( $params, (string) $request->getBody() );
117
		$response->getBody()->write( $output );
118
119
		return $response;
120
	}
121
122
123
	/**
124
	 * Returns the JS file content
125
	 *
126
	 * @param ContainerInterface $container Dependency injection container
127
	 * @param ServerRequestInterface $request Request object
128
	 * @param ResponseInterface $response Response object
129
	 * @param array $args Associative list of route parameters
130
	 * @return ResponseInterface $response Modified response object with generated output
131
	 */
132
	public static function fileAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
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...
Unused Code introduced by
The parameter $args 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
		$contents = '';
135
		$jsFiles = array();
136
		$aimeos = $container->get( 'aimeos' );
137
138
		foreach( $aimeos->getCustomPaths( 'admin/extjs' ) as $base => $paths )
139
		{
140
			foreach( $paths as $path )
141
			{
142
				$jsbAbsPath = $base . '/' . $path;
143
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
144
				$jsFiles = array_merge( $jsFiles, $jsb2->getFiles( 'js' ) );
145
			}
146
		}
147
148
		foreach( $jsFiles as $file )
149
		{
150
			if( ( $content = file_get_contents( $file ) ) !== false ) {
151
				$contents .= $content;
152
			}
153
		}
154
155
		return $response->withHeader( 'Content-Type', 'application/javascript' )->getBody()->write( $contents );
156
	}
157
158
159
	/**
160
	 * Creates a list of all available translations
161
	 *
162
	 * @param \Aimeos\Bootstrap $aimeos Aimeos object
163
	 * @return array List of language IDs with labels
164
	 */
165
	protected static function getJsonLanguages( \Aimeos\Bootstrap $aimeos )
166
	{
167
		$result = array();
168
169
		foreach( $aimeos->getI18nList( 'admin' ) as $id ) {
170
			$result[] = array( 'id' => $id, 'label' => $id );
171
		}
172
173
		return json_encode( $result );
174
	}
175
176
177
	/**
178
	 * Returns the JSON encoded configuration for the ExtJS client.
179
	 *
180
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
181
	 * @return string JSON encoded configuration object
182
	 */
183
	protected static function getJsonClientConfig( \Aimeos\MShop\Context\Item\Iface $context )
184
	{
185
		$config = $context->getConfig()->get( 'admin/extjs', array() );
186
		return json_encode( array( 'admin' => array( 'extjs' => $config ) ), JSON_FORCE_OBJECT );
187
	}
188
189
190
	/**
191
	 * Returns the JSON encoded translations for the ExtJS client.
192
	 *
193
	 * @param array $i18nPaths List of file system paths which contain the translation files
194
	 * @param string $lang ISO language code like "en" or "en_GB"
195
	 * @return string JSON encoded translation object
196
	 */
197
	protected static function getJsonClientI18n( array $i18nPaths, $lang )
198
	{
199
		$i18n = new \Aimeos\MW\Translation\Gettext( $i18nPaths, $lang );
200
201
		$content = array(
202
			'admin' => $i18n->getAll( 'admin' ),
203
			'admin/ext' => $i18n->getAll( 'admin/ext' ),
204
		);
205
206
		return json_encode( $content, JSON_FORCE_OBJECT );
207
	}
208
209
210
	/**
211
	 * Returns the JSON encoded site item.
212
	 *
213
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
214
	 * @param string $site Unique site code
215
	 * @return string JSON encoded site item object
216
	 * @throws Exception If no site item was found for the code
217
	 */
218
	protected static function getJsonSiteItem( \Aimeos\MShop\Context\Item\Iface $context, $site )
219
	{
220
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' );
221
222
		$criteria = $manager->createSearch();
223
		$criteria->setConditions( $criteria->compare( '==', 'locale.site.code', $site ) );
224
		$items = $manager->searchItems( $criteria );
225
226
		if( ( $item = reset( $items ) ) === false ) {
227
			throw new \Exception( sprintf( 'No site found for code "%1$s"', $site ) );
228
		}
229
230
		return json_encode( $item->toArray() );
231
	}
232
233
234
	/**
235
	 * Sets the locale item in the given context
236
	 *
237
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
238
	 * @param string $sitecode Unique site code
239
	 * @param string $locale ISO language code, e.g. "en" or "en_GB"
240
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
241
	 */
242
	protected static function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $sitecode = 'default', $locale = null )
243
	{
244
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
245
246
		try {
247
			$localeItem = $localeManager->bootstrap( $sitecode, $locale, '', false );
248
		} catch( \Aimeos\MShop\Locale\Exception $e ) {
249
			$localeItem = $localeManager->createItem();
250
		}
251
252
		$context->setLocale( $localeItem );
253
254
		return $context;
255
	}
256
}