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

Jqadm::createAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
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 HTML code for a copy of a resource object
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 copyAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
36
	{
37
		$cntl = self::createClient( $container, $request, $response, $args );
38
		return self::getHtml( $container, $response, $cntl->copy() );
39
	}
40
41
42
	/**
43
	 * Returns the HTML code for a new resource object
44
	 *
45
	 * @param ContainerInterface $container Dependency injection container
46
	 * @param ServerRequestInterface $request Request object
47
	 * @param ResponseInterface $response Response object
48
	 * @param array $args Associative list of route parameters
49
	 * @return ResponseInterface $response Modified response object with generated output
50
	 */
51
	public static function createAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
52
	{
53
		$cntl = self::createClient( $container, $request, $response, $args );
54
		return self::getHtml( $container, $response, $cntl->create() );
55
	}
56
57
58
	/**
59
	 * Deletes the resource object or a list of resource objects
60
	 *
61
	 * @param ContainerInterface $container Dependency injection container
62
	 * @param ServerRequestInterface $request Request object
63
	 * @param ResponseInterface $response Response object
64
	 * @param array $args Associative list of route parameters
65
	 * @return ResponseInterface $response Modified response object with generated output
66
	 */
67
	public static function deleteAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
68
	{
69
		$cntl = self::createClient( $container, $request, $response, $args );
70
		return self::getHtml( $container, $response, $cntl->delete() . $cntl->search() );
71
	}
72
73
74
	/**
75
	 * Returns the HTML code for the requested resource object
76
	 *
77
	 * @param ContainerInterface $container Dependency injection container
78
	 * @param ServerRequestInterface $request Request object
79
	 * @param ResponseInterface $response Response object
80
	 * @param array $args Associative list of route parameters
81
	 * @return ResponseInterface $response Modified response object with generated output
82
	 */
83
	public static function getAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
84
	{
85
		$cntl = self::createClient( $container, $request, $response, $args );
86
		return self::getHtml( $container, $response, $cntl->get() );
87
	}
88
89
90
	/**
91
	 * Saves a new resource object
92
	 *
93
	 * @param ContainerInterface $container Dependency injection container
94
	 * @param ServerRequestInterface $request Request object
95
	 * @param ResponseInterface $response Response object
96
	 * @param array $args Associative list of route parameters
97
	 * @return ResponseInterface $response Modified response object with generated output
98
	 */
99
	public static function saveAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
100
	{
101
		$cntl = self::createClient( $container, $request, $response, $args );
102
		return self::getHtml( $container, $response, ( $cntl->save() ? : $cntl->search() ) );
103
	}
104
105
106
	/**
107
	 * Returns the HTML code for a list of resource objects
108
	 *
109
	 * @param ContainerInterface $container Dependency injection container
110
	 * @param ServerRequestInterface $request Request object
111
	 * @param ResponseInterface $response Response object
112
	 * @param array $args Associative list of route parameters
113
	 * @return ResponseInterface $response Modified response object with generated output
114
	 */
115
	public static function searchAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
116
	{
117
		$cntl = self::createClient( $container, $request, $response, $args );
118
		return self::getHtml( $container, $response, $cntl->search() );
119
	}
120
121
122
	/**
123
	 * Returns the resource controller
124
	 *
125
	 * @param ContainerInterface $container Dependency injection container
126
	 * @param ServerRequestInterface $request Request object
127
	 * @param ResponseInterface $response Response object
128
	 * @param array $args Associative list of route parameters
129
	 * @return \Aimeos\Admin\JQAdm\Iface JQAdm client
130
	 */
131 View Code Duplication
	protected static function createClient( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
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...
132
	{
133
		$resource = ( isset( $args['resource'] ) ? $args['resource'] : null );
134
		$site = ( isset( $args['site'] ) ? $args['site'] : 'default' );
135
		$lang = ( isset( $args['lang'] ) ? $args['lang'] : 'en' );
136
137
		$templatePaths = $container->get( 'aimeos' )->getCustomPaths( 'admin/jqadm/templates' );
138
139
		$context = $container->get( 'aimeos_context' )->get( false, $args );
140
		$context = self::setLocale( $container->get( 'aimeos_i18n' ), $context, $site, $lang );
141
142
		$view = $container->get( 'aimeos_view' )->create( $request, $response, $args, $templatePaths, $lang );
143
		$context->setView( $view );
144
145
		return \Aimeos\Admin\JQAdm\Factory::createClient( $context, $templatePaths, $resource );
146
	}
147
148
149
	/**
150
	 * Returns the generated HTML code
151
	 *
152
	 * @param ContainerInterface $container Dependency injection container
153
	 * @param ResponseInterface $response Response object
154
	 * @param string $content Content from admin client
155
	 * @return \Illuminate\Contracts\View\View View for rendering the output
156
	 */
157
	protected static function getHtml( ContainerInterface $container, ResponseInterface $response, $content )
158
	{
159
		$version = \Aimeos\Slim\Bootstrap::getVersion();
160
		$content = str_replace( array( '{type}', '{version}' ), array( 'Slim', $version ), $content );
161
162
		return $container->get( 'view' )->render( $response, 'Jqadm/index.html.twig', array( 'content' => $content ) );
163
	}
164
165
166
	/**
167
	 * Sets the locale item in the given context
168
	 *
169
	 * @param \Aimeos\Slim\Base\I18n $i18n Aimeos translation object builder
170
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
171
	 * @param string $site Unique site code
172
	 * @param string $lang ISO language code, e.g. "en" or "en_GB"
173
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
174
	 */
175 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...
176
	{
177
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
178
179
		try
180
		{
181
			$localeItem = $localeManager->bootstrap( $site, '', '', false );
182
			$localeItem->setLanguageId( null );
183
			$localeItem->setCurrencyId( null );
184
		}
185
		catch( \Aimeos\MShop\Locale\Exception $e )
186
		{
187
			$localeItem = $localeManager->createItem();
188
		}
189
190
		$context->setLocale( $localeItem );
191
		$context->setI18n( $i18n->get( array( $lang, 'en' ) ) );
192
193
		return $context;
194
	}
195
}
196