Completed
Push — master ( 2a954a...31dc8b )
by Aimeos
02:47
created

Base::checkServiceAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Service\Decorator;
12
13
14
/**
15
 * Base for basket frontend controller decorators
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
abstract class Base extends \Aimeos\Controller\Frontend\Common\Decorator\Base
21
{
22
	/**
23
	 * Returns the service items that are available for the service type and the content of the basket.
24
	 *
25
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
26
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket of the user
27
	 * @param array $ref List of domains for which the items referenced by the services should be fetched too
28
	 * @return array List of service items implementing \Aimeos\MShop\Service\Item\Iface with referenced items
29
	 */
30
	public function getServices( $type, \Aimeos\MShop\Order\Item\Base\Iface $basket,
31
		$ref = array( 'media', 'price', 'text' ) )
32
	{
33
		$this->getController()->getServices( $type, $basket, $ref );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\Controller\Frontend\Common\Iface as the method getServices() does only exist in the following implementations of said interface: Aimeos\Controller\Frontend\Service\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...
34
	}
35
36
37
	/**
38
	 * Returns the list of attribute definitions which must be used to render the input form where the customer can
39
	 * enter or chose the required data necessary by the service provider.
40
	 *
41
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
42
	 * @param string $serviceId Identifier of one of the service option returned by getService()
43
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
44
	 * @return array List of attribute definitions implementing \Aimeos\MW\Criteria\Attribute\Iface
45
	 */
46
	public function getServiceAttributes( $type, $serviceId, \Aimeos\MShop\Order\Item\Base\Iface $basket )
47
	{
48
		$this->getController()->getServiceAttributes( $type, $serviceId, $basket );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\Controller\Frontend\Common\Iface as the method getServiceAttributes() does only exist in the following implementations of said interface: Aimeos\Controller\Frontend\Service\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...
49
	}
50
51
52
	/**
53
	 * Returns the price of the service.
54
	 *
55
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
56
	 * @param string $serviceId Identifier of one of the service option returned by getService()
57
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket with products
58
	 * @return \Aimeos\MShop\Price\Item\Iface Price item
59
	 * @throws \Aimeos\Controller\Frontend\Service\Exception If no active service provider for this ID is available
60
	 * @throws \Aimeos\MShop\Exception If service provider isn't available
61
	 * @throws \Exception If an error occurs
62
	 */
63
	public function getServicePrice( $type, $serviceId, \Aimeos\MShop\Order\Item\Base\Iface $basket )
64
	{
65
		$this->getController()->getServicePrice( $type, $serviceId, $basket );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\Controller\Frontend\Common\Iface as the method getServicePrice() does only exist in the following implementations of said interface: Aimeos\Controller\Frontend\Service\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...
66
	}
67
68
69
	/**
70
	 * Returns a list of attributes that are invalid.
71
	 *
72
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
73
	 * @param string $serviceId Identifier of the service option chosen by the customer
74
	 * @param array $attributes List of key/value pairs with name of the attribute from attribute definition object as
75
	 * 	key and the string entered by the customer as value
76
	 * @return array List of key/value pairs of attributes keys and an error message for values that are invalid or
77
	 * 	missing
78
	 */
79
	public function checkServiceAttributes( $type, $serviceId, array $attributes )
80
	{
81
		$this->getController()->checkServiceAttributes( $type, $serviceId, $attributes );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\Controller\Frontend\Common\Iface as the method checkServiceAttributes() does only exist in the following implementations of said interface: Aimeos\Controller\Frontend\Service\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...
82
	}
83
}
84