Factory.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Price Info factory
  8. */
  9. namespace Magento\Framework\Pricing\PriceInfo;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\Pricing\SaleableInterface;
  12. /**
  13. * Price info model factory
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Factory
  19. {
  20. /**
  21. * List of Price Info classes by product types
  22. *
  23. * @var array
  24. */
  25. protected $types = [];
  26. /**
  27. * Object Manager
  28. *
  29. * @var ObjectManagerInterface
  30. */
  31. protected $objectManager;
  32. /**
  33. * Construct
  34. *
  35. * @param array $types
  36. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  37. */
  38. public function __construct(
  39. array $types,
  40. ObjectManagerInterface $objectManager
  41. ) {
  42. $this->types = $types;
  43. $this->objectManager = $objectManager;
  44. }
  45. /**
  46. * Create Price Info object for particular product
  47. *
  48. * @param SaleableInterface $saleableItem
  49. * @param array $arguments
  50. * @return \Magento\Framework\Pricing\PriceInfoInterface
  51. * @throws \InvalidArgumentException
  52. */
  53. public function create(SaleableInterface $saleableItem, array $arguments = [])
  54. {
  55. $type = $saleableItem->getTypeId();
  56. if (isset($this->types[$type]['infoClass'])) {
  57. $priceInfo = $this->types[$type]['infoClass'];
  58. } else {
  59. $priceInfo = $this->types['default']['infoClass'];
  60. }
  61. if (isset($this->types[$type]['prices'])) {
  62. $priceCollection = $this->types[$type]['prices'];
  63. } else {
  64. $priceCollection = $this->types['default']['prices'];
  65. }
  66. $arguments['saleableItem'] = $saleableItem;
  67. $quantity = $saleableItem->getQty();
  68. if ($quantity) {
  69. $arguments['quantity'] = $quantity;
  70. }
  71. $arguments['prices'] = $this->objectManager->create(
  72. $priceCollection,
  73. [
  74. 'saleableItem' => $arguments['saleableItem'],
  75. 'quantity' => $quantity
  76. ]
  77. );
  78. return $this->objectManager->create($priceInfo, $arguments);
  79. }
  80. }