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

Standard::get()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 54
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 54
rs 8.9617
cc 6
nc 17
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Locale;
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
			if( $view->param( 'id' ) != '' ) {
41
				$response = $this->getItem( $view, $request, $response );
42
			} else {
43
				$response = $this->getItems( $view, $request, $response );
44
			}
45
46
			$status = 200;
47
		}
48
		catch( \Aimeos\MShop\Exception $e )
49
		{
50
			$status = 404;
51
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
52
		}
53
		catch( \Exception $e )
54
		{
55
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
56
			$view->errors = $this->getErrorDetails( $e );
57
		}
58
59
		/** client/jsonapi/locale/standard/template
60
		 * Relative path to the locale lists JSON API template
61
		 *
62
		 * The template file contains the code and processing instructions
63
		 * to generate the result shown in the JSON API body. The
64
		 * configuration string is the path to the template file relative
65
		 * to the templates directory (usually in client/jsonapi/templates).
66
		 *
67
		 * You can overwrite the template file configuration in extensions and
68
		 * provide alternative templates. These alternative templates should be
69
		 * named like the default one but with the string "standard" replaced by
70
		 * an unique name. You may use the name of your project for this. If
71
		 * you've implemented an alternative client class as well, "standard"
72
		 * should be replaced by the name of the new class.
73
		 *
74
		 * @param string Relative path to the template creating the body for the GET method of the JSON API
75
		 * @since 2017.03
76
		 * @category Developer
77
		 */
78
		$tplconf = 'client/jsonapi/locale/standard/template';
79
		$default = 'locale/standard';
80
81
		$body = $view->render( $view->config( $tplconf, $default ) );
82
83
		return $response->withHeader( 'Allow', 'GET,OPTIONS' )
84
			->withHeader( 'Cache-Control', 'max-age=300' )
85
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
86
			->withBody( $view->response()->createStreamFromString( $body ) )
87
			->withStatus( $status );
88
	}
89
90
91
	/**
92
	 * Returns the available REST verbs and the available parameters
93
	 *
94
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
95
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
96
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
97
	 */
98
	public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
99
	{
100
		return $this->getOptionsResponse( $request, $response, 'GET,OPTIONS' );
101
	}
102
103
104
	/**
105
	 * Retrieves the item and adds the data to the view
106
	 *
107
	 * @param \Aimeos\MW\View\Iface $view View instance
108
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
109
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
110
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
111
	 */
112
	protected function getItem( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
113
	{
114
		$view->items = \Aimeos\Controller\Frontend::create( $this->getContext(), 'locale' )->get( $view->param( 'id' ) );
115
		$view->total = 1;
116
117
		return $response;
118
	}
119
120
121
	/**
122
	 * Retrieves the items and adds the data to the view
123
	 *
124
	 * @param \Aimeos\MW\View\Iface $view View instance
125
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
126
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
127
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
128
	 */
129
	protected function getItems( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
130
	{
131
		$total = 0;
132
133
		$view->items = \Aimeos\Controller\Frontend::create( $this->getContext(), 'locale' )
134
			->parse( $view->param( 'filter', [] ) )->sort( $view->param( 'sort', 'position' ) )
135
			->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 25 ) )
136
			->search( $total );
137
		$view->total = $total;
138
139
		return $response;
140
	}
141
}
142