Completed
Push — master ( 57e5a6...06d3b4 )
by Aimeos
02:53
created

ExtadmController::indexAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 57
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 57
rs 9.0309
cc 4
eloc 38
nc 4
nop 1

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