Completed
Push — master ( 7193bc...d7424f )
by Aimeos
19:48
created

JsonadmController::createAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package symfony
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\ShopBundle\Controller;
12
13
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Zend\Diactoros\Response;
16
17
18
/**
19
 * Aimeos controller for the JSON REST API
20
 *
21
 * @package symfony
22
 * @subpackage Controller
23
 */
24
class JsonadmController extends Controller
25
{
26
	/**
27
	 * Deletes the resource object or a list of resource objects
28
	 *
29
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
30
	 * @param string Resource location, e.g. "product/property/type"
31
	 * @param string $site Unique site code
32
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
33
	 */
34
	public function deleteAction( ServerRequestInterface $request, $resource, $site = 'default' )
35
	{
36
		$client = $this->createAdmin( $site, $resource, $request->getAttribute( 'lang', 'en' ) );
37
		return $client->delete( $request, new Response() );
38
	}
39
40
41
	/**
42
	 * Returns the requested resource object or list of resource objects
43
	 *
44
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
45
	 * @param string Resource location, e.g. "product/property/type"
46
	 * @param string $site Unique site code
47
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
48
	 */
49
	public function getAction( ServerRequestInterface $request, $resource, $site = 'default' )
50
	{
51
		$client = $this->createAdmin( $site, $resource, $request->getAttribute( 'lang', 'en' ) );
52
		return $client->get( $request, new Response() );
53
	}
54
55
56
	/**
57
	 * Updates a resource object or a list of resource objects
58
	 *
59
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
60
	 * @param string Resource location, e.g. "product/property/type"
61
	 * @param string $site Unique site code
62
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
63
	 */
64
	public function patchAction( ServerRequestInterface $request, $resource, $site = 'default' )
65
	{
66
		$client = $this->createAdmin( $site, $resource, $request->getAttribute( 'lang', 'en' ) );
67
		return $client->patch( $request, new Response() );
68
	}
69
70
71
	/**
72
	 * Creates a new resource object or a list of resource objects
73
	 *
74
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
75
	 * @param string Resource location, e.g. "product/property/type"
76
	 * @param string $site Unique site code
77
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
78
	 */
79
	public function postAction( ServerRequestInterface $request, $resource, $site = 'default' )
80
	{
81
		$client = $this->createAdmin( $site, $resource, $request->getAttribute( 'lang', 'en' ) );
82
		return $client->post( $request, new Response() );
83
	}
84
85
86
	/**
87
	 * Creates or updates a single resource object
88
	 *
89
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
90
	 * @param string Resource location, e.g. "product/property/type"
91
	 * @param string $site Unique site code
92
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
93
	 */
94
	public function putAction( ServerRequestInterface $request, $resource, $site = 'default' )
95
	{
96
		$client = $this->createAdmin( $site, $resource, $request->getAttribute( 'lang', 'en' ) );
97
		return $client->put( $request, new Response() );
98
	}
99
100
101
	/**
102
	 * Returns the available HTTP verbs and the resource URLs
103
	 *
104
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
105
	 * @param string Resource location, e.g. "product/property/type"
106
	 * @param string $site Unique site code
107
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
108
	 */
109
	public function optionsAction( ServerRequestInterface $request, $resource = '', $site = 'default' )
110
	{
111
		$client = $this->createAdmin( $site, $resource, $request->getAttribute( 'lang', 'en' ) );
112
		return $client->options( $request, new Response() );
113
	}
114
115
116
	/**
117
	 * Returns the resource controller
118
	 *
119
	 * @param string $site Unique site code
120
	 * @param string Resource location, e.g. "product/property/type"
121
	 * @param string $lang Language code
122
	 * @return \Aimeos\MShop\Context\Item\Iface Context item
123
	 */
124
	protected function createAdmin( $site, $resource, $lang )
125
	{
126
		$aimeos = $this->get( 'aimeos' )->get();
127
		$templatePaths = $aimeos->getCustomPaths( 'admin/jsonadm/templates' );
128
129
		$context = $this->get( 'aimeos_context' )->get( false, 'backend' );
130
		$context->setI18n( $this->get('aimeos_i18n')->get( array( $lang, 'en' ) ) );
131
		$context->setLocale( $this->get('aimeos_locale')->getBackend( $context, $site ) );
132
133
		$view = $this->get('aimeos_view')->create( $context, $templatePaths, $lang );
134
		$context->setView( $view );
135
136
		return \Aimeos\Admin\JsonAdm::create( $context, $aimeos, $resource );
137
	}
138
}
139