Conditions | 8 |
Paths | 11 |
Total Lines | 60 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
50 | public function update( \Aimeos\MW\Observer\Publisher\Iface $order, $action, $value = null ) |
||
51 | { |
||
52 | if( !( $order instanceof \Aimeos\MShop\Order\Item\Base\Iface ) ) |
||
53 | { |
||
54 | $msg = $this->getContext()->getI18n()->dt( 'mshop', 'Object is not of required type "%1$s"' ); |
||
55 | throw new \Aimeos\MShop\Plugin\Exception( sprintf( $msg, '\Aimeos\MShop\Order\Item\Base\Iface' ) ); |
||
56 | } |
||
57 | |||
58 | $ids = []; |
||
59 | $context = $this->getContext(); |
||
60 | $services = $order->getServices(); |
||
61 | |||
62 | |||
63 | if( count( $order->getProducts() ) === 0 ) |
||
64 | { |
||
65 | $priceManager = \Aimeos\MShop\Factory::createManager( $context, 'price' ); |
||
66 | |||
67 | foreach( $services as $type => $service ) { |
||
68 | $service->setPrice( $priceManager->createItem() ); |
||
69 | } |
||
70 | |||
71 | return true; |
||
72 | } |
||
73 | |||
74 | |||
75 | foreach( $services as $type => $service ) { |
||
76 | $ids[$type] = $service->getServiceId(); |
||
77 | } |
||
78 | |||
79 | $serviceManager = \Aimeos\MShop\Factory::createManager( $context, 'service' ); |
||
80 | |||
81 | $search = $serviceManager->createSearch( true ); |
||
82 | $expr = array( |
||
83 | $search->compare( '==', 'service.id', $ids ), |
||
84 | $search->getConditions(), |
||
85 | ); |
||
86 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
87 | |||
88 | $result = $serviceManager->searchItems( $search, array( 'price' ) ); |
||
89 | |||
90 | |||
91 | foreach( $services as $type => $service ) |
||
92 | { |
||
93 | if( isset( $result[$service->getServiceId()] ) ) |
||
94 | { |
||
95 | $provider = $serviceManager->getProvider( $result[$service->getServiceId()] ); |
||
96 | |||
97 | if( $provider->isAvailable( $order ) ) |
||
98 | { |
||
99 | $service->setPrice( $provider->calcPrice( $order ) ); |
||
100 | $order->setService( $service, $type ); |
||
101 | continue; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | $order->deleteService( $type ); |
||
106 | } |
||
107 | |||
108 | return true; |
||
109 | } |
||
110 | } |
||
111 |