Completed
Push — master ( b53863...ac109f )
by Aimeos
07:38
created

Standard::get()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 60
Code Lines 25

Duplication

Lines 60
Ratio 100 %

Importance

Changes 0
Metric Value
dl 60
loc 60
rs 8.9618
c 0
b 0
f 0
cc 4
eloc 25
nc 8
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
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
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
25
	implements \Aimeos\Client\JsonApi\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
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 View Code Duplication
	public function get( ServerRequestInterface $request, ResponseInterface $response )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 = array( array(
1 ignored issue
show
Bug introduced by
Accessing errors on the interface Aimeos\MW\View\Iface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
52
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
53
				'detail' => $e->getTraceAsString(),
54
			) );
55
		}
56
		catch( \Exception $e )
57
		{
58
			$status = 500;
59
			$view->errors = array( array(
1 ignored issue
show
Bug introduced by
Accessing errors on the interface Aimeos\MW\View\Iface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
60
				'title' => $e->getMessage(),
61
				'detail' => $e->getTraceAsString(),
62
			) );
63
		}
64
65
		/** client/jsonapi/locale/standard/template-get
66
		 * Relative path to the locale lists JSON API template for GET requests
67
		 *
68
		 * The template file contains the code and processing instructions
69
		 * to generate the result shown in the JSON API body. The
70
		 * configuration string is the path to the template file relative
71
		 * to the templates directory (usually in client/jsonapi/templates).
72
		 *
73
		 * You can overwrite the template file configuration in extensions and
74
		 * provide alternative templates. These alternative templates should be
75
		 * named like the default one but with the string "default" replaced by
76
		 * an unique name. You may use the name of your project for this. If
77
		 * you've implemented an alternative client class as well, "standard"
78
		 * should be replaced by the name of the new class.
79
		 *
80
		 * @param string Relative path to the template creating the body for the GET method of the JSON API
81
		 * @since 2017.03
82
		 * @category Developer
83
		 */
84
		$tplconf = 'client/jsonapi/locale/standard/template-get';
85
		$default = 'locale/get-default.php';
86
87
		$body = $view->render( $view->config( $tplconf, $default ) );
88
89
		return $response->withHeader( 'Allow', 'GET' )
90
			->withHeader( 'Content-Type', 'application/vnd.api+json' )
91
			->withBody( $view->response()->createStreamFromString( $body ) )
92
			->withStatus( $status );
93
	}
94
95
96
	/**
97
	 * Retrieves the item and adds the data to the view
98
	 *
99
	 * @param \Aimeos\MW\View\Iface $view View instance
100
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
101
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
102
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
103
	 */
104 View Code Duplication
	protected function getItem( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response )
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
	{
106
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'locale' );
107
108
		$view->items = $cntl->getItem( $view->param( 'id' ) );
1 ignored issue
show
Bug introduced by
Accessing items on the interface Aimeos\MW\View\Iface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
109
		$view->total = 1;
1 ignored issue
show
Bug introduced by
Accessing total on the interface Aimeos\MW\View\Iface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
110
111
		return $response;
112
	}
113
114
115
	/**
116
	 * Retrieves the items and adds the data to the view
117
	 *
118
	 * @param \Aimeos\MW\View\Iface $view View instance
119
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
120
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
121
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
122
	 */
123
	protected function getItems( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response )
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
	{
125
		$total = 0;
126
127
		$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'locale' );
128
129
		$filter = $cntl->createFilter();
130
		$filter = $this->initCriteriaConditions( $filter, $view->param() );
131
132
		$view->items = $cntl->searchItems( $filter, [], $total );
1 ignored issue
show
Bug introduced by
Accessing items on the interface Aimeos\MW\View\Iface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
133
		$view->total = $total;
1 ignored issue
show
Bug introduced by
Accessing total on the interface Aimeos\MW\View\Iface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
134
135
		return $response;
136
	}
137
}
138