Completed
Push — master ( 33ff98...45c513 )
by Aimeos
02:35
created

JsonadmController::createClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
nc 2
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package flow
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use TYPO3\Flow\Annotations as Flow;
14
15
16
/**
17
 * Aimeos controller for the JSON REST API
18
 *
19
 * @package flow
20
 * @subpackage Controller
21
 */
22
class JsonadmController extends \TYPO3\Flow\Mvc\Controller\ActionController
23
{
24
	/**
25
	 * @var \Aimeos\Shop\Base\Aimeos
26
	 * @Flow\Inject
27
	 */
28
	protected $aimeos;
29
30
	/**
31
	 * @var \Aimeos\Shop\Base\Context
32
	 * @Flow\Inject
33
	 */
34
	protected $context;
35
36
	/**
37
	 * @var \Aimeos\Shop\Base\I18n
38
	 * @Flow\Inject
39
	 */
40
	protected $i18n;
41
42
	/**
43
	 * @var \Aimeos\Shop\Base\View
44
	 * @Flow\Inject
45
	 */
46
	protected $viewContainer;
47
48
49
	/**
50
	 * Deletes the resource object or a list of resource objects
51
	 *
52
	 * @param string Resource location, e.g. "product/stock/wareshouse"
53
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
54
	 * @param integer $id Unique resource ID
55
	 * @return string Generated output
56
	 */
57
	public function deleteAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
58
	{
59
		$request = $this->request->getHttpRequest();
60
		$header = $request->getHeaders()->getAll();
61
		$status = 500;
62
63
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
64
		$result = $client->delete( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
66
		$this->setResponse( $status, $header );
67
		return $result;
68
	}
69
70
71
	/**
72
	 * Returns the requested resource object or list of resource objects
73
	 *
74
	 * @param string Resource location, e.g. "product/stock/wareshouse"
75
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
	 * @param integer $id Unique resource ID
77
	 * @return string Generated output
78
	 */
79
	public function getAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
80
	{
81
		$request = $this->request->getHttpRequest();
82
		$header = $request->getHeaders()->getAll();
83
		$status = 500;
84
85
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
86
		$result = $client->get( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
88
		$this->setResponse( $status, $header );
89
		return $result;
90
	}
91
92
93
	/**
94
	 * Updates a resource object or a list of resource objects
95
	 *
96
	 * @param string Resource location, e.g. "product/stock/wareshouse"
97
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
98
	 * @param integer $id Unique resource ID
99
	 * @return string Generated output
100
	 */
101
	public function patchAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
102
	{
103
		$request = $this->request->getHttpRequest();
104
		$header = $request->getHeaders()->getAll();
105
		$status = 500;
106
107
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
108
		$result = $client->patch( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method patch() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
110
		$this->setResponse( $status, $header );
111
		return $result;
112
	}
113
114
115
	/**
116
	 * Creates a new resource object or a list of resource objects
117
	 *
118
	 * @param string Resource location, e.g. "product/stock/wareshouse"
119
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
120
	 * @param integer $id Unique ID of the resource
121
	 * @return string Generated output
122
	 */
123
	public function postAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
124
	{
125
		$request = $this->request->getHttpRequest();
126
		$header = $request->getHeaders()->getAll();
127
		$status = 500;
128
129
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
130
		$result = $client->post( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method post() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
132
		$this->setResponse( $status, $header );
133
		return $result;
134
	}
135
136
137
	/**
138
	 * Creates or updates a single resource object
139
	 *
140
	 * @param string Resource location, e.g. "product/stock/wareshouse"
141
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
142
	 * @param integer $id Unique resource ID
143
	 * @return string Generated output
144
	 */
145
	public function putAction( $resource, $site = 'default', $id = '' )
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
146
	{
147
		$request = $this->request->getHttpRequest();
148
		$header = $request->getHeaders()->getAll();
149
		$status = 500;
150
151
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
152
		$result = $client->put( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
154
		$this->setResponse( $status, $header );
155
		return $result;
156
	}
157
158
159
	/**
160
	 * Returns the available HTTP verbs and the resource URLs
161
	 *
162
	 * @param string Resource location, e.g. "product/stock/wareshouse"
163
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
164
	 * @return string Generated output
165
	 */
166
	public function optionsAction( $resource = '', $site = 'default' )
167
	{
168
		$request = $this->request->getHttpRequest();
169
		$header = $request->getHeaders()->getAll();
170
		$status = 500;
171
172
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
173
		$result = $client->options( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method options() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
174
175
		$this->setResponse( $status, $header );
176
		return $result;
177
	}
178
179
180
	/**
181
	 * Returns the resource controller
182
	 *
183
	 * @param string $sitecode Unique site code
184
	 * @param string Resource location, e.g. "product/stock/wareshouse"
185
	 * @param string $lang Language code
186
	 * @return \Aimeos\MShop\Context\Item\Iface Context item
187
	 */
188
	protected function createClient( $sitecode, $resource, $lang )
189
	{
190
		$lang = ( $lang ? $lang : 'en' );
191
		$templatePaths = $this->aimeos->get()->getCustomPaths( 'admin/jsonadm/templates' );
192
193
		$context = $this->context->get();
194
		$context = $this->setLocale( $context, $sitecode, $lang );
195
196
		$view = $this->viewContainer->create( $context->getConfig(), $this->uriBuilder, $templatePaths, $this->request, $lang );
197
		$context->setView( $view );
198
199
		return \Aimeos\Admin\JsonAdm\Factory::createClient( $context, $templatePaths, $resource );
200
	}
201
202
203
	/**
204
	 * Creates a new response object
205
	 *
206
	 * @param integer $status HTTP status
207
	 * @param array $header List of HTTP headers
208
	 * @return \TYPO3\Flow\Http\Response HTTP response object
209
	 */
210
	protected function setResponse( $status, array $header )
211
	{
212
		$this->response->setStatus( $status );
213
214
		foreach( $header as $key => $value ) {
215
			$this->response->setHeader( $key, $value );
216
		}
217
	}
218
219
220
	/**
221
	 * Sets the locale item in the given context
222
	 *
223
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
224
	 * @param string $sitecode Unique site code
225
	 * @param string $lang ISO language code, e.g. "en" or "en_GB"
226
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
227
	 */
228
	protected function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $sitecode, $lang )
229
	{
230
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
231
232
		try
233
		{
234
			$localeItem = $localeManager->bootstrap( $sitecode, '', '', false );
235
			$localeItem->setLanguageId( null );
236
			$localeItem->setCurrencyId( null );
237
		}
238
		catch( \Aimeos\MShop\Locale\Exception $e )
239
		{
240
			$localeItem = $localeManager->createItem();
241
		}
242
243
		$context->setLocale( $localeItem );
244
		$context->setI18n( $this->i18n->get( array( $lang ) ) );
245
246
		return $context;
247
	}
248
}
249