Completed
Push — master ( dc433d...4234c3 )
by Aimeos
08:12
created

ServicesUpdate::update()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 60
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 30
c 1
b 0
f 0
nc 11
nop 3
dl 0
loc 60
rs 7.0677

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, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2016
7
 * @package MShop
8
 * @subpackage Plugin
9
 */
10
11
12
namespace Aimeos\MShop\Plugin\Provider\Order;
13
14
15
/**
16
 * Updates service items on basket change.
17
 *
18
 * @package MShop
19
 * @subpackage Plugin
20
 */
21
class ServicesUpdate
22
	extends \Aimeos\MShop\Plugin\Provider\Factory\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Plugin\Provider\Factory\Iface
24
{
25
	/**
26
	 * Subscribes itself to a publisher
27
	 *
28
	 * @param \Aimeos\MW\Observer\Publisher\Iface $p Object implementing publisher interface
29
	 */
30
	public function register( \Aimeos\MW\Observer\Publisher\Iface $p )
31
	{
32
		$p->addListener( $this->getObject(), 'setAddress.after' );
33
		$p->addListener( $this->getObject(), 'deleteAddress.after' );
34
		$p->addListener( $this->getObject(), 'addProduct.after' );
35
		$p->addListener( $this->getObject(), 'deleteProduct.after' );
36
		$p->addListener( $this->getObject(), 'addCoupon.after' );
37
		$p->addListener( $this->getObject(), 'deleteCoupon.after' );
38
	}
39
40
41
	/**
42
	 * Receives a notification from a publisher object
43
	 *
44
	 * @param \Aimeos\MW\Observer\Publisher\Iface $order Shop basket instance implementing publisher interface
45
	 * @param string $action Name of the action to listen for
46
	 * @param mixed $value Object or value changed in publisher
47
	 * @throws \Aimeos\MShop\Plugin\Provider\Exception if checks fail
48
	 * @return bool true if checks succeed
49
	 */
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