Completed
Push — master ( ba7de8...b94793 )
by Aimeos
02:24
created

Standard::get()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
rs 8.8571
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi;
12
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
17
/**
18
 * JSON API standard client
19
 *
20
 * @package Client
21
 * @subpackage JsonApi
22
 */
23
class Standard
24
	extends \Aimeos\Client\JsonApi\Base
25
	implements \Aimeos\Client\JsonApi\Iface
26
{
27
	/**
28
	 * Retrieves the resource or the resource list
29
	 *
30
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
31
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
32
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
33
	 */
34
	public function get( ServerRequestInterface $request, ResponseInterface $response )
35
	{
36
		$view = $this->getView();
37
38
		/** client/jsonapi/standard/template-get
39
		 * Relative path to the default JSON API template for unknown GET request
40
		 *
41
		 * The template file contains the code and processing instructions
42
		 * to generate the result shown in the JSON API body. The
43
		 * configuration string is the path to the template file relative
44
		 * to the templates directory (usually in client/jsonapi/templates).
45
		 *
46
		 * You can overwrite the template file configuration in extensions and
47
		 * provide alternative templates. These alternative templates should be
48
		 * named like the default one but with the string "standard" replaced by
49
		 * an unique name. You may use the name of your project for this. If
50
		 * you've implemented an alternative client class as well, "standard"
51
		 * should be replaced by the name of the new class.
52
		 *
53
		 * @param string Relative path to the template creating the body for the JSON API GET response
54
		 * @since 2017.05
55
		 * @category Developer
56
		 * @see client/jsonapi/standard/template-options
57
		 */
58
		$tplconf = 'client/jsonapi/standard/template-get';
59
		$default = 'get-standard.php';
60
61
		$body = $view->render( $view->config( $tplconf, $default ) );
62
63
		return $response->withHeader( 'Allow', 'OPTIONS' )
64
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
65
			->withBody( $view->response()->createStreamFromString( $body ) )
66
			->withStatus( 200 );
67
	}
68
69
70
	/**
71
	 * Returns the available REST verbs and the available resources
72
	 *
73
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
74
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
75
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
76
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
77
	 */
78
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
79
	{
80
		$view = $this->getView();
81
82
		try
83
		{
84
			/** client/jsonapi/resources
85
			 * A list of resource names whose clients are available for the JSON API
86
			 *
87
			 * The HTTP OPTIONS method returns a list of resources known by the
88
			 * JSON API including their URLs. The list of available resources
89
			 * can be exteded dynamically be implementing a new Jsonadm client
90
			 * class handling request for this new domain.
91
			 *
92
			 * To add the new domain client to the list of resources returned
93
			 * by the HTTP OPTIONS method, you have to add its name in lower case
94
			 * to the existing configuration.
95
			 *
96
			 * @param array List of resource names
97
			 * @since 2017.03
98
			 * @category Developer
99
			 */
100
			$default = ['attribute', 'basket', 'catalog', 'customer', 'locale', 'order', 'product', 'service', 'stock'];
101
			$resources = $this->getContext()->getConfig()->get( 'client/jsonapi/resources', $default );
102
103
			$view->resources = (array) $resources;
104
			$view->prefix = $prefix;
105
			$status = 200;
106
		}
107
		catch( \Exception $e )
108
		{
109
			$status = 500;
110
			$view->errors = $this->getErrorDetails( $e );
111
		}
112
113
		/** client/jsonapi/standard/template-options
114
		 * Relative path to the JSON API template for OPTIONS requests
115
		 *
116
		 * The template file contains the code and processing instructions
117
		 * to generate the result shown in the JSON API body. The
118
		 * configuration string is the path to the template file relative
119
		 * to the templates directory (usually in client/jsonapi/templates).
120
		 *
121
		 * You can overwrite the template file configuration in extensions and
122
		 * provide alternative templates. These alternative templates should be
123
		 * named like the default one but with the string "standard" replaced by
124
		 * an unique name. You may use the name of your project for this. If
125
		 * you've implemented an alternative client class as well, "standard"
126
		 * should be replaced by the name of the new class.
127
		 *
128
		 * @param string Relative path to the template creating the body for the OPTIONS method of the JSON API
129
		 * @since 2017.02
130
		 * @category Developer
131
		 * @see client/jsonapi/standard/template-get
132
		 */
133
		$tplconf = 'client/jsonapi/standard/template-options';
134
		$default = 'options-standard.php';
135
136
		$body = $view->render( $view->config( $tplconf, $default ) );
137
138
		return $response->withHeader( 'Allow', 'GET' )
139
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
140
			->withBody( $view->response()->createStreamFromString( $body ) )
141
			->withStatus( $status );
142
	}
143
}
144