Passed
Push — master ( 38b577...fe458e )
by Aimeos
07:53
created

Base::getLowestPrice()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 5
nop 3
dl 0
loc 24
rs 8.8333
c 0
b 0
f 0
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-2020
7
 * @package MShop
8
 * @subpackage Price
9
 */
10
11
12
namespace Aimeos\MShop\Price\Manager;
13
14
15
/**
16
 * Abstract class for all price managers with basic methods.
17
 *
18
 * @package MShop
19
 * @subpackage Price
20
 */
21
abstract class Base
22
	extends \Aimeos\MShop\Common\Manager\Base
23
{
24
	use \Aimeos\MShop\Common\Manager\ListRef\Traits;
25
	use \Aimeos\MShop\Common\Manager\PropertyRef\Traits;
26
27
28
	/**
29
	 * Returns the price item with the lowest price for the given quantity.
30
	 *
31
	 * @param \Aimeos\Map $priceItems List of price items implementing \Aimeos\MShop\Price\Item\Iface
32
	 * @param float $quantity Number of products
33
	 * @param string|null $currencyId Three letter ISO currency code or null for all
34
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with the lowest price
35
	 * @throws \Aimeos\MShop\Price\Exception if no price item is available
36
	 */
37
	public function getLowestPrice( \Aimeos\Map $priceItems, float $quantity, string $currencyId = null ) : \Aimeos\MShop\Price\Item\Iface
38
	{
39
		$priceList = $this->getPriceList( $priceItems, $currencyId );
40
41
		if( ( $price = $priceList->first() ) === null )
42
		{
43
			$msg = $this->getContext()->getI18n()->dt( 'mshop', 'Price item not available' );
44
			throw new \Aimeos\MShop\Price\Exception( $msg );
45
		}
46
47
		if( $price->getQuantity() > $quantity )
48
		{
49
			$msg = $this->getContext()->getI18n()->dt( 'mshop', 'Price for the given quantity "%1$s" not available' );
50
			throw new \Aimeos\MShop\Price\Exception( sprintf( $msg, $quantity ) );
51
		}
52
53
		foreach( $priceList as $qty => $priceItem )
54
		{
55
			if( $qty <= $quantity && $qty > $price->getQuantity() && $priceItem->getValue() < $price->getValue() ) {
56
				$price = $priceItem;
57
			}
58
		}
59
60
		return $price;
61
	}
62
63
64
	/**
65
	 * Returns the price items sorted by quantity
66
	 *
67
	 * @param \Aimeos\Map $priceItems List of price items implementing \Aimeos\MShop\Price\Item\Iface
68
	 * @param string|null $currencyId Three letter ISO currency code or null for all
69
	 * @return \Aimeos\Map Associative list of quantity as keys and price item as value
70
	 * @throws \Aimeos\MShop\Price\Exception If an object is no price item
71
	 */
72
	protected function getPriceList( \Aimeos\Map $priceItems, ?string $currencyId ) : \Aimeos\Map
73
	{
74
		$list = map();
75
76
		foreach( $priceItems as $priceItem )
77
		{
78
			self::checkClass( \Aimeos\MShop\Price\Item\Iface::class, $priceItem );
79
80
			if( $currencyId !== null && $currencyId !== $priceItem->getCurrencyId() ) {
81
				continue;
82
			}
83
84
			$qty = (string) $priceItem->getQuantity();
85
86
			if( !isset( $list[$qty] ) || $list[$qty]->getValue() > $priceItem->getValue() ) {
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
			if( !isset( $list[$qty] ) || $list[$qty]->/** @scrutinizer ignore-call */ getValue() > $priceItem->getValue() ) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
				$list[$qty] = $priceItem;
88
			}
89
		}
90
91
		return $list->ksort();
92
	}
93
}
94