Completed
Push — master ( 1fc904...dc433d )
by Aimeos
07:56
created

ServicesUpdate::update()   C

Complexity

Conditions 9
Paths 12

Size

Total Lines 67
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 35
nc 12
nop 3
dl 0
loc 67
rs 6.3448
c 1
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, 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
	private $singleton = false;
26
27
28
	/**
29
	 * Subscribes itself to a publisher
30
	 *
31
	 * @param \Aimeos\MW\Observer\Publisher\Iface $p Object implementing publisher interface
32
	 */
33
	public function register( \Aimeos\MW\Observer\Publisher\Iface $p )
34
	{
35
		$p->addListener( $this->getObject(), 'setAddress.after' );
36
		$p->addListener( $this->getObject(), 'deleteAddress.after' );
37
		$p->addListener( $this->getObject(), 'addProduct.after' );
38
		$p->addListener( $this->getObject(), 'deleteProduct.after' );
39
		$p->addListener( $this->getObject(), 'addCoupon.after' );
40
		$p->addListener( $this->getObject(), 'deleteCoupon.after' );
41
	}
42
43
44
	/**
45
	 * Receives a notification from a publisher object
46
	 *
47
	 * @param \Aimeos\MW\Observer\Publisher\Iface $order Shop basket instance implementing publisher interface
48
	 * @param string $action Name of the action to listen for
49
	 * @param mixed $value Object or value changed in publisher
50
	 * @throws \Aimeos\MShop\Plugin\Provider\Exception if checks fail
51
	 * @return bool true if checks succeed
52
	 */
53
	public function update( \Aimeos\MW\Observer\Publisher\Iface $order, $action, $value = null )
54
	{
55
		if( $this->singleton === true ) {
56
			return true;
57
		}
58
		$this->singleton = true;
59
60
		if( !( $order instanceof \Aimeos\MShop\Order\Item\Base\Iface ) )
61
		{
62
			$msg = $this->getContext()->getI18n()->dt( 'mshop', 'Object is not of required type "%1$s"' );
63
			throw new \Aimeos\MShop\Plugin\Exception( sprintf( $msg, '\Aimeos\MShop\Order\Item\Base\Iface' ) );
64
		}
65
66
		$ids = [];
67
		$context = $this->getContext();
68
		$services = $order->getServices();
69
70
71
		if( count( $order->getProducts() ) === 0 )
72
		{
73
			$priceManager = \Aimeos\MShop\Factory::createManager( $context, 'price' );
74
75
			foreach( $services as $type => $service ) {
76
				$service->setPrice( $priceManager->createItem() );
77
			}
78
79
			$this->singleton = false;
80
			return true;
81
		}
82
83
84
		foreach( $services as $type => $service ) {
85
			$ids[$type] = $service->getServiceId();
86
		}
87
88
		$serviceManager = \Aimeos\MShop\Factory::createManager( $context, 'service' );
89
90
		$search = $serviceManager->createSearch( true );
91
		$expr = array(
92
			$search->compare( '==', 'service.id', $ids ),
93
			$search->getConditions(),
94
		);
95
		$search->setConditions( $search->combine( '&&', $expr ) );
96
97
		$result = $serviceManager->searchItems( $search, array( 'price' ) );
98
99
100
		foreach( $services as $type => $service )
101
		{
102
			if( isset( $result[$service->getServiceId()] ) )
103
			{
104
				$provider = $serviceManager->getProvider( $result[$service->getServiceId()] );
105
106
				if( $provider->isAvailable( $order ) )
107
				{
108
					$service->setPrice( $provider->calcPrice( $order ) );
109
					$order->setService( $service, $type );
110
					continue;
111
				}
112
			}
113
114
			$order->deleteService( $type );
115
		}
116
117
		$this->singleton = false;
118
		return true;
119
	}
120
}
121