Pool.php 2.8 KB

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