Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pool.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\Pricing\Adjustment\Factory as AdjustmentFactory;
10 
17 class Pool
18 {
22  const DEFAULT_SORT_ORDER = -1;
23 
27  protected $adjustmentFactory;
28 
32  protected $adjustments;
33 
38 
43  public function __construct(AdjustmentFactory $adjustmentFactory, $adjustments = [])
44  {
45  $this->adjustmentFactory = $adjustmentFactory;
46  $this->adjustments = $adjustments;
47  }
48 
52  public function getAdjustments()
53  {
54  if (!isset($this->adjustmentInstances)) {
55  $this->adjustmentInstances = $this->createAdjustments(array_keys($this->adjustments));
56  }
58  }
59 
65  public function getAdjustmentByCode($adjustmentCode)
66  {
67  if (!isset($this->adjustmentInstances)) {
68  $this->adjustmentInstances = $this->createAdjustments(array_keys($this->adjustments));
69  }
70  if (!isset($this->adjustmentInstances[$adjustmentCode])) {
71  throw new \InvalidArgumentException(sprintf('Price adjustment "%s" is not registered', $adjustmentCode));
72  }
73  return $this->adjustmentInstances[$adjustmentCode];
74  }
75 
82  protected function createAdjustments($adjustments)
83  {
84  $instances = [];
85  foreach ($adjustments as $code) {
86  if (!isset($instances[$code])) {
87  $instances[$code] = $this->createAdjustment($code);
88  }
89  }
90  return $instances;
91  }
92 
99  protected function createAdjustment($adjustmentCode)
100  {
101  $adjustmentData = $this->adjustments[$adjustmentCode];
102  $sortOrder = isset($adjustmentData['sortOrder']) ? (int)$adjustmentData['sortOrder'] : self::DEFAULT_SORT_ORDER;
103  return $this->adjustmentFactory->create(
104  $adjustmentData['className'],
105  [
106  'sortOrder' => $sortOrder
107  ]
108  );
109  }
110 }
__construct(AdjustmentFactory $adjustmentFactory, $adjustments=[])
Definition: Pool.php:43
getAdjustmentByCode($adjustmentCode)
Definition: Pool.php:65
$code
Definition: info.phtml:12