Completed
Push — master ( 4c5eea...8c1ffe )
by Aimeos
02:39
created

Base::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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\Attribute\Decorator;
12
13
14
/**
15
 * Base for attribute 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\Attribute\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\Attribute\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 given search filter with the conditions attached for filtering by type code
64
	 *
65
	 * @param \Aimeos\MW\Criteria\Iface $filter Criteria object used for attribute search
66
	 * @param array $codes List of attribute type codes
67
	 * @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
68
	 * @since 2017.03
69
	 */
70
	public function addFilterTypes( \Aimeos\MW\Criteria\Iface $filter, array $codes )
71
	{
72
		return $this->controller->addFilterTypes( $filter, $codes );
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 addFilterTypes() does only exist in the following implementations of said interface: Aimeos\Controller\Fronte...ttribute\Decorator\Base, Aimeos\Controller\Frontend\Attribute\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...
73
	}
74
75
76
	/**
77
	 * Returns the default attribute filter
78
	 *
79
	 * @param boolean True to add default criteria
80
	 * @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
81
	 * @since 2017.03
82
	 */
83
	public function createFilter()
84
	{
85
		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\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 attribute item for the given attribute ID
91
	 *
92
	 * @param string $id Unique attribute ID
93
	 * @param string[] $domains Domain names of items that are associated with the attributes and that should be fetched too
94
	 * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items
95
	 * @since 2017.03
96
	 */
97
	public function getItem( $id, array $domains = array( 'media', 'price', 'text' ) )
98
	{
99
		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\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...
100
	}
101
102
103
	/**
104
	 * Returns the attribute items for the given attribute IDs
105
	 *
106
	 * @param string $ids Unique attribute IDs
107
	 * @param string[] $domains Domain names of items that are associated with the attributes and that should be fetched too
108
	 * @return \Aimeos\MShop\Attribute\Item\Iface[] Associative list of attribute item including the referenced domains items
109
	 * @since 2017.03
110
	 */
111
	public function getItems( array $ids, array $domains = array( 'media', 'price', 'text' ) )
112
	{
113
		return $this->controller->getItems( $ids, $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 getItems() does only exist in the following implementations of said interface: Aimeos\Controller\Fronte...ttribute\Decorator\Base, Aimeos\Controller\Frontend\Attribute\Standard, Aimeos\Controller\Frontend\Product\Decorator\Base, Aimeos\Controller\Frontend\Product\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...
114
	}
115
116
117
	/**
118
	 * Returns the attributes filtered by the given criteria object
119
	 *
120
	 * @param \Aimeos\MW\Criteria\Iface $filter Critera object which contains the filter conditions
121
	 * @param string[] $domains Domain names of items that are associated with the attributes and that should be fetched too
122
	 * @param integer &$total Parameter where the total number of found attributes will be stored in
123
	 * @return array Ordered list of attribute items implementing \Aimeos\MShop\Attribute\Item\Iface
124
	 * @since 2017.03
125
	 */
126
	public function searchItems( \Aimeos\MW\Criteria\Iface $filter, array $domains = array( 'media', 'price', 'text' ), &$total = null )
127
	{
128
		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\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...
129
	}
130
131
132
	/**
133
	 * Returns the frontend controller
134
	 *
135
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Frontend controller object
136
	 */
137
	protected function getController()
138
	{
139
		return $this->controller;
140
	}
141
}
142