Completed
Push — master ( f435a7...9e3b53 )
by Aimeos
11:21
created

JsonapiController::indexAction()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 6
eloc 15
nc 6
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
	 * Deletes the resource object or a list of resource objects
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
		$client = $this->createClient( $resource );
54
55
		switch( $this->request->getHttpRequest()->getMethod() )
56
		{
57
			case 'DELETE':
58
				return $this->setPsrResponse( $client->delete( $this->getPsrRequest(), new Response() ) );
59
			case 'GET':
60
				return $this->setPsrResponse( $client->get( $this->getPsrRequest(), new Response() ) );
61
			case 'PATCH':
62
				return $this->setPsrResponse( $client->patch( $this->getPsrRequest(), new Response() ) );
63
			case 'POST':
64
				return $this->setPsrResponse( $client->post( $this->getPsrRequest(), new Response() ) );
65
			case 'PUT':
66
				return $this->setPsrResponse( $client->put( $this->getPsrRequest(), new Response() ) );
67
			default:
68
				return $this->setPsrResponse( $client->options( $this->getPsrRequest(), new Response() ) );
69
		}
70
	}
71
72
73
	/**
74
	 * Returns the resource controller
75
	 *
76
	 * @param string Resource location, e.g. "customer"
77
	 * @return \Aimeos\Client\JsonApi\Iface JsonApi client
78
	 */
79
	protected function createClient( $resource )
80
	{
81
		$related = '';
82
		$tmplPaths = $this->aimeos->get()->getCustomPaths( 'client/jsonapi/templates' );
83
84
		if( $this->request->hasArgument( 'related' ) ) {
85
			$related = $this->request->getArgument( 'related' );
86
		}
87
88
		$context = $this->context->get( $this->request );
89
		$langid = $context->getLocale()->getLanguageId();
90
91
		$view =$this->viewContainer->create( $context, $this->uriBuilder, $tmplPaths, $this->request, $langid );
92
		$context->setView( $view );
93
94
		return \Aimeos\Client\JsonApi\Factory::createClient( $context, $tmplPaths, $resource . '/' . $related );
95
	}
96
97
98
	/**
99
	 * Returns a PSR-7 request object for the current request
100
	 *
101
	 * @return \Psr\Http\Message\ServerRequestInterface PSR-7 request object
102
	 */
103
	protected function getPsrRequest()
104
	{
105
		$psrRequest = new \Zend\Diactoros\ServerRequest();
106
		$flowRequest = $this->request->getHttpRequest();
107
108
		try {
109
			$resource = $flowRequest->getContent( true );
110
		} catch( \Neos\Flow\Http\Exception $exception ) {
111
			$resource = fopen( 'php://temp', 'rw' );
112
			fwrite( $resource, $flowRequest->getContent() );
113
		}
114
115
		$psrRequest = $psrRequest->withBody( new \Zend\Diactoros\Stream( $resource ) );
116
117
		foreach( $flowRequest->getHeaders()->getAll() as $headerName => $headerValues ) {
118
			$psrRequest = $psrRequest->withHeader( $headerName, $headerValues );
119
		}
120
121
		return $psrRequest;
122
	}
123
124
125
	/**
126
	 * Set the response data from a PSR-7 response object
127
	 *
128
	 * @param \Psr\Http\Message\ResponseInterface $response PSR-7 response object
129
	 * @return string Response message content
130
	 */
131
	protected function setPsrResponse( \Psr\Http\Message\ResponseInterface $response )
132
	{
133
		$this->response->setStatus( $response->getStatusCode() );
134
135
		foreach( $response->getHeaders() as $key => $value ) {
136
			$this->response->setHeader( $key, $value );
137
		}
138
139
		return (string) $response->getBody();
140
	}
141
}
142