MsrpPriceCalculator.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Msrp\Pricing;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. /**
  10. * @inheritdoc
  11. */
  12. class MsrpPriceCalculator implements MsrpPriceCalculatorInterface
  13. {
  14. /**
  15. * @var MsrpPriceCalculatorInterface[]
  16. */
  17. private $msrpPriceCalculators;
  18. /**
  19. * @param array $msrpPriceCalculators
  20. */
  21. public function __construct(array $msrpPriceCalculators)
  22. {
  23. $this->msrpPriceCalculators = $this->getMsrpPriceCalculators($msrpPriceCalculators);
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function getMsrpPriceValue(ProductInterface $product): float
  29. {
  30. $productType = $product->getTypeId();
  31. if (isset($this->msrpPriceCalculators[$productType])) {
  32. $priceCalculator = $this->msrpPriceCalculators[$productType];
  33. $msrp = $priceCalculator->getMsrpPriceValue($product);
  34. } else {
  35. $msrp = (float)$product->getMsrp();
  36. }
  37. return $msrp;
  38. }
  39. /**
  40. * Convert the configuration of MSRP price calculators.
  41. *
  42. * @param array $msrpPriceCalculatorsConfig
  43. * @return array
  44. */
  45. private function getMsrpPriceCalculators(array $msrpPriceCalculatorsConfig): array
  46. {
  47. $msrpPriceCalculators = [];
  48. foreach ($msrpPriceCalculatorsConfig as $msrpPriceCalculator) {
  49. if (isset($msrpPriceCalculator['productType'], $msrpPriceCalculator['priceCalculator'])) {
  50. $msrpPriceCalculators[$msrpPriceCalculator['productType']] =
  51. $msrpPriceCalculator['priceCalculator'];
  52. }
  53. }
  54. return $msrpPriceCalculators;
  55. }
  56. }