Completed
Push — master ( 98e6c7...18f4ad )
by Aimeos
14:21
created

JsonapiController::createClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2017
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 client REST API
20
 *
21
 * @package symfony
22
 * @subpackage Controller
23
 */
24
class JsonapiController 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"
31
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
32
	 */
33
	public function deleteAction( ServerRequestInterface $request, $resource )
34
	{
35
		return $this->createClient( $resource )->delete( $request, new Response() );
36
	}
37
38
39
	/**
40
	 * Returns the requested resource object or list of resource objects
41
	 *
42
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
43
	 * @param string Resource location, e.g. "product"
44
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
45
	 */
46
	public function getAction( ServerRequestInterface $request, $resource )
47
	{
48
		return $this->createClient( $resource )->get( $request, new Response() );
49
	}
50
51
52
	/**
53
	 * Updates a resource object or a list of resource objects
54
	 *
55
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
56
	 * @param string Resource location, e.g. "product"
57
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
58
	 */
59
	public function patchAction( ServerRequestInterface $request, $resource )
60
	{
61
		return $this->createClient( $resource )->patch( $request, new Response() );
62
	}
63
64
65
	/**
66
	 * Creates a new resource object or a list of resource objects
67
	 *
68
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
69
	 * @param string Resource location, e.g. "product"
70
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
71
	 */
72
	public function postAction( ServerRequestInterface $request, $resource )
73
	{
74
		return $this->createClient( $resource )->post( $request, new Response() );
75
	}
76
77
78
	/**
79
	 * Creates or updates a single resource object
80
	 *
81
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
82
	 * @param string Resource location, e.g. "product"
83
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
84
	 */
85
	public function putAction( ServerRequestInterface $request, $resource )
86
	{
87
		return $this->createClient( $resource )->put( $request, new Response() );
88
	}
89
90
91
	/**
92
	 * Returns the available HTTP verbs and the resource URLs
93
	 *
94
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
95
	 * @param string Resource location, e.g. "product"
96
	 * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output
97
	 */
98
	public function optionsAction( ServerRequestInterface $request, $resource = '' )
99
	{
100
		return $this->createClient( $resource )->options( $request, new Response() );
101
	}
102
103
104
	/**
105
	 * Returns the resource controller
106
	 *
107
	 * @param string Resource location, e.g. "product"
108
	 * @return \Aimeos\MShop\Context\Item\Iface Context item
109
	 */
110
	protected function createClient( $resource )
111
	{
112
		$tmplPaths = $this->container->get( 'aimeos' )->get()->getCustomPaths( 'client/jsonapi/templates' );
113
		$context = $this->container->get( 'aimeos_context' )->get();
114
		$langid = $context->getLocale()->getLanguageId();
115
116
		$view = $this->container->get( 'aimeos_view' )->create( $context, $tmplPaths, $langid );
117
		$context->setView( $view );
118
119
		return \Aimeos\Client\JsonApi\Factory::createClient( $context, $tmplPaths, $resource );
120
	}
121
}
122