Completed
Push — master ( d848d5...321d4d )
by Aimeos
02:48
created

JsonadmController::createClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
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\Http\Request;
14
use Illuminate\Routing\Controller;
15
use Illuminate\Support\Facades\Input;
16
use Illuminate\Support\Facades\Route;
17
use Illuminate\Support\Facades\Response;
18
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
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 \Illuminate\Http\Request $request Request object
36
	 * @return \Illuminate\Http\Response Response object containing the generated output
37
	 */
38
	public function deleteAction( Request $request )
39
	{
40
		if( config( 'shop.authorize', true ) ) {
41
			$this->authorize( 'admin' );
42
		}
43
44
		$status = 500;
45
		$header = $request->headers->all();
46
47
		$client = $this->createClient();
48
		$result = $client->delete( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
50
		return $this->createResponse( $result, $status, $header );
51
	}
52
53
54
	/**
55
	 * Returns the requested resource object or list of resource objects
56
	 *
57
	 * @param \Illuminate\Http\Request $request Request object
58
	 * @return \Illuminate\Http\Response Response object containing the generated output
59
	 */
60
	public function getAction( Request $request )
61
	{
62
		if( config( 'shop.authorize', true ) ) {
63
			$this->authorize( 'admin' );
64
		}
65
66
		$status = 500;
67
		$header = $request->headers->all();
68
69
		$client = $this->createClient();
70
		$result = $client->get( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
72
		return $this->createResponse( $result, $status, $header );
73
	}
74
75
76
	/**
77
	 * Updates a resource object or a list of resource objects
78
	 *
79
	 * @param \Illuminate\Http\Request $request Request object
80
	 * @return \Illuminate\Http\Response Response object containing the generated output
81
	 */
82
	public function patchAction( Request $request )
83
	{
84
		if( config( 'shop.authorize', true ) ) {
85
			$this->authorize( 'admin' );
86
		}
87
88
		$status = 500;
89
		$header = $request->headers->all();
90
91
		$client = $this->createClient();
92
		$result = $client->patch( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method patch() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
94
		return $this->createResponse( $result, $status, $header );
95
	}
96
97
98
	/**
99
	 * Creates a new resource object or a list of resource objects
100
	 *
101
	 * @param \Illuminate\Http\Request $request Request object
102
	 * @return \Illuminate\Http\Response Response object containing the generated output
103
	 */
104
	public function postAction( Request $request )
105
	{
106
		if( config( 'shop.authorize', true ) ) {
107
			$this->authorize( 'admin' );
108
		}
109
110
		$status = 500;
111
		$header = $request->headers->all();
112
113
		$client = $this->createClient();
114
		$result = $client->post( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method post() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
116
		return $this->createResponse( $result, $status, $header );
117
	}
118
119
120
	/**
121
	 * Creates or updates a single resource object
122
	 *
123
	 * @param \Illuminate\Http\Request $request Request object
124
	 * @return \Illuminate\Http\Response Response object containing the generated output
125
	 */
126
	public function putAction( Request $request )
127
	{
128
		if( config( 'shop.authorize', true ) ) {
129
			$this->authorize( 'admin' );
130
		}
131
132
		$status = 500;
133
		$header = $request->headers->all();
134
135
		$client = $this->createClient();
136
		$result = $client->put( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
138
		return $this->createResponse( $result, $status, $header );
139
	}
140
141
142
	/**
143
	 * Returns the available HTTP verbs and the resource URLs
144
	 *
145
	 * @param \Illuminate\Http\Request $request Request object
146
	 * @return \Illuminate\Http\Response Response object containing the generated output
147
	 */
148
	public function optionsAction( Request $request )
149
	{
150
		if( config( 'shop.authorize', true ) ) {
151
			$this->authorize( 'admin' );
152
		}
153
154
		$status = 500;
155
		$header = $request->headers->all();
156
157
		$client = $this->createClient();
158
		$result = $client->options( $request->getContent(), $header, $status );
0 ignored issues
show
Bug introduced by
The method options() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
159
160
		return $this->createResponse( $result, $status, $header );
161
	}
162
163
164
	/**
165
	 * Returns the resource controller
166
	 *
167
	 * @return \Aimeos\MShop\Context\Item\Iface Context item
168
	 */
169
	protected function createClient()
170
	{
171
		$site = Route::input( 'site', Input::get( 'site', 'default' ) );
172
		$lang = Input::get( 'lang', config( 'app.locale', 'en' ) );
173
		$resource = Route::input( 'resource' );
174
175
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
176
		$templatePaths = $aimeos->getCustomPaths( 'admin/jsonadm/templates' );
177
178
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false );
179
		$context = $this->setLocale( $context, $site, $lang );
180
181
		$view = app( '\Aimeos\Shop\Base\View' )->create( $context->getConfig(), $templatePaths, $lang );
182
		$context->setView( $view );
183
184
		return \Aimeos\Admin\JsonAdm\Factory::createClient( $context, $templatePaths, $resource );
185
	}
186
187
188
	/**
189
	 * Creates a new response object
190
	 *
191
	 * @param string $content Body of the HTTP response
192
	 * @param integer $status HTTP status
193
	 * @param array $header List of HTTP headers
194
	 * @return \Illuminate\Http\Response HTTP response object
195
	 */
196
	protected function createResponse( $content, $status, array $header )
197
	{
198
		$response = Response::make( $content, $status );
199
200
		foreach( $header as $key => $value ) {
201
			$response->header( $key, $value );
202
		}
203
204
		return $response;
205
	}
206
207
208
	/**
209
	 * Sets the locale item in the given context
210
	 *
211
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
212
	 * @param string $sitecode Unique site code
213
	 * @param string $lang ISO language code, e.g. "en" or "en_GB"
214
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
215
	 */
216 View Code Duplication
	protected function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $sitecode, $lang )
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
	{
218
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
219
220
		try
221
		{
222
			$localeItem = $localeManager->bootstrap( $sitecode, '', '', false );
223
			$localeItem->setLanguageId( null );
224
			$localeItem->setCurrencyId( null );
225
		}
226
		catch( \Aimeos\MShop\Locale\Exception $e )
227
		{
228
			$localeItem = $localeManager->createItem();
229
		}
230
231
		$context->setLocale( $localeItem );
232
		$context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $lang ) ) );
233
234
		return $context;
235
	}
236
}
237