PriceComposite.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing;
  7. use Magento\Framework\Pricing\Price\Factory as PriceFactory;
  8. use Magento\Framework\Pricing\Price\PriceInterface;
  9. /**
  10. * Composite price model
  11. */
  12. class PriceComposite
  13. {
  14. /**
  15. * @var PriceFactory
  16. */
  17. protected $priceFactory;
  18. /**
  19. * @var array
  20. */
  21. protected $metadata;
  22. /**
  23. * @param PriceFactory $priceFactory
  24. * @param array $metadata
  25. */
  26. public function __construct(PriceFactory $priceFactory, array $metadata = [])
  27. {
  28. $this->priceFactory = $priceFactory;
  29. $this->metadata = $metadata;
  30. }
  31. /**
  32. * @return array
  33. */
  34. public function getPriceCodes()
  35. {
  36. return array_keys($this->metadata);
  37. }
  38. /**
  39. * Returns metadata for prices
  40. *
  41. * @return array
  42. */
  43. public function getMetadata()
  44. {
  45. return $this->metadata;
  46. }
  47. /**
  48. * @param SaleableInterface $salableItem
  49. * @param string $priceCode
  50. * @param float $quantity
  51. * @return PriceInterface
  52. * @throws \InvalidArgumentException
  53. */
  54. public function createPriceObject(SaleableInterface $salableItem, $priceCode, $quantity)
  55. {
  56. if (!isset($this->metadata[$priceCode])) {
  57. throw new \InvalidArgumentException($priceCode . ' is not registered in prices list');
  58. }
  59. $className = $this->metadata[$priceCode]['class'];
  60. return $this->priceFactory->create($salableItem, $className, $quantity);
  61. }
  62. }