Completed
Push — master ( da2dea...5ebe79 )
by Aimeos
22:25
created

ExtadmController::indexAction()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 54
rs 8.7449
cc 5
eloc 35
nc 8
nop 0

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 laravel
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use Illuminate\Support\Facades\View;
14
use Illuminate\Support\Facades\Input;
15
use Illuminate\Support\Facades\Route;
16
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
17
18
19
/**
20
 * Controller providing the ExtJS administration interface
21
 *
22
 * @package laravel
23
 * @subpackage Controller
24
 */
25
class ExtadmController extends AdminController
26
{
27
	use AuthorizesRequests;
28
29
30
	/**
31
	 * Returns the initial HTML view for the admin interface.
32
	 *
33
	 * @return Response Response object containing the generated output
34
	 */
35
	public function indexAction()
36
	{
37
		if( config( 'shop.authorize', true ) ) {
38
			$this->authorize( 'admin' );
39
		}
40
41
		$site = Route::input( 'site', 'default' );
42
		$lang = Input::get( 'lang', config( 'app.locale', 'en' ) );
43
44
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
45
		$cntlPaths = $aimeos->getCustomPaths( 'controller/extjs' );
46
47
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false );
48
		$context = $this->setLocale( $context, $site, $lang );
49
50
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
51
		$cssFiles = array();
52
53
		foreach( $aimeos->getCustomPaths( 'admin/extjs' ) as $base => $paths )
54
		{
55
			foreach( $paths as $path )
56
			{
57
				$jsbAbsPath = $base . '/' . $path;
58
59
				if( !is_file( $jsbAbsPath ) ) {
60
					throw new \Exception( sprintf( 'JSB2 file "%1$s" not found', $jsbAbsPath ) );
61
				}
62
63
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $path ) );
64
				$cssFiles = array_merge( $cssFiles, $jsb2->getUrls( 'css' ) );
65
			}
66
		}
67
68
		$adminUrl = route( 'aimeos_shop_extadm', array( 'site' => '%site%', 'lang' => '%lang%', 'tab' => '%tab%' ) );
69
		$jsonUrl = route( 'aimeos_shop_extadm_json', array( 'site' => $site, '_token' => csrf_token() ) );
70
71
		$vars = array(
72
			'lang' => $lang,
73
			'cssFiles' => $cssFiles,
74
			'languages' => $this->getJsonLanguages( $context),
75
			'config' => $this->getJsonClientConfig( $context ),
76
			'site' => $this->getJsonSiteItem( $context, Input::get( 'site', 'default' ) ),
77
			'i18nContent' => $this->getJsonClientI18n( $aimeos->getI18nPaths(), $lang ),
78
			'searchSchemas' => $controller->getJsonSearchSchemas(),
79
			'itemSchemas' => $controller->getJsonItemSchemas(),
80
			'smd' => $controller->getJsonSmd( $jsonUrl ),
81
			'urlTemplate' => urldecode( $adminUrl ),
82
			'uploaddir' => config( 'shop::uploaddir' ),
83
			'activeTab' => Input::get( 'tab', 0 ),
84
			'version' => $this->getVersion(),
85
		);
86
87
		return \View::make('shop::admin.extadm-index', $vars);
88
	}
89
90
91
	/**
92
	 * Single entry point for all JSON admin requests.
93
	 *
94
	 * @return Response Response object containing the generated output
95
	 */
96
	public function doAction()
97
	{
98
		if( config( 'shop.authorize', true ) ) {
99
			$this->authorize( 'admin' );
100
		}
101
102
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
103
		$cntlPaths = $aimeos->getCustomPaths( 'controller/extjs' );
104
105
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false );
106
		$context = $this->setLocale( $context );
107
108
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
109
110
		$response = $controller->process( Input::instance()->request->all(), 'php://input' );
0 ignored issues
show
Bug introduced by
The method instance() does not exist on Illuminate\Support\Facades\Input. Did you maybe mean clearResolvedInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
111
		return View::make('shop::admin.extadm-do', array( 'output' => $response ));
112
	}
113
114
115
	/**
116
	 * Returns the JS file content
117
	 *
118
	 * @return Response Response object
119
	 */
120
	public function fileAction()
121
	{
122
		if( config( 'shop.authorize', true ) ) {
123
			$this->authorize( 'admin' );
124
		}
125
126
		$contents = '';
127
		$jsFiles = array();
128
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
129
130
		foreach( $aimeos->getCustomPaths( 'admin/extjs' ) as $base => $paths )
131
		{
132
			foreach( $paths as $path )
133
			{
134
				$jsbAbsPath = $base . '/' . $path;
135
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
136
				$jsFiles = array_merge( $jsFiles, $jsb2->getUrls( 'js', '' ) );
137
			}
138
		}
139
140
		foreach( $jsFiles as $file )
141
		{
142
			if( ( $content = file_get_contents( $file ) ) !== false ) {
143
				$contents .= $content;
144
			}
145
		}
146
147
		return response( $contents )->header( 'Content-Type', 'application/javascript' );
0 ignored issues
show
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
148
	}
149
150
151
	/**
152
	 * Creates a list of all available translations.
153
	 *
154
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
155
	 * @return array List of language IDs with labels
156
	 */
157
	protected function getJsonLanguages( \Aimeos\MShop\Context\Item\Iface $context )
158
	{
159
		$paths = app( '\Aimeos\Shop\Base\Aimeos' )->get()->getI18nPaths();
160
		$langs = array();
161
162
		if( !isset( $paths['admin'] ) ) {
163
			return json_encode( array() );
164
		}
165
166
		foreach( $paths['admin'] as $path )
167
		{
168
			$iter = new \DirectoryIterator( $path );
169
170
			foreach( $iter as $file )
171
			{
172
				$name = $file->getFilename();
173
174
				if( preg_match('/^[a-z]{2,3}(_[A-Z]{2})?$/', $name ) ) {
175
					$langs[$name] = null;
176
				}
177
			}
178
		}
179
180
		return json_encode( $this->getLanguages( $context, array_keys( $langs ) ) );
181
	}
182
183
184
	/**
185
	 * Returns the JSON encoded configuration for the ExtJS client.
186
	 *
187
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
188
	 * @return string JSON encoded configuration object
189
	 */
190
	protected function getJsonClientConfig( \Aimeos\MShop\Context\Item\Iface $context )
191
	{
192
		$config = $context->getConfig()->get( 'admin/extjs', array() );
193
		return json_encode( array( 'admin' => array( 'extjs' => $config ) ), JSON_FORCE_OBJECT );
194
	}
195
196
197
	/**
198
	 * Returns the JSON encoded translations for the ExtJS client.
199
	 *
200
	 * @param array $i18nPaths List of file system paths which contain the translation files
201
	 * @param string $lang ISO language code like "en" or "en_GB"
202
	 * @return string JSON encoded translation object
203
	 */
204
	protected function getJsonClientI18n( array $i18nPaths, $lang )
205
	{
206
		$i18n = new \Aimeos\MW\Translation\Zend2( $i18nPaths, 'gettext', $lang, array( 'disableNotices' => true ) );
207
208
		$content = array(
209
			'admin' => $i18n->getAll( 'admin' ),
210
			'admin/ext' => $i18n->getAll( 'admin/ext' ),
211
		);
212
213
		return json_encode( $content, JSON_FORCE_OBJECT );
214
	}
215
216
217
	/**
218
	 * Returns the JSON encoded site item.
219
	 *
220
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
221
	 * @param string $site Unique site code
222
	 * @return string JSON encoded site item object
223
	 * @throws Exception If no site item was found for the code
224
	 */
225
	protected function getJsonSiteItem( \Aimeos\MShop\Context\Item\Iface $context, $site )
226
	{
227
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' );
228
229
		$criteria = $manager->createSearch();
230
		$criteria->setConditions( $criteria->compare( '==', 'locale.site.code', $site ) );
231
		$items = $manager->searchItems( $criteria );
232
233
		if( ( $item = reset( $items ) ) === false ) {
234
			throw new \Exception( sprintf( 'No site found for code "%1$s"', $site ) );
235
		}
236
237
		return json_encode( $item->toArray() );
238
	}
239
240
241
	/**
242
	 * Returns a list of arrays with "id" and "label"
243
	 *
244
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
245
	 * @param array $langIds List of language IDs
246
	 * @return array List of associative lists with "id" and "label" as keys
247
	 */
248
	protected function getLanguages( \Aimeos\MShop\Context\Item\Iface $context, array $langIds )
249
	{
250
		$languageManager = \Aimeos\MShop\Factory::createManager( $context, 'locale/language' );
251
		$result = array();
252
253
		$search = $languageManager->createSearch();
254
		$search->setConditions( $search->compare('==', 'locale.language.id', $langIds ) );
255
		$search->setSortations( array( $search->sort( '-', 'locale.language.status' ), $search->sort( '+', 'locale.language.label' ) ) );
256
		$langItems = $languageManager->searchItems( $search );
257
258
		foreach( $langItems as $id => $item ) {
259
			$result[] = array( 'id' => $id, 'label' => $item->getLabel() );
260
		}
261
262
		return $result;
263
	}
264
265
266
	/**
267
	 * Sets the locale item in the given context
268
	 *
269
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
270
	 * @param string $sitecode Unique site code
271
	 * @param string $locale ISO language code, e.g. "en" or "en_GB"
272
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
273
	 */
274
	protected function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $sitecode = 'default', $locale = null )
275
	{
276
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
277
278
		try {
279
			$localeItem = $localeManager->bootstrap( $sitecode, $locale, '', false );
280
		} catch( \Aimeos\MShop\Locale\Exception $e ) {
281
			$localeItem = $localeManager->createItem();
282
		}
283
284
		$context->setLocale( $localeItem );
285
286
		return $context;
287
	}
288
}