Completed
Push — master ( e8440a...f62092 )
by Aimeos
02:50
created

Standard::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 13
nc 1
nop 3
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\Service;
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
	 * Returns 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
		try
39
		{
40
			$ref = $view->param( 'include', ['media', 'price', 'text'] );
41
42
			if( is_string( $ref ) ) {
43
				$ref = explode( ',', $ref );
44
			}
45
46
			$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'service' );
47
			$basketCntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'basket' );
48
			$basket = $basketCntl->get();
49
50
			if( ( $id = $view->param( 'id' ) ) != '' )
51
			{
52
				$provider = $cntl->getProvider( $id, $ref );
53
54
				if( $provider->isAvailable( $basket ) === true )
55
				{
56
					$view->attributes = [$id => $provider->getConfigFE( $basket )];
57
					$view->prices = [$id => $provider->calcPrice( $basket )];
58
					$view->items = $provider->getServiceItem();
59
					$view->total = 1;
60
				}
61
			}
62
			else
63
			{
64
				$attributes = $prices = $items = [];
65
				$type = $view->param( 'filter/cs_type' );
66
67
				foreach( $cntl->getProviders( $type, $ref ) as $id => $provider )
68
				{
69
					if( $provider->isAvailable( $basket ) === true )
70
					{
71
						$attributes[$id] = $provider->getConfigFE( $basket );
72
						$prices[$id] = $provider->calcPrice( $basket );
73
						$items[$id] = $provider->getServiceItem();
74
					}
75
				}
76
77
				$view->attributes = $attributes;
78
				$view->prices = $prices;
79
				$view->items = $items;
80
				$view->total = count( $items );
81
			}
82
83
			$status = 200;
84
		}
85
		catch( \Aimeos\MShop\Exception $e )
86
		{
87
			$status = 404;
88
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
89
		}
90
		catch( \Exception $e )
91
		{
92
			$status = 500;
93
			$view->errors = $this->getErrorDetails( $e );
94
		}
95
96
		/** client/jsonapi/service/standard/template
97
		 * Relative path to the service JSON API template
98
		 *
99
		 * The template file contains the code and processing instructions
100
		 * to generate the result shown in the JSON API body. The
101
		 * configuration string is the path to the template file relative
102
		 * to the templates directory (usually in client/jsonapi/templates).
103
		 *
104
		 * You can overwrite the template file configuration in extensions and
105
		 * provide alternative templates. These alternative templates should be
106
		 * named like the default one but with the string "standard" replaced by
107
		 * an unique name. You may use the name of your project for this. If
108
		 * you've implemented an alternative client class as well, "standard"
109
		 * should be replaced by the name of the new class.
110
		 *
111
		 * @param string Relative path to the template creating the body for the GET method of the JSON API
112
		 * @since 2017.03
113
		 * @category Developer
114
		 */
115
		$tplconf = 'client/jsonapi/service/standard/template';
116
		$default = 'service/standard.php';
117
118
		$body = $view->render( $view->config( $tplconf, $default ) );
119
120
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
121
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
122
			->withBody( $view->response()->createStreamFromString( $body ) )
123
			->withStatus( $status );
124
	}
125
126
127
	/**
128
	 * Returns the available REST verbs and the available parameters
129
	 *
130
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
131
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
132
	 * @param string|null $prefix Form parameter prefix when nesting parameters is required
133
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
134
	 */
135
	public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null )
136
	{
137
		$view = $this->getView();
138
		$view->filter = [
139
			'cs_type' => [
140
				'label' => 'Type of the service items that should be returned ("delivery" or "payment")',
141
				'type' => 'string', 'default' => '', 'required' => false,
142
			],
143
		];
144
145
		$tplconf = 'client/jsonapi/standard/template-options';
146
		$default = 'options-standard.php';
147
148
		$body = $view->render( $view->config( $tplconf, $default ) );
149
150
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
151
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
152
			->withBody( $view->response()->createStreamFromString( $body ) )
153
			->withStatus( 200 );
154
	}
155
}
156