Completed
Push — test ( fadfb1...9f215d )
by Aimeos
10:29
created

JsonapiController::indexAction()   C

Complexity

Conditions 7
Paths 31

Size

Total Lines 39
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 39
rs 6.7272
cc 7
eloc 33
nc 31
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package flow
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use Neos\Flow\Annotations as Flow;
14
use Zend\Diactoros\Response;
15
16
17
/**
18
 * Aimeos controller for the frontend JSON REST API
19
 *
20
 * @package flow
21
 * @subpackage Controller
22
 */
23
class JsonapiController extends \Neos\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\View
39
	 * @Flow\Inject
40
	 */
41
	protected $viewContainer;
42
43
44
	/**
45
	 * JSON REST API endpoint
46
	 *
47
	 * @param string Resource location, e.g. "customer"
48
	 * @return string Response message content
49
	 * @Flow\Session(autoStart = TRUE)
50
	 */
51
	public function indexAction( $resource = '' )
52
	{
53
		$response = new Response();
54
		$request = $this->getPsrRequest();
55
		$client = $this->createClient( $resource );
56
error_log( 'class: ' . get_class( $client ) );
57
error_log( 'resource: ' . $resource );
58
error_log( 'method: ' . $this->request->getHttpRequest()->getMethod() );
59
60
try {
61
		switch( $this->request->getHttpRequest()->getMethod() )
62
		{
63
			case 'DELETE':
64
error_log( 'm: DELETE' );
65
				$response = $client->delete( $request, $response ); break;
66
			case 'GET':
67
error_log( 'm: GET' );
68
				$response = $client->get( $request, $response ); break;
69
			case 'PATCH':
70
error_log( 'm: PATCH' );
71
				$response = $client->patch( $request, $response ); break;
72
			case 'POST':
73
error_log( 'm: POST' );
74
				$response = $client->post( $request, $response ); break;
75
			case 'PUT':
76
error_log( 'm: PUT' );
77
				$response = $client->put( $request, $response ); break;
78
			default:
79
error_log( 'm: "' . $this->request->getHttpRequest()->getMethod() . '"' );
80
				$response = $client->options( $request, $response );
81
		}
82
error_log( 'status: ' . $response->getStatusCode() );
83
error_log( print_r( (string) $response->getBody(), true ) );
84
} catch( \Throwable $e ) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
85
	error_log( $e->getMessage() );
86
	error_log( $e->getTraceAsString() );
87
}
88
		return $this->setPsrResponse( $response );
89
	}
90
91
92
	/**
93
	 * Returns the resource controller
94
	 *
95
	 * @param string Resource location, e.g. "customer"
96
	 * @return \Aimeos\Client\JsonApi\Iface JsonApi client
97
	 */
98
	protected function createClient( $resource )
99
	{
100
		$related = '';
101
		$tmplPaths = $this->aimeos->get()->getCustomPaths( 'client/jsonapi/templates' );
102
103
		if( $this->request->hasArgument( 'related' ) ) {
104
			$related = $this->request->getArgument( 'related' );
105
		}
106
107
		$context = $this->context->get( $this->request );
108
		$langid = $context->getLocale()->getLanguageId();
109
110
		$view =$this->viewContainer->create( $context, $this->uriBuilder, $tmplPaths, $this->request, $langid );
111
		$context->setView( $view );
112
error_log( 'params: ' . print_r( $view->param(),true ) );
113
error_log( 'path: ' . $resource . '/' . $related );
114
115
		return \Aimeos\Client\JsonApi\Factory::createClient( $context, $tmplPaths, $resource . '/' . $related );
116
	}
117
118
119
	/**
120
	 * Returns a PSR-7 request object for the current request
121
	 *
122
	 * @return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
123
	 */
124
	protected function getPsrRequest()
125
	{
126
		$psrRequest = new \Zend\Diactoros\ServerRequest();
127
		$flowRequest = $this->request->getHttpRequest();
128
129
		try {
130
			$resource = $flowRequest->getContent( true );
131
		} catch( \Neos\Flow\Http\Exception $exception ) {
132
			$resource = fopen( 'php://temp', 'rw' );
133
			fwrite( $resource, $flowRequest->getContent() );
134
		}
135
136
		$psrRequest = $psrRequest->withBody( new \Zend\Diactoros\Stream( $resource ) );
137
138
		foreach( $flowRequest->getHeaders()->getAll() as $headerName => $headerValues ) {
139
			$psrRequest = $psrRequest->withHeader( $headerName, $headerValues );
140
		}
141
142
		return $psrRequest;
143
	}
144
145
146
	/**
147
	 * Set the response data from a PSR-7 response object
148
	 *
149
	 * @param \Psr\Http\Message\ResponseInterface $response PSR-7 response object
150
	 * @return string Response message content
151
	 */
152
	protected function setPsrResponse( \Psr\Http\Message\ResponseInterface $response )
153
	{
154
		$this->response->setStatus( $response->getStatusCode() );
155
156
		foreach( $response->getHeaders() as $key => $value ) {
157
			$this->response->setHeader( $key, $value );
158
		}
159
160
		return (string) $response->getBody();
161
	}
162
}
163