Completed
Push — master ( 32337a...5c15c9 )
by Aimeos
10:07
created

Base::getProvider()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 59
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 5
nop 2
dl 0
loc 59
rs 8.7117
c 0
b 0
f 0

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 Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 * @package MShop
8
 * @subpackage Coupon
9
 */
10
11
namespace Aimeos\MShop\Coupon\Manager;
12
13
14
/**
15
 * Abstract class for coupon managers.
16
 *
17
 * @package MShop
18
 * @subpackage Coupon
19
 */
20
abstract class Base
21
	extends \Aimeos\MShop\Common\Manager\Base
22
{
23
	/**
24
	 * Returns the coupon model which belongs to the given code.
25
	 *
26
	 * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item interface
27
	 * @param string $code Coupon code
28
	 * @return \Aimeos\MShop\Coupon\Provider\Iface Returns a coupon provider instance
29
	 * @throws \Aimeos\MShop\Coupon\Exception If coupon couldn't be found
30
	 */
31
	public function getProvider( \Aimeos\MShop\Coupon\Item\Iface $item, $code )
32
	{
33
		$names = explode( ',', $item->getProvider() );
34
35
		if( ( $providername = array_shift( $names ) ) === null ) {
36
			throw new \Aimeos\MShop\Coupon\Exception( sprintf( 'Provider in "%1$s" not available', $item->getProvider() ) );
37
		}
38
39
		if( ctype_alnum( $providername ) === false ) {
40
			throw new \Aimeos\MShop\Coupon\Exception( sprintf( 'Invalid characters in provider name "%1$s"', $providername ) );
41
		}
42
43
		$interface = '\\Aimeos\\MShop\\Coupon\\Provider\\Factory\\Iface';
44
		$classname = '\\Aimeos\\MShop\\Coupon\\Provider\\' . $providername;
45
46
		if( class_exists( $classname ) === false ) {
47
			throw new \Aimeos\MShop\Coupon\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
48
		}
49
50
		$context = $this->getContext();
51
		$provider = new $classname( $context, $item, $code );
52
53
		if( ( $provider instanceof $interface ) === false )
54
		{
55
			$msg = sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $interface );
56
			throw new \Aimeos\MShop\Coupon\Exception( $msg );
57
		}
58
59
		/** mshop/coupon/provider/decorators
60
		 * Adds a list of decorators to all coupon provider objects automatcally
61
		 *
62
		 * Decorators extend the functionality of a class by adding new aspects
63
		 * (e.g. log what is currently done), executing the methods of the underlying
64
		 * class only in certain conditions (e.g. only for logged in users) or
65
		 * modify what is returned to the caller.
66
		 *
67
		 * This option allows you to wrap decorators
68
		 * ("\Aimeos\MShop\Coupon\Provider\Decorator\*") around the coupon provider.
69
		 *
70
		 *  mshop/coupon/provider/decorators = array( 'decorator1' )
71
		 *
72
		 * This would add the decorator named "decorator1" defined by
73
		 * "\Aimeos\MShop\Coupon\Provider\Decorator\Decorator1" to all coupon provider
74
		 * objects.
75
		 *
76
		 * @param array List of decorator names
77
		 * @since 2014.05
78
		 * @category Developer
79
		 * @see client/html/common/decorators/default
80
		 * @see client/html/account/favorite/decorators/excludes
81
		 * @see client/html/account/favorite/decorators/local
82
		 */
83
		$decorators = $context->getConfig()->get( 'mshop/coupon/provider/decorators', [] );
84
85
		$object = $this->addCouponDecorators( $item, $code, $provider, $names );
86
		$object = $this->addCouponDecorators( $item, $code, $object, $decorators );
87
88
		return $object->setObject( $object );
89
	}
90
91
92
	/**
93
	 * Wraps the named coupon decorators around the coupon provider.
94
	 *
95
	 * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object
96
	 * @param string $code Coupon code
97
	 * @param \Aimeos\MShop\Coupon\Provider\Iface $provider Coupon provider object
98
	 * @param array $names List of decorator names
99
	 * @return \Aimeos\MShop\Coupon\Provider\Iface Coupon provider wrapped by one or more coupon decorators
100
	 * @throws \Aimeos\MShop\Coupon\Exception If a coupon decorator couldn't be instantiated
101
	 */
102
	protected function addCouponDecorators( \Aimeos\MShop\Coupon\Item\Iface $item, $code,
103
		\Aimeos\MShop\Coupon\Provider\Iface $provider, array $names )
104
	{
105
		$iface = '\\Aimeos\\MShop\\Coupon\\Provider\\Decorator\\Iface';
106
		$classprefix = '\\Aimeos\\MShop\\Coupon\\Provider\\Decorator\\';
107
108
		foreach( $names as $name )
109
		{
110
			if( ctype_alnum( $name ) === false ) {
111
				throw new \Aimeos\MShop\Coupon\Exception( sprintf( 'Invalid characters in class name "%1$s"', $name ) );
112
			}
113
114
			$classname = $classprefix . $name;
115
116
			if( class_exists( $classname ) === false ) {
117
				throw new \Aimeos\MShop\Coupon\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
118
			}
119
120
			$provider = new $classname( $provider, $this->getContext(), $item, $code );
121
122
			if( ( $provider instanceof $iface ) === false ) {
123
				throw new \Aimeos\MShop\Coupon\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $iface ) );
124
			}
125
		}
126
127
		return $provider;
128
	}
129
}