Completed
Push — master ( f77883...03da81 )
by Aimeos
10:59
created

JsonadmController::getPsrRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 0
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
use Zend\Diactoros\Response;
15
16
17
/**
18
 * Aimeos controller for the JSON REST API
19
 *
20
 * @package flow
21
 * @subpackage Controller
22
 */
23
class JsonadmController extends \TYPO3\Flow\Mvc\Controller\ActionController
24
{
25
	/**
26
	 * @var \Aimeos\Shop\Base\Aimeos
27
	 * @Flow\Inject
28
	 */
29
	protected $aimeos;
30
31
	/**
32
	 * @var \Aimeos\Shop\Base\Context
33
	 * @Flow\Inject
34
	 */
35
	protected $context;
36
37
	/**
38
	 * @var \Aimeos\Shop\Base\I18n
39
	 * @Flow\Inject
40
	 */
41
	protected $i18n;
42
43
	/**
44
	 * @var \Aimeos\Shop\Base\Locale
45
	 * @Flow\Inject
46
	 */
47
	protected $locale;
48
49
	/**
50
	 * @var \Aimeos\Shop\Base\View
51
	 * @Flow\Inject
52
	 */
53
	protected $viewContainer;
54
55
56
	/**
57
	 * Deletes the resource object or a list of resource objects
58
	 *
59
	 * @param string Resource location, e.g. "product/stock/wareshouse"
60
	 * @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...
61
	 * @param integer $id Unique resource ID
62
	 */
63
	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...
64
	{
65
		$request = $this->request->getHttpRequest();
66
67
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
68
		$psrResponse = $client->delete( $this->getPsrRequest(), new Response() );
0 ignored issues
show
Compatibility introduced by
$this->getPsrRequest() of type object<Psr\Http\Message\RequestInterface> is not a sub-type of object<Psr\Http\Message\ServerRequestInterface>. It seems like you assume a child interface of the interface Psr\Http\Message\RequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
69
70
		$this->setPsrResponse( $psrResponse );
71
	}
72
73
74
	/**
75
	 * Returns the requested resource object or list of resource objects
76
	 *
77
	 * @param string Resource location, e.g. "product/stock/wareshouse"
78
	 * @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...
79
	 * @param integer $id Unique resource ID
80
	 */
81
	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...
82
	{
83
		$request = $this->request->getHttpRequest();
84
85
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
86
		$psrResponse = $client->get( $this->getPsrRequest(), new Response() );
0 ignored issues
show
Compatibility introduced by
$this->getPsrRequest() of type object<Psr\Http\Message\RequestInterface> is not a sub-type of object<Psr\Http\Message\ServerRequestInterface>. It seems like you assume a child interface of the interface Psr\Http\Message\RequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
87
88
		$this->setPsrResponse( $psrResponse );
89
	}
90
91
92
	/**
93
	 * Updates a resource object or a list of resource objects
94
	 *
95
	 * @param string Resource location, e.g. "product/stock/wareshouse"
96
	 * @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...
97
	 * @param integer $id Unique resource ID
98
	 */
99
	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...
100
	{
101
		$request = $this->request->getHttpRequest();
102
103
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
104
		$psrResponse = $client->patch( $this->getPsrRequest(), new Response() );
0 ignored issues
show
Compatibility introduced by
$this->getPsrRequest() of type object<Psr\Http\Message\RequestInterface> is not a sub-type of object<Psr\Http\Message\ServerRequestInterface>. It seems like you assume a child interface of the interface Psr\Http\Message\RequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
105
106
		$this->setPsrResponse( $psrResponse );
107
	}
108
109
110
	/**
111
	 * Creates a new resource object or a list of resource objects
112
	 *
113
	 * @param string Resource location, e.g. "product/stock/wareshouse"
114
	 * @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...
115
	 * @param integer $id Unique ID of the resource
116
	 */
117
	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...
118
	{
119
		$request = $this->request->getHttpRequest();
120
121
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
122
		$psrResponse = $client->post( $this->getPsrRequest(), new Response() );
0 ignored issues
show
Compatibility introduced by
$this->getPsrRequest() of type object<Psr\Http\Message\RequestInterface> is not a sub-type of object<Psr\Http\Message\ServerRequestInterface>. It seems like you assume a child interface of the interface Psr\Http\Message\RequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
123
124
		$this->setPsrResponse( $psrResponse );
125
	}
126
127
128
	/**
129
	 * Creates or updates a single resource object
130
	 *
131
	 * @param string Resource location, e.g. "product/stock/wareshouse"
132
	 * @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...
133
	 * @param integer $id Unique resource ID
134
	 */
135
	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...
136
	{
137
		$request = $this->request->getHttpRequest();
138
139
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
140
		$psrResponse = $client->put( $this->getPsrRequest(), new Response() );
0 ignored issues
show
Compatibility introduced by
$this->getPsrRequest() of type object<Psr\Http\Message\RequestInterface> is not a sub-type of object<Psr\Http\Message\ServerRequestInterface>. It seems like you assume a child interface of the interface Psr\Http\Message\RequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
141
142
		$this->setPsrResponse( $psrResponse );
143
	}
144
145
146
	/**
147
	 * Returns the available HTTP verbs and the resource URLs
148
	 *
149
	 * @param string Resource location, e.g. "product/stock/wareshouse"
150
	 * @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...
151
	 */
152
	public function optionsAction( $resource = '', $site = 'default' )
153
	{
154
		$request = $this->request->getHttpRequest();
155
156
		$client = $this->createClient( $site, $resource, $request->getArgument( 'lang' ) );
157
		$psrResponse = $client->options( $this->getPsrRequest(), new Response() );
0 ignored issues
show
Compatibility introduced by
$this->getPsrRequest() of type object<Psr\Http\Message\RequestInterface> is not a sub-type of object<Psr\Http\Message\ServerRequestInterface>. It seems like you assume a child interface of the interface Psr\Http\Message\RequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
158
159
		$this->setPsrResponse( $psrResponse );
160
	}
161
162
163
	/**
164
	 * Returns the resource controller
165
	 *
166
	 * @param string $sitecode Unique site code
167
	 * @param string Resource location, e.g. "product/stock/wareshouse"
168
	 * @param string $lang Language code
169
	 * @return \Aimeos\Admin\JsonAdm\Iface JsonAdm client
170
	 */
171
	protected function createClient( $sitecode, $resource, $lang )
172
	{
173
		$lang = ( $lang ? $lang : 'en' );
174
		$templatePaths = $this->aimeos->get()->getCustomPaths( 'admin/jsonadm/templates' );
175
176
		$context = $this->context->get( null, 'backend' );
177
		$context->setI18n( $this->i18n->get( array( $lang, 'en' ) ) );
178
		$context->setLocale( $this->locale->getBackend( $context, $sitecode ) );
179
		$context->setView( $this->viewContainer->create( $context, $this->uriBuilder, $templatePaths, $this->request, $lang ) );
180
181
		return \Aimeos\Admin\JsonAdm\Factory::createClient( $context, $templatePaths, $resource );
182
	}
183
184
185
	/**
186
	 * Returns a PSR-7 request object for the current request
187
	 *
188
	 * @return \Psr\Http\Message\RequestInterface PSR-7 request object
189
	 */
190
	protected function getPsrRequest()
191
	{
192
		$psrRequest = new \Zend\Diactoros\ServerRequest();
193
		$flowRequest = $this->request->getHttpRequest();
194
195
		try {
196
			$resource = $flowRequest->getContent( true );
197
		} catch( \TYPO3\Flow\Http\Exception $exception ) {
198
			fwrite( tmpfile(), $flowRequest->getContent() );
199
		}
200
201
		$psrRequest = $psrRequest->withBody( new \Zend\Diactoros\Stream( $resource ) );
202
203
		foreach( $flowRequest->getHeaders()->getAll() as $headerName => $headerValues ) {
204
			$psrRequest = $psrRequest->withHeader( $headerName, $headerValues );
205
		}
206
207
		return $psrRequest;
208
	}
209
210
211
	/**
212
	 * Set the response data from a PSR-7 response object
213
	 *
214
	 * @param \Psr\Http\Message\ResponseInterface $response PSR-7 response object
215
	 */
216
	protected function setPsrResponse( \Psr\Http\Message\ResponseInterface $response )
217
	{
218
		$this->response->setStatus( $status );
0 ignored issues
show
Bug introduced by
The variable $status does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
219
220
		foreach( $header as $key => $value ) {
0 ignored issues
show
Bug introduced by
The variable $header does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
221
			$this->response->setHeader( $key, $value );
222
		}
223
224
		$this->response->setConent( (string) $response->getBody() );
0 ignored issues
show
Bug introduced by
The method setConent() does not exist on TYPO3\Flow\Http\Response. Did you maybe mean setContent()?

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...
225
	}
226
}
227