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

Jsonadm::setLocale()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 20
loc 20
rs 9.4285
cc 2
eloc 11
nc 4
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 JSON REST API
20
 *
21
 * @package Slim
22
 * @subpackage Controller
23
 */
24
class Jsonadm
25
{
26
	/**
27
	 * Deletes the resource object or a list of resource objects
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 View Code Duplication
	public static function deleteAction( 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...
36
	{
37
		$status = 500;
38
		$header = $request->getHeaders();
39
40
		$cntl = self::createController( $container, $request, $response, $args );
41
		$result = $cntl->delete( (string) $request->getBody(), $header, $status );
42
43
		return self::withResponse( $response, $result, $status, $header );
44
	}
45
46
47
	/**
48
	 * Returns the requested resource object or list of resource objects
49
	 *
50
	 * @param ContainerInterface $container Dependency injection container
51
	 * @param ServerRequestInterface $request Request object
52
	 * @param ResponseInterface $response Response object
53
	 * @param array $args Associative list of route parameters
54
	 * @return ResponseInterface $response Modified response object with generated output
55
	 */
56 View Code Duplication
	public static function getAction( 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...
57
	{
58
		$status = 500;
59
		$header = $request->getHeaders();
60
61
		$cntl = self::createController( $container, $request, $response, $args );
62
		$result = $cntl->get( (string) $request->getBody(), $header, $status );
63
64
		return self::withResponse( $response, $result, $status, $header );
65
	}
66
67
68
	/**
69
	 * Updates a resource object or a list of resource objects
70
	 *
71
	 * @param ContainerInterface $container Dependency injection container
72
	 * @param ServerRequestInterface $request Request object
73
	 * @param ResponseInterface $response Response object
74
	 * @param array $args Associative list of route parameters
75
	 * @return ResponseInterface $response Modified response object with generated output
76
	 */
77 View Code Duplication
	public static function patchAction( 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...
78
	{
79
		$status = 500;
80
		$header = $request->getHeaders();
81
82
		$cntl = self::createController( $container, $request, $response, $args );
83
		$result = $cntl->patch( (string) $request->getBody(), $header, $status );
84
85
		return self::withResponse( $response, $result, $status, $header );
86
	}
87
88
89
	/**
90
	 * Creates a new resource object or a list of resource objects
91
	 *
92
	 * @param ContainerInterface $container Dependency injection container
93
	 * @param ServerRequestInterface $request Request object
94
	 * @param ResponseInterface $response Response object
95
	 * @param array $args Associative list of route parameters
96
	 * @return ResponseInterface $response Modified response object with generated output
97
	 */
98 View Code Duplication
	public static function postAction( 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...
99
	{
100
		$status = 500;
101
		$header = $request->getHeaders();
102
103
		$cntl = self::createController( $container, $request, $response, $args );
104
		$result = $cntl->post( (string) $request->getBody(), $header, $status );
105
106
		return self::withResponse( $response, $result, $status, $header );
107
	}
108
109
110
	/**
111
	 * Creates or updates a single resource object
112
	 *
113
	 * @param ContainerInterface $container Dependency injection container
114
	 * @param ServerRequestInterface $request Request object
115
	 * @param ResponseInterface $response Response object
116
	 * @param array $args Associative list of route parameters
117
	 * @return ResponseInterface $response Modified response object with generated output
118
	 */
119 View Code Duplication
	public static function putAction( 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...
120
	{
121
		$status = 500;
122
		$header = $request->getHeaders();
123
124
		$cntl = self::createController( $container, $request, $response, $args );
125
		$result = $cntl->put( (string) $request->getBody(), $header, $status );
126
127
		return self::withResponse( $response, $result, $status, $header );
128
	}
129
130
131
	/**
132
	 * Returns the available HTTP verbs and the resource URLs
133
	 *
134
	 * @param ContainerInterface $container Dependency injection container
135
	 * @param ServerRequestInterface $request Request object
136
	 * @param ResponseInterface $response Response object
137
	 * @param array $args Associative list of route parameters
138
	 * @return ResponseInterface $response Modified response object with generated output
139
	 */
140 View Code Duplication
	public static function optionsAction( 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...
141
	{
142
		$status = 500;
143
		$header = $request->getHeaders();
144
145
		$cntl = self::createController( $container, $request, $response, $args );
146
		$result = $cntl->options( (string) $request->getBody(), $header, $status );
147
148
		return self::withResponse( $response, $result, $status, $header );
149
	}
150
151
152
	/**
153
	 * Returns the resource controller
154
	 *
155
	 * @param ContainerInterface $container Dependency injection container
156
	 * @param ServerRequestInterface $request Request object
157
	 * @param ResponseInterface $response Response object
158
	 * @param array $args Associative list of route parameters
159
	 * @return \Aimeos\Controller\JsonAdm\Iface JSON admin controller
160
	 */
161 View Code Duplication
	protected static function createController( 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...
162
	{
163
		$resource = ( isset( $args['resource'] ) ? $args['resource'] : null );
164
		$site = ( isset( $args['site'] ) ? $args['site'] : 'default' );
165
		$lang = ( isset( $args['lang'] ) ? $args['lang'] : 'en' );
166
167
		$templatePaths = $container->get( 'aimeos' )->getCustomPaths( 'controller/jsonadm/templates' );
168
169
		$context = $container->get( 'aimeos_context' )->get( false, $args );
170
		$context = self::setLocale( $container->get( 'aimeos_i18n' ), $context, $site, $lang );
171
172
		$view = $container->get( 'aimeos_view' )->create( $request, $response, $args, $templatePaths, $lang );
173
		$context->setView( $view );
174
175
		return \Aimeos\Controller\JsonAdm\Factory::createController( $context, $templatePaths, $resource );
176
	}
177
178
179
	/**
180
	 * Populates the response object
181
	 *
182
	 * @param ResponseInterface $response Response object
183
	 * @param string $content Body of the HTTP response
184
	 * @param integer $status HTTP status
185
	 * @param array $header List of HTTP headers
186
	 * @return ResponseInterface $response Populated response object
187
	 */
188
	protected static function withResponse( ResponseInterface $response, $content, $status, array $header )
189
	{
190
		$response->getBody()->write( $content );
191
		$response = $response->withStatus( $status );
192
193
		foreach( $header as $key => $value ) {
194
			$response->withHeader( $key, $value );
195
		}
196
197
		return $response;
198
	}
199
200
201
	/**
202
	 * Sets the locale item in the given context
203
	 *
204
	 * @param \Aimeos\Slim\Base\I18n $i18n Aimeos translation object builder
205
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
206
	 * @param string $sitecode Unique site code
207
	 * @param string $lang ISO language code, e.g. "en" or "en_GB"
208
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
209
	 */
210 View Code Duplication
	protected static function setLocale( \Aimeos\Slim\Base\I18n $i18n, \Aimeos\MShop\Context\Item\Iface $context, $sitecode, $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...
211
	{
212
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
213
214
		try
215
		{
216
			$localeItem = $localeManager->bootstrap( $sitecode, '', '', false );
217
			$localeItem->setLanguageId( null );
218
			$localeItem->setCurrencyId( null );
219
		}
220
		catch( \Aimeos\MShop\Locale\Exception $e )
221
		{
222
			$localeItem = $localeManager->createItem();
223
		}
224
225
		$context->setLocale( $localeItem );
226
		$context->setI18n( $i18n->get( array( $lang ) ) );
227
228
		return $context;
229
	}
230
}
231