JsonapiController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteAction() 0 3 1
A patchAction() 0 3 1
A createClient() 0 14 1
A getAction() 0 3 1
A postAction() 0 3 1
A optionsAction() 0 6 1
A putAction() 0 3 1
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2017-2023
6
 */
7
8
9
namespace Aimeos\Shop\Controller;
10
11
use Illuminate\Routing\Controller;
12
use Illuminate\Support\Facades\Route;
13
use Illuminate\Support\Facades\Request;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Nyholm\Psr7\Factory\Psr17Factory;
16
17
18
/**
19
 * Aimeos controller for the JSON REST API
20
 */
21
class JsonapiController extends Controller
22
{
23
	/**
24
	 * Deletes the resource object or a list of resource objects
25
	 *
26
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
27
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
28
	 */
29
	public function deleteAction( ServerRequestInterface $request )
30
	{
31
		return $this->createClient()->delete( $request, ( new Psr17Factory )->createResponse() );
32
	}
33
34
35
	/**
36
	 * Returns the requested resource object or list of resource objects
37
	 *
38
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
39
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
40
	 */
41
	public function getAction( ServerRequestInterface $request )
42
	{
43
		return $this->createClient()->get( $request, ( new Psr17Factory )->createResponse() );
44
	}
45
46
47
	/**
48
	 * Updates a resource object or a list of resource objects
49
	 *
50
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
51
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
52
	 */
53
	public function patchAction( ServerRequestInterface $request )
54
	{
55
		return $this->createClient()->patch( $request, ( new Psr17Factory )->createResponse() );
56
	}
57
58
59
	/**
60
	 * Creates a new resource object or a list of resource objects
61
	 *
62
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
63
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
64
	 */
65
	public function postAction( ServerRequestInterface $request )
66
	{
67
		return $this->createClient()->post( $request, ( new Psr17Factory )->createResponse() );
68
	}
69
70
71
	/**
72
	 * Creates or updates a single resource object
73
	 *
74
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
75
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
76
	 */
77
	public function putAction( ServerRequestInterface $request )
78
	{
79
		return $this->createClient()->put( $request, ( new Psr17Factory )->createResponse() );
80
	}
81
82
83
	/**
84
	 * Returns the available HTTP verbs and the resource URLs
85
	 *
86
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
87
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
88
	 */
89
	public function optionsAction( ServerRequestInterface $request )
90
	{
91
		return $this->createClient()->options( $request, ( new Psr17Factory )->createResponse() )
92
			->withHeader( 'access-control-allow-headers', 'authorization,content-type' )
93
			->withHeader( 'access-control-allow-methods', 'DELETE, GET, OPTIONS, PATCH, POST, PUT' )
94
			->withHeader( 'access-control-allow-origin', $request->getHeaderLine( 'origin' ) );
95
	}
96
97
98
	/**
99
	 * Returns the JsonAdm client
100
	 *
101
	 * @return \Aimeos\Client\JsonApi\Iface JsonApi client
102
	 */
103
	protected function createClient() : \Aimeos\Client\JsonApi\Iface
104
	{
105
		$resource = Route::input( 'resource' );
106
		$related = Route::input( 'related', Request::get( 'related' ) );
107
108
		$aimeos = app( 'aimeos' )->get();
109
		$context = app( 'aimeos.context' )->get();
110
		$tmplPaths = $aimeos->getTemplatePaths( 'client/jsonapi/templates', $context->locale()->getSiteItem()->getTheme() );
111
112
		$langid = $context->locale()->getLanguageId();
113
114
		$context->setView( app( 'aimeos.view' )->create( $context, $tmplPaths, $langid ) );
115
116
		return \Aimeos\Client\JsonApi::create( $context, $resource . '/' . $related );
117
	}
118
}
119