Passed
Push — master ( a8f9a6...baca7c )
by Aimeos
04:57
created

Base   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 26
c 1
b 0
f 0
dl 0
loc 86
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLowestPrice() 0 17 3
A calcLowestPrice() 0 13 4
A getPriceList() 0 19 6
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2022
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\ListsRef\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->context()->translate( 'mshop', 'Price item not available' );
44
			throw new \Aimeos\MShop\Price\Exception( $msg );
45
		}
46
47
		if( $price->getQuantity() > $quantity )
48
		{
49
			$msg = $this->context()->translate( 'mshop', 'Price for the given quantity "%1$s" not available' );
50
			throw new \Aimeos\MShop\Price\Exception( sprintf( $msg, $quantity ) );
51
		}
52
53
		return $this->call( 'calcLowestPrice', $priceList, $quantity );
54
	}
55
56
57
	/**
58
	 * Returns the lowest price for the given quantity
59
	 *
60
	 * @param \Aimeos\Map Associative list of quantity as keys and price item as value
0 ignored issues
show
Bug introduced by
The type Aimeos\MShop\Price\Manager\Associative was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
	 * @param float $quantity Number of products
62
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with the lowest price
63
	 */
64
	protected function calcLowestPrice( \Aimeos\Map $priceList, float $quantity ) : \Aimeos\MShop\Price\Item\Iface
65
	{
66
		$price = $priceList->first();
67
68
		foreach( $priceList as $qty => $priceItem )
69
		{
70
			// add $priceItem->getValue() < $price->getValue() to use lowest price regardless of quantity
71
			if( $quantity >= $qty && $price->getQuantity() < $qty ) {
72
				$price = $priceItem;
73
			}
74
		}
75
76
		return $price;
77
	}
78
79
80
	/**
81
	 * Returns the price items sorted by quantity
82
	 *
83
	 * @param \Aimeos\Map $priceItems List of price items implementing \Aimeos\MShop\Price\Item\Iface
84
	 * @param string|null $currencyId Three letter ISO currency code or null for all
85
	 * @return \Aimeos\Map Associative list of quantity as keys and price item as value
86
	 * @throws \Aimeos\MShop\Price\Exception If an object is no price item
87
	 */
88
	protected function getPriceList( \Aimeos\Map $priceItems, ?string $currencyId ) : \Aimeos\Map
89
	{
90
		$list = map();
91
		$priceItems->implements( \Aimeos\MShop\Price\Item\Iface::class, true );
92
93
		foreach( $priceItems as $priceItem )
94
		{
95
			if( $currencyId !== null && $currencyId !== $priceItem->getCurrencyId() ) {
96
				continue;
97
			}
98
99
			$qty = (string) $priceItem->getQuantity();
100
101
			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

101
			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...
102
				$list[$qty] = $priceItem;
103
			}
104
		}
105
106
		return $list->ksort();
107
	}
108
}
109