Conditions | 6 |
Paths | 6 |
Total Lines | 84 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | public function getProvider( \Aimeos\MShop\Service\Item\Iface $item ) |
||
32 | { |
||
33 | $type = ucwords( $item->getType() ); |
||
34 | $names = explode( ',', $item->getProvider() ); |
||
35 | |||
36 | if( ctype_alnum( $type ) === false ) { |
||
37 | throw new \Aimeos\MShop\Service\Exception( sprintf( 'Invalid characters in type name "%1$s"', $type ) ); |
||
38 | } |
||
39 | |||
40 | if( ( $provider = array_shift( $names ) ) === null ) { |
||
41 | throw new \Aimeos\MShop\Service\Exception( sprintf( 'Provider in "%1$s" not available', $item->getProvider() ) ); |
||
42 | } |
||
43 | |||
44 | if( ctype_alnum( $provider ) === false ) { |
||
45 | throw new \Aimeos\MShop\Service\Exception( sprintf( 'Invalid characters in provider name "%1$s"', $provider ) ); |
||
46 | } |
||
47 | |||
48 | $interface = '\\Aimeos\\MShop\\Service\\Provider\\Factory\\Iface'; |
||
49 | $classname = '\\Aimeos\\MShop\\Service\\Provider\\' . $type . '\\' . $provider; |
||
50 | |||
51 | if( class_exists( $classname ) === false ) { |
||
52 | throw new \Aimeos\MShop\Service\Exception( sprintf( 'Class "%1$s" not available', $classname ) ); |
||
53 | } |
||
54 | |||
55 | $context = $this->getContext(); |
||
56 | $config = $context->getConfig(); |
||
57 | $provider = new $classname( $context, $item ); |
||
58 | |||
59 | if( ( $provider instanceof $interface ) === false ) |
||
60 | { |
||
61 | $msg = sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $interface ); |
||
62 | throw new \Aimeos\MShop\Service\Exception( $msg ); |
||
63 | } |
||
64 | |||
65 | /** mshop/service/provider/delivery/decorators |
||
66 | * Adds a list of decorators to all delivery provider objects automatcally |
||
67 | * |
||
68 | * Decorators extend the functionality of a class by adding new aspects |
||
69 | * (e.g. log what is currently done), executing the methods of the underlying |
||
70 | * class only in certain conditions (e.g. only for logged in users) or |
||
71 | * modify what is returned to the caller. |
||
72 | * |
||
73 | * This option allows you to wrap decorators |
||
74 | * ("\Aimeos\MShop\Service\Provider\Decorator\*") around the delivery provider. |
||
75 | * |
||
76 | * mshop/service/provider/delivery/decorators = array( 'decorator1' ) |
||
77 | * |
||
78 | * This would add the decorator named "decorator1" defined by |
||
79 | * "\Aimeos\MShop\Service\Provider\Decorator\Decorator1" to all delivery provider |
||
80 | * objects. |
||
81 | * |
||
82 | * @param array List of decorator names |
||
83 | * @since 2014.03 |
||
84 | * @category Developer |
||
85 | * @see mshop/service/provider/payment/decorators |
||
86 | */ |
||
87 | |||
88 | /** mshop/service/provider/payment/decorators |
||
89 | * Adds a list of decorators to all payment provider objects automatcally |
||
90 | * |
||
91 | * Decorators extend the functionality of a class by adding new aspects |
||
92 | * (e.g. log what is currently done), executing the methods of the underlying |
||
93 | * class only in certain conditions (e.g. only for logged in users) or |
||
94 | * modify what is returned to the caller. |
||
95 | * |
||
96 | * This option allows you to wrap decorators |
||
97 | * ("\Aimeos\MShop\Service\Provider\Decorator\*") around the payment provider. |
||
98 | * |
||
99 | * mshop/service/provider/payment/decorators = array( 'decorator1' ) |
||
100 | * |
||
101 | * This would add the decorator named "decorator1" defined by |
||
102 | * "\Aimeos\MShop\Service\Provider\Decorator\Decorator1" to all payment provider |
||
103 | * objects. |
||
104 | * |
||
105 | * @param array List of decorator names |
||
106 | * @since 2014.03 |
||
107 | * @category Developer |
||
108 | * @see mshop/service/provider/delivery/decorators |
||
109 | */ |
||
110 | $decorators = $config->get( 'mshop/service/provider/' . $item->getType() . '/decorators', [] ); |
||
111 | |||
112 | $provider = $this->addServiceDecorators( $item, $provider, $names ); |
||
113 | return $this->addServiceDecorators( $item, $provider, $decorators ); |
||
114 | } |
||
115 | |||
153 | } |