Completed
Push — master ( af6c70...4f7083 )
by Aimeos
02:25
created

Jqadm::fileAction()   C

Complexity

Conditions 8
Paths 54

Size

Total Lines 34
Code Lines 19

Duplication

Lines 9
Ratio 26.47 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 34
rs 5.3846
cc 8
eloc 19
nc 54
nop 4
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 the JQuery admin interface
20
 *
21
 * @package Slim
22
 * @subpackage Controller
23
 */
24
class Jqadm
25
{
26
	/**
27
	 * Returns the JS file content
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 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...
36
	{
37
		$contents = '';
38
		$files = array();
39
		$aimeos = $container->get( 'aimeos' );
40
		$type = ( isset( $args['type'] ) ? $args['type'] : 'js' );
41
42 View Code Duplication
		foreach( $aimeos->getCustomPaths( 'admin/jqadm' ) as $base => $paths )
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
		{
44
			foreach( $paths as $path )
45
			{
46
				$jsbAbsPath = $base . '/' . $path;
47
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
48
				$files = array_merge( $files, $jsb2->getFiles( $type ) );
49
			}
50
		}
51
52
		foreach( $files as $file )
53
		{
54
			if( ( $content = file_get_contents( $file ) ) !== false ) {
55
				$contents .= $content;
56
			}
57
		}
58
59
		$response->getBody()->write( $contents );
60
61
		if( $type === 'js' ) {
62
			$response = $response->withHeader( 'Content-Type', 'application/javascript' );
63
		} elseif( $type === 'css' ) {
64
			$response = $response->withHeader( 'Content-Type', 'text/css' );
65
		}
66
67
		return $response;
68
	}
69
70
71
	/**
72
	 * Returns the HTML code for a copy of a resource object
73
	 *
74
	 * @param ContainerInterface $container Dependency injection container
75
	 * @param ServerRequestInterface $request Request object
76
	 * @param ResponseInterface $response Response object
77
	 * @param array $args Associative list of route parameters
78
	 * @return ResponseInterface $response Modified response object with generated output
79
	 */
80
	public static function copyAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
81
	{
82
		$cntl = self::createClient( $container, $request, $response, $args );
83
		return self::getHtml( $container, $response, $cntl->copy() );
84
	}
85
86
87
	/**
88
	 * Returns the HTML code for a new resource object
89
	 *
90
	 * @param ContainerInterface $container Dependency injection container
91
	 * @param ServerRequestInterface $request Request object
92
	 * @param ResponseInterface $response Response object
93
	 * @param array $args Associative list of route parameters
94
	 * @return ResponseInterface $response Modified response object with generated output
95
	 */
96
	public static function createAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
97
	{
98
		$cntl = self::createClient( $container, $request, $response, $args );
99
		return self::getHtml( $container, $response, $cntl->create() );
100
	}
101
102
103
	/**
104
	 * Deletes the resource object or a list of resource objects
105
	 *
106
	 * @param ContainerInterface $container Dependency injection container
107
	 * @param ServerRequestInterface $request Request object
108
	 * @param ResponseInterface $response Response object
109
	 * @param array $args Associative list of route parameters
110
	 * @return ResponseInterface $response Modified response object with generated output
111
	 */
112
	public static function deleteAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
113
	{
114
		$cntl = self::createClient( $container, $request, $response, $args );
115
		return self::getHtml( $container, $response, $cntl->delete() . $cntl->search() );
116
	}
117
118
119
	/**
120
	 * Returns the HTML code for the requested resource object
121
	 *
122
	 * @param ContainerInterface $container Dependency injection container
123
	 * @param ServerRequestInterface $request Request object
124
	 * @param ResponseInterface $response Response object
125
	 * @param array $args Associative list of route parameters
126
	 * @return ResponseInterface $response Modified response object with generated output
127
	 */
128
	public static function getAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
129
	{
130
		$cntl = self::createClient( $container, $request, $response, $args );
131
		return self::getHtml( $container, $response, $cntl->get() );
132
	}
133
134
135
	/**
136
	 * Saves a new resource object
137
	 *
138
	 * @param ContainerInterface $container Dependency injection container
139
	 * @param ServerRequestInterface $request Request object
140
	 * @param ResponseInterface $response Response object
141
	 * @param array $args Associative list of route parameters
142
	 * @return ResponseInterface $response Modified response object with generated output
143
	 */
144
	public static function saveAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
145
	{
146
		$cntl = self::createClient( $container, $request, $response, $args );
147
		return self::getHtml( $container, $response, ( $cntl->save() ? : $cntl->search() ) );
148
	}
149
150
151
	/**
152
	 * Returns the HTML code for a list of resource objects
153
	 *
154
	 * @param ContainerInterface $container Dependency injection container
155
	 * @param ServerRequestInterface $request Request object
156
	 * @param ResponseInterface $response Response object
157
	 * @param array $args Associative list of route parameters
158
	 * @return ResponseInterface $response Modified response object with generated output
159
	 */
160
	public static function searchAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
161
	{
162
		$cntl = self::createClient( $container, $request, $response, $args );
163
		return self::getHtml( $container, $response, $cntl->search() );
164
	}
165
166
167
	/**
168
	 * Returns the resource controller
169
	 *
170
	 * @param ContainerInterface $container Dependency injection container
171
	 * @param ServerRequestInterface $request Request object
172
	 * @param ResponseInterface $response Response object
173
	 * @param array $args Associative list of route parameters
174
	 * @return \Aimeos\Admin\JQAdm\Iface JQAdm client
175
	 */
176
	protected static function createClient( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
177
	{
178
		$resource = ( isset( $args['resource'] ) ? $args['resource'] : null );
179
		$site = ( isset( $args['site'] ) ? $args['site'] : 'default' );
180
		$lang = ( isset( $args['lang'] ) ? $args['lang'] : 'en' );
181
182
		$templatePaths = $container->get( 'aimeos' )->getCustomPaths( 'admin/jqadm/templates' );
183
184
		$context = $container->get( 'aimeos_context' )->get( false, $args );
185
		$context = self::setLocale( $container->get( 'aimeos_i18n' ), $context, $site, $lang );
186
187
		$view = $container->get( 'aimeos_view' )->create( $request, $response, $args, $templatePaths, $lang );
188
		$context->setView( $view );
189
190
		return \Aimeos\Admin\JQAdm\Factory::createClient( $context, $templatePaths, $resource );
191
	}
192
193
194
	/**
195
	 * Returns the generated HTML code
196
	 *
197
	 * @param ContainerInterface $container Dependency injection container
198
	 * @param ResponseInterface $response Response object
199
	 * @param string $content Content from admin client
200
	 * @return \Illuminate\Contracts\View\View View for rendering the output
201
	 */
202
	protected static function getHtml( ContainerInterface $container, ResponseInterface $response, $content )
203
	{
204
		$version = \Aimeos\Slim\Bootstrap::getVersion();
205
		$content = str_replace( array( '{type}', '{version}' ), array( 'Slim', $version ), $content );
206
207
		return $container->get( 'view' )->render( $response, 'Jqadm/index.html.twig', array( 'content' => $content ) );
208
	}
209
210
211
	/**
212
	 * Sets the locale item in the given context
213
	 *
214
	 * @param \Aimeos\Slim\Base\I18n $i18n Aimeos translation object builder
215
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
216
	 * @param string $site Unique site code
217
	 * @param string $lang ISO language code, e.g. "en" or "en_GB"
218
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
219
	 */
220 View Code Duplication
	protected static function setLocale( \Aimeos\Slim\Base\I18n $i18n, \Aimeos\MShop\Context\Item\Iface $context, $site, $lang )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
	{
222
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
223
224
		try
225
		{
226
			$localeItem = $localeManager->bootstrap( $site, '', '', false );
227
			$localeItem->setLanguageId( null );
228
			$localeItem->setCurrencyId( null );
229
		}
230
		catch( \Aimeos\MShop\Locale\Exception $e )
231
		{
232
			$localeItem = $localeManager->createItem();
233
		}
234
235
		$context->setLocale( $localeItem );
236
		$context->setI18n( $i18n->get( array( $lang, 'en' ) ) );
237
238
		return $context;
239
	}
240
}
241