AmountFactory.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Amount;
  7. /**
  8. * Class AmountFactory
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class AmountFactory
  14. {
  15. /**
  16. * Default amount class
  17. */
  18. const DEFAULT_PRICE_AMOUNT_CLASS = \Magento\Framework\Pricing\Amount\AmountInterface::class;
  19. /**
  20. * @var \Magento\Framework\ObjectManagerInterface
  21. */
  22. protected $objectManager;
  23. /**
  24. * Constructor
  25. *
  26. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  27. */
  28. public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
  29. {
  30. $this->objectManager = $objectManager;
  31. }
  32. /**
  33. * Create Amount object
  34. *
  35. * @param float $amount
  36. * @param array $adjustmentAmounts
  37. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  38. * @throws \InvalidArgumentException
  39. */
  40. public function create($amount, array $adjustmentAmounts = [])
  41. {
  42. $amountModel = $this->objectManager->create(
  43. self::DEFAULT_PRICE_AMOUNT_CLASS,
  44. [
  45. 'amount' => $amount,
  46. 'adjustmentAmounts' => $adjustmentAmounts
  47. ]
  48. );
  49. if (!$amountModel instanceof \Magento\Framework\Pricing\Amount\AmountInterface) {
  50. throw new \InvalidArgumentException(
  51. get_class($amountModel) . ' doesn\'t implement \Magento\Framework\Pricing\Amount\AmountInterface'
  52. );
  53. }
  54. return $amountModel;
  55. }
  56. }