Completed
Push — master ( 270945...1cb1eb )
by Aimeos
03:29
created

Standard   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 60
c 1
b 0
f 0
dl 0
loc 135
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 21 1
C get() 0 93 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
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 ) : \Psr\Http\Message\ResponseInterface
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::create( $this->getContext(), 'service' )->uses( $ref );
47
			$basketCntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'basket' );
48
			$basket = $basketCntl->get();
49
50
			if( ( $id = $view->param( 'id' ) ) != '' )
51
			{
52
				$provider = $cntl->getProvider( $id );
53
54
				if( $provider->isAvailable( $basket ) === true )
55
				{
56
					$view->prices = map( [$id => $provider->calcPrice( $basket )] );
57
					$view->attributes = [$id => $provider->getConfigFE( $basket )];
58
					$view->items = $provider->getServiceItem();
59
					$view->total = 1;
60
				}
61
			}
62
			else
63
			{
64
				$attributes = [];
65
				$items = map();
66
				$prices = map();
67
				$cntl->type( $view->param( 'filter/cs_type' ) );
68
69
				foreach( $cntl->getProviders() as $id => $provider )
70
				{
71
					if( $provider->isAvailable( $basket ) === true )
72
					{
73
						$attributes[$id] = $provider->getConfigFE( $basket );
74
						$prices[$id] = $provider->calcPrice( $basket );
75
						$items[$id] = $provider->getServiceItem();
76
					}
77
				}
78
79
				$view->attributes = $attributes;
80
				$view->prices = $prices;
81
				$view->items = $items;
82
				$view->total = count( $items );
83
			}
84
85
			$status = 200;
86
		}
87
		catch( \Aimeos\MShop\Exception $e )
88
		{
89
			$status = 404;
90
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
91
		}
92
		catch( \Exception $e )
93
		{
94
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
95
			$view->errors = $this->getErrorDetails( $e );
96
		}
97
98
		/** client/jsonapi/service/standard/template
99
		 * Relative path to the service JSON API template
100
		 *
101
		 * The template file contains the code and processing instructions
102
		 * to generate the result shown in the JSON API body. The
103
		 * configuration string is the path to the template file relative
104
		 * to the templates directory (usually in client/jsonapi/templates).
105
		 *
106
		 * You can overwrite the template file configuration in extensions and
107
		 * provide alternative templates. These alternative templates should be
108
		 * named like the default one but with the string "standard" replaced by
109
		 * an unique name. You may use the name of your project for this. If
110
		 * you've implemented an alternative client class as well, "standard"
111
		 * should be replaced by the name of the new class.
112
		 *
113
		 * @param string Relative path to the template creating the body for the GET method of the JSON API
114
		 * @since 2017.03
115
		 * @category Developer
116
		 */
117
		$tplconf = 'client/jsonapi/service/standard/template';
118
		$default = 'service/standard';
119
120
		$body = $view->render( $view->config( $tplconf, $default ) );
121
122
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
123
			->withHeader( 'Cache-Control', 'max-age=300' )
124
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
125
			->withBody( $view->response()->createStreamFromString( $body ) )
126
			->withStatus( $status );
127
	}
128
129
130
	/**
131
	 * Returns the available REST verbs and the available parameters
132
	 *
133
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
134
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
135
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
136
	 */
137
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
138
	{
139
		$view = $this->getView();
140
141
		$view->filter = [
142
			'cs_type' => [
143
				'label' => 'Type of the service items that should be returned ("delivery" or "payment")',
144
				'type' => 'string', 'default' => '', 'required' => false,
145
			],
146
		];
147
148
		$tplconf = 'client/jsonapi/standard/template-options';
149
		$default = 'options-standard';
150
151
		$body = $view->render( $view->config( $tplconf, $default ) );
152
153
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
154
			->withHeader( 'Cache-Control', 'max-age=300' )
155
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
156
			->withBody( $view->response()->createStreamFromString( $body ) )
157
			->withStatus( 200 );
158
	}
159
}
160