Composite.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue;
  8. use Magento\Catalog\Model\Product\Option;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  11. use Magento\Framework\ObjectManagerInterface;
  12. use Magento\Quote\Model\Quote\Item as QuoteItem;
  13. use Magento\Quote\Model\Quote\Item\Option as SelectedOption;
  14. use Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValueInterface;
  15. /**
  16. * @inheritdoc
  17. */
  18. class Composite implements CustomizableOptionValueInterface
  19. {
  20. /**
  21. * @var ObjectManagerInterface
  22. */
  23. private $objectManager;
  24. /**
  25. * @var CustomizableOptionValueInterface[]
  26. */
  27. private $customizableOptionValues;
  28. /**
  29. * @param ObjectManagerInterface $objectManager
  30. * @param CustomizableOptionValueInterface[] $customizableOptionValues
  31. */
  32. public function __construct(
  33. ObjectManagerInterface $objectManager,
  34. array $customizableOptionValues = []
  35. ) {
  36. $this->objectManager = $objectManager;
  37. $this->customizableOptionValues = $customizableOptionValues;
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function getData(
  43. QuoteItem $cartItem,
  44. Option $option,
  45. SelectedOption $selectedOption
  46. ): array {
  47. $optionType = $option->getType();
  48. if (!array_key_exists($optionType, $this->customizableOptionValues)) {
  49. throw new GraphQlInputException(__('Option type "%1" is not supported', $optionType));
  50. }
  51. $customizableOptionValueClassName = $this->customizableOptionValues[$optionType];
  52. $customizableOptionValue = $this->objectManager->get($customizableOptionValueClassName);
  53. if (!$customizableOptionValue instanceof CustomizableOptionValueInterface) {
  54. throw new LocalizedException(
  55. __('%1 doesn\'t implement CustomizableOptionValueInterface', $customizableOptionValueClassName)
  56. );
  57. }
  58. return $customizableOptionValue->getData($cartItem, $option, $selectedOption);
  59. }
  60. }