1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Pricing\Adjustment;
/**
* Adjustment collection model
*
* @api
* @since 100.0.2
*/
class Collection
{
/**
* @var Pool
*/
protected $adjustmentPool;
/**
* @var string[]
*/
protected $adjustments;
/**
* @var AdjustmentInterface[]
*/
protected $adjustmentInstances;
/**
* @param Pool $adjustmentPool
* @param string[] $adjustments
*/
public function __construct(
Pool $adjustmentPool,
array $adjustments
) {
$this->adjustmentPool = $adjustmentPool;
$this->adjustments = $adjustments;
}
/**
* @return AdjustmentInterface[]
*/
public function getItems()
{
if ($this->adjustmentInstances === null) {
$this->adjustmentInstances = $this->fetchAdjustments($this->adjustments);
}
return $this->adjustmentInstances;
}
/**
* Get adjustment by code
*
* @param string $adjustmentCode
* @throws \InvalidArgumentException
* @return AdjustmentInterface
*/
public function getItemByCode($adjustmentCode)
{
if ($this->adjustmentInstances === null) {
$this->adjustmentInstances = $this->fetchAdjustments($this->adjustments);
}
if (!isset($this->adjustmentInstances[$adjustmentCode])) {
throw new \InvalidArgumentException(sprintf('Price adjustment "%s" is not found', $adjustmentCode));
}
return $this->adjustmentInstances[$adjustmentCode];
}
/**
* @param string[] $adjustments
* @return AdjustmentInterface[]
*/
protected function fetchAdjustments($adjustments)
{
$instances = [];
foreach ($adjustments as $code) {
$instances[$code] = $this->adjustmentPool->getAdjustmentByCode($code);
}
uasort($instances, [$this, 'sortAdjustments']);
return $instances;
}
/**
* Sort adjustments
*
* @param AdjustmentInterface $firstAdjustment
* @param AdjustmentInterface $secondAdjustment
* @return int
*/
protected function sortAdjustments(AdjustmentInterface $firstAdjustment, AdjustmentInterface $secondAdjustment)
{
if ($firstAdjustment->getSortOrder() === \Magento\Framework\Pricing\Adjustment\Pool::DEFAULT_SORT_ORDER) {
return 1;
}
return $firstAdjustment->getSortOrder() > $secondAdjustment->getSortOrder() ? 1 : -1;
}
}