Completed
Push — master ( def7d9...4bce11 )
by Aimeos
01:54
created

Jsonadm::createAdmin()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 8
nop 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Slim\Controller;
12
13
use Interop\Container\ContainerInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
18
/**
19
 * Aimeos controller for the JSON REST API
20
 *
21
 * @package Slim
22
 * @subpackage Controller
23
 */
24
class Jsonadm
25
{
26
	/**
27
	 * Deletes the resource object or a list of resource objects
28
	 *
29
	 * @param ContainerInterface $container Dependency injection container
30
	 * @param ServerRequestInterface $request Request object
31
	 * @param ResponseInterface $response Response object
32
	 * @param array $args Associative list of route parameters
33
	 * @return ResponseInterface $response Modified response object with generated output
34
	 */
35
	public static function deleteAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
36
	{
37
		return self::createAdmin( $container, $request, $response, $args )->delete( $request, $response );
38
	}
39
40
41
	/**
42
	 * Returns the requested resource object or list of resource objects
43
	 *
44
	 * @param ContainerInterface $container Dependency injection container
45
	 * @param ServerRequestInterface $request Request object
46
	 * @param ResponseInterface $response Response object
47
	 * @param array $args Associative list of route parameters
48
	 * @return ResponseInterface $response Modified response object with generated output
49
	 */
50
	public static function getAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
51
	{
52
		return self::createAdmin( $container, $request, $response, $args )->get( $request, $response );
53
	}
54
55
56
	/**
57
	 * Updates a resource object or a list of resource objects
58
	 *
59
	 * @param ContainerInterface $container Dependency injection container
60
	 * @param ServerRequestInterface $request Request object
61
	 * @param ResponseInterface $response Response object
62
	 * @param array $args Associative list of route parameters
63
	 * @return ResponseInterface $response Modified response object with generated output
64
	 */
65
	public static function patchAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
66
	{
67
		return self::createAdmin( $container, $request, $response, $args )->patch( $request, $response );
68
	}
69
70
71
	/**
72
	 * Creates a new resource object or a list of resource objects
73
	 *
74
	 * @param ContainerInterface $container Dependency injection container
75
	 * @param ServerRequestInterface $request Request object
76
	 * @param ResponseInterface $response Response object
77
	 * @param array $args Associative list of route parameters
78
	 * @return ResponseInterface $response Modified response object with generated output
79
	 */
80
	public static function postAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
81
	{
82
		return self::createAdmin( $container, $request, $response, $args )->post( $request, $response );
83
	}
84
85
86
	/**
87
	 * Creates or updates a single resource object
88
	 *
89
	 * @param ContainerInterface $container Dependency injection container
90
	 * @param ServerRequestInterface $request Request object
91
	 * @param ResponseInterface $response Response object
92
	 * @param array $args Associative list of route parameters
93
	 * @return ResponseInterface $response Modified response object with generated output
94
	 */
95
	public static function putAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
96
	{
97
		return self::createAdmin( $container, $request, $response, $args )->put( $request, $response );
98
	}
99
100
101
	/**
102
	 * Returns the available HTTP verbs and the resource URLs
103
	 *
104
	 * @param ContainerInterface $container Dependency injection container
105
	 * @param ServerRequestInterface $request Request object
106
	 * @param ResponseInterface $response Response object
107
	 * @param array $args Associative list of route parameters
108
	 * @return ResponseInterface $response Modified response object with generated output
109
	 */
110
	public static function optionsAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
111
	{
112
		return self::createAdmin( $container, $request, $response, $args )->options( $request, $response );
113
	}
114
115
116
	/**
117
	 * Returns the resource controller
118
	 *
119
	 * @param ContainerInterface $container Dependency injection container
120
	 * @param ServerRequestInterface $request Request object
121
	 * @param ResponseInterface $response Response object
122
	 * @param array $args Associative list of route parameters
123
	 * @return \Aimeos\Admin\JsonAdm\Iface JSON admin client
124
	 */
125
	protected static function createAdmin( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
126
	{
127
		$resource = ( isset( $args['resource'] ) ? $args['resource'] : null );
128
		$site = ( isset( $args['site'] ) ? $args['site'] : 'default' );
129
		$lang = ( isset( $args['lang'] ) ? $args['lang'] : 'en' );
130
131
		$aimeos = $container->get( 'aimeos' );
132
		$templatePaths = $aimeos->getCustomPaths( 'admin/jsonadm/templates' );
133
134
		$context = $container->get( 'aimeos.context' )->get( false, $args, 'backend' );
135
		$context->setI18n( $container->get( 'aimeos.i18n' )->get( array( $lang, 'en' ) ) );
136
		$context->setLocale( $container->get( 'aimeos.locale' )->getBackend( $context, $site ) );
137
138
		$view = $container->get( 'aimeos.view' )->create( $context, $request, $response, $args, $templatePaths, $lang );
139
		$context->setView( $view );
140
141
		return \Aimeos\Admin\JsonAdm::create( $context, $aimeos, $resource );
142
	}
143
}
144