ItemPool.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\CustomerData;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Magento\Quote\Model\Quote\Item;
  10. /**
  11. * Item pool
  12. */
  13. class ItemPool implements ItemPoolInterface
  14. {
  15. /**
  16. * Object Manager
  17. *
  18. * @var ObjectManagerInterface
  19. */
  20. protected $objectManager;
  21. /**
  22. * Default item id
  23. *
  24. * @var string
  25. */
  26. protected $defaultItemId;
  27. /**
  28. * Item map. Key is item type, value is item object id in di
  29. *
  30. * @var array
  31. */
  32. protected $itemMap;
  33. /**
  34. * Construct
  35. *
  36. * @param ObjectManagerInterface $objectManager
  37. * @param string $defaultItemId
  38. * @param array $itemMap
  39. * @codeCoverageIgnore
  40. */
  41. public function __construct(
  42. ObjectManagerInterface $objectManager,
  43. $defaultItemId,
  44. array $itemMap = []
  45. ) {
  46. $this->objectManager = $objectManager;
  47. $this->defaultItemId = $defaultItemId;
  48. $this->itemMap = $itemMap;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. * @codeCoverageIgnore
  53. */
  54. public function getItemData(Item $item)
  55. {
  56. return $this->get($item->getProductType())->getItemData($item);
  57. }
  58. /**
  59. * Get section source by name
  60. *
  61. * @param string $type
  62. * @return ItemInterface
  63. * @throws LocalizedException
  64. */
  65. protected function get($type)
  66. {
  67. $itemId = isset($this->itemMap[$type]) ? $this->itemMap[$type] : $this->defaultItemId;
  68. $item = $this->objectManager->get($itemId);
  69. if (!$item instanceof ItemInterface) {
  70. throw new LocalizedException(
  71. __('%1 doesn\'t extend \Magento\Checkout\CustomerData\ItemInterface', $type)
  72. );
  73. }
  74. return $item;
  75. }
  76. }