Completed
Push — master ( 5e44e2...ae0969 )
by Aimeos
07:40
created

Base   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 94
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A __call() 0 4 1
A createFilter() 0 4 1
A getItem() 0 4 1
A searchItems() 0 4 1
A getController() 0 4 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Locale\Decorator;
12
13
14
/**
15
 * Base for locale frontend controller decorators
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
abstract class Base
21
	extends \Aimeos\Controller\Frontend\Base
2 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...
22
	implements \Aimeos\Controller\Frontend\Common\Decorator\Iface, \Aimeos\Controller\Frontend\Locale\Iface
1 ignored issue
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	private $controller;
25
26
27
	/**
28
	 * Initializes the controller decorator.
29
	 *
30
	 * @param \Aimeos\Controller\Frontend\Iface $controller Controller object
31
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
32
	 */
33
	public function __construct( \Aimeos\Controller\Frontend\Iface $controller, \Aimeos\MShop\Context\Item\Iface $context )
34
	{
35
		$iface = '\Aimeos\Controller\Frontend\Locale\Iface';
36
		if( !( $controller instanceof $iface ) )
37
		{
38
			$msg = sprintf( 'Class "%1$s" does not implement interface "%2$s"', get_class( $controller ), $iface );
39
			throw new \Aimeos\Controller\Frontend\Exception( $msg );
40
		}
41
42
		$this->controller = $controller;
43
44
		parent::__construct( $context );
45
	}
46
47
48
	/**
49
	 * Passes unknown methods to wrapped objects.
50
	 *
51
	 * @param string $name Name of the method
52
	 * @param array $param List of method parameter
53
	 * @return mixed Returns the value of the called method
54
	 * @throws \Aimeos\Controller\Frontend\Exception If method call failed
55
	 */
56
	public function __call( $name, array $param )
57
	{
58
		return @call_user_func_array( array( $this->controller, $name ), $param );
59
	}
60
61
62
	/**
63
	 * Returns the default locale filter
64
	 *
65
	 * @param boolean True to add default criteria
66
	 * @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
67
	 * @since 2017.03
68
	 */
69
	public function createFilter()
70
	{
71
		return $this->controller->createFilter();
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\Controller\Frontend\Iface as the method createFilter() does only exist in the following implementations of said interface: Aimeos\Controller\Fronte...ttribute\Decorator\Base, Aimeos\Controller\Frontend\Attribute\Standard, Aimeos\Controller\Frontend\Catalog\Decorator\Base, Aimeos\Controller\Frontend\Catalog\Standard, Aimeos\Controller\Frontend\Locale\Decorator\Base, Aimeos\Controller\Frontend\Locale\Standard, Aimeos\Controller\Frontend\Product\Decorator\Base, Aimeos\Controller\Frontend\Product\Standard, Aimeos\Controller\Frontend\Stock\Decorator\Base, Aimeos\Controller\Frontend\Stock\Standard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
72
	}
73
74
75
	/**
76
	 * Returns the locale item for the given locale ID
77
	 *
78
	 * @param string $id Unique locale ID
79
	 * @param string[] $domains Domain names of items that are associated with the locales and that should be fetched too
80
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item including the referenced domains items
81
	 * @since 2017.03
82
	 */
83
	public function getItem( $id, array $domains = array() )
84
	{
85
		return $this->controller->getItem( $id, $domains );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\Controller\Frontend\Iface as the method getItem() does only exist in the following implementations of said interface: Aimeos\Controller\Fronte...ttribute\Decorator\Base, Aimeos\Controller\Frontend\Attribute\Standard, Aimeos\Controller\Frontend\Locale\Decorator\Base, Aimeos\Controller\Frontend\Locale\Standard, Aimeos\Controller\Frontend\Product\Decorator\Base, Aimeos\Controller\Frontend\Product\Standard, Aimeos\Controller\Frontend\Stock\Decorator\Base, Aimeos\Controller\Frontend\Stock\Standard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
86
	}
87
88
89
	/**
90
	 * Returns the locales filtered by the given criteria object
91
	 *
92
	 * @param \Aimeos\MW\Criteria\Iface $filter Critera object which contains the filter conditions
93
	 * @param string[] $domains Domain names of items that are associated with the locales and that should be fetched too
94
	 * @param integer &$total Parameter where the total number of found locales will be stored in
95
	 * @return array Ordered list of locale items implementing \Aimeos\MShop\Locale\Item\Iface
96
	 * @since 2017.03
97
	 */
98
	public function searchItems( \Aimeos\MW\Criteria\Iface $filter, array $domains = array(), &$total = null )
99
	{
100
		return $this->controller->searchItems( $filter, $domains, $total );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\Controller\Frontend\Iface as the method searchItems() does only exist in the following implementations of said interface: Aimeos\Controller\Fronte...ttribute\Decorator\Base, Aimeos\Controller\Frontend\Attribute\Standard, Aimeos\Controller\Frontend\Locale\Decorator\Base, Aimeos\Controller\Frontend\Locale\Standard, Aimeos\Controller\Frontend\Product\Decorator\Base, Aimeos\Controller\Frontend\Product\Standard, Aimeos\Controller\Frontend\Stock\Decorator\Base, Aimeos\Controller\Frontend\Stock\Standard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
101
	}
102
103
104
	/**
105
	 * Returns the frontend controller
106
	 *
107
	 * @return \Aimeos\Controller\Frontend\Locale\Iface Frontend controller object
108
	 */
109
	protected function getController()
110
	{
111
		return $this->controller;
112
	}
113
}
114