Passed
Push — master ( b75e64...dbba3d )
by Aimeos
08:20
created

src/Aimeos/Shop/Controller/JsonadmController.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use Illuminate\Routing\Controller;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Request;
16
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Nyholm\Psr7\Factory\Psr17Factory;
19
20
21
/**
22
 * Aimeos controller for the JSON REST API
23
 *
24
 * @package laravel
25
 * @subpackage Controller
26
 */
27
class JsonadmController extends Controller
28
{
29
	use AuthorizesRequests;
30
31
32
	/**
33
	 * Deletes the resource object or a list of resource objects
34
	 *
35
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
36
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
37
	 */
38
	public function deleteAction( ServerRequestInterface $request )
39
	{
40
		if( config( 'shop.authorize', true ) ) {
41
			$this->authorize( 'admin', [JsonadmController::class, ['admin', 'api']] );
42
		}
43
44
		return $this->createAdmin()->delete( $request, ( new Psr17Factory )->createResponse() );
45
	}
46
47
48
	/**
49
	 * Returns the requested resource object or list of resource objects
50
	 *
51
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
52
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
53
	 */
54
	public function getAction( ServerRequestInterface $request )
55
	{
56
		if( config( 'shop.authorize', true ) ) {
57
			$this->authorize( 'admin', [JsonadmController::class, ['admin', 'api', 'editor']] );
58
		}
59
60
		return $this->createAdmin()->get( $request, ( new Psr17Factory )->createResponse() );
61
	}
62
63
64
	/**
65
	 * Updates a resource object or a list of resource objects
66
	 *
67
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
68
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
69
	 */
70
	public function patchAction( ServerRequestInterface $request )
71
	{
72
		if( config( 'shop.authorize', true ) ) {
73
			$this->authorize( 'admin', [JsonadmController::class, ['admin', 'api']] );
74
		}
75
76
		return $this->createAdmin()->patch( $request, ( new Psr17Factory )->createResponse() );
77
	}
78
79
80
	/**
81
	 * Creates a new resource object or a list of resource objects
82
	 *
83
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
84
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
85
	 */
86
	public function postAction( ServerRequestInterface $request )
87
	{
88
		if( config( 'shop.authorize', true ) ) {
89
			$this->authorize( 'admin', [JsonadmController::class, ['admin', 'api']] );
90
		}
91
92
		return $this->createAdmin()->post( $request, ( new Psr17Factory )->createResponse() );
93
	}
94
95
96
	/**
97
	 * Creates or updates a single resource object
98
	 *
99
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
100
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
101
	 */
102
	public function putAction( ServerRequestInterface $request )
103
	{
104
		if( config( 'shop.authorize', true ) ) {
105
			$this->authorize( 'admin', [JsonadmController::class, ['admin', 'api']] );
106
		}
107
108
		return $this->createAdmin()->put( $request, ( new Psr17Factory )->createResponse() );
109
	}
110
111
112
	/**
113
	 * Returns the available HTTP verbs and the resource URLs
114
	 *
115
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
116
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
117
	 */
118
	public function optionsAction( ServerRequestInterface $request )
119
	{
120
		if( config( 'shop.authorize', true ) ) {
121
			$this->authorize( 'admin', [JsonadmController::class, ['admin', 'api', 'editor']] );
122
		}
123
124
		return $this->createAdmin()->options( $request, ( new Psr17Factory )->createResponse() );
125
	}
126
127
128
	/**
129
	 * Returns the JsonAdm client
130
	 *
131
	 * @return \Aimeos\Admin\JsonAdm\Iface JsonAdm client
132
	 */
133
	protected function createAdmin() : \Aimeos\Admin\JsonAdm\Iface
134
	{
135
		$site = Route::input( 'site', Request::get( 'site', 'default' ) );
136
		$lang = Request::get( 'locale', config( 'app.locale', 'en' ) );
137
		$resource = Route::input( 'resource', '' );
138
139
		$aimeos = app( 'aimeos' )->get();
140
		$templatePaths = $aimeos->getCustomPaths( 'admin/jsonadm/templates' );
141
142
		$context = app( 'aimeos.context' )->get( false, 'backend' );
143
		$context->setI18n( app( 'aimeos.i18n' )->get( array( $lang, 'en' ) ) );
144
		$context->setLocale( app( 'aimeos.locale' )->getBackend( $context, $site ) );
0 ignored issues
show
It seems like $site can also be of type object; however, parameter $site of Aimeos\Shop\Base\Locale::getBackend() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
		$context->setLocale( app( 'aimeos.locale' )->getBackend( $context, /** @scrutinizer ignore-type */ $site ) );
Loading history...
145
		$context->setView( app( 'aimeos.view' )->create( $context, $templatePaths, $lang ) );
146
147
		return \Aimeos\Admin\JsonAdm::create( $context, $aimeos, $resource );
0 ignored issues
show
It seems like $resource can also be of type object; however, parameter $path of Aimeos\Admin\JsonAdm::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
		return \Aimeos\Admin\JsonAdm::create( $context, $aimeos, /** @scrutinizer ignore-type */ $resource );
Loading history...
148
	}
149
}
150