Completed
Push — master ( 9dd5c5...5796dc )
by Aimeos
02:20
created

Jsonapi::createClient()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 16
nop 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
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 frontend JSON REST API
20
 *
21
 * @package Slim
22
 * @subpackage Controller
23
 */
24
class Jsonapi
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::createClient( $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::createClient( $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::createClient( $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::createClient( $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::createClient( $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::createClient( $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\Client\JsonApi\Iface JSON client controller
124
	 */
125
	protected static function createClient( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
126
	{
127
		$params = $request->getQueryParams();
128
		$resource = ( isset( $args['resource'] ) ? $args['resource'] : ( isset( $params['resource'] ) ? $params['resource'] : null ) );
129
		$related = ( isset( $args['related'] ) ? $args['related'] : ( isset( $params['related'] ) ? $params['related'] : null ) );
130
		$tmplPaths = $container->get( 'aimeos' )->getCustomPaths( 'client/jsonapi/templates' );
131
132
		$context = $container->get( 'aimeos_context' )->get( true, $args );
133
		$langid = $context->getLocale()->getLanguageId();
134
		$config = $context->getConfig();
135
136
		$view = $container->get( 'aimeos_view' )->create( $config, $request, $response, $args, $tmplPaths, $langid );
137
		$context->setView( $view );
138
139
		return \Aimeos\Client\JsonApi\Factory::createClient( $context, $tmplPaths, $resource . '/' . $related );
140
	}
141
}
142