BundleItems.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\BundleGraphQl\Model\Resolver;
  8. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  9. use Magento\Bundle\Model\Product\Type;
  10. use Magento\BundleGraphQl\Model\Resolver\Options\Collection;
  11. use Magento\Catalog\Api\Data\ProductInterface;
  12. use Magento\Framework\EntityManager\MetadataPool;
  13. use Magento\Framework\GraphQl\Config\Element\Field;
  14. use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
  15. use Magento\Framework\GraphQl\Query\ResolverInterface;
  16. /**
  17. * @inheritdoc
  18. */
  19. class BundleItems implements ResolverInterface
  20. {
  21. /**
  22. * @var Collection
  23. */
  24. private $bundleOptionCollection;
  25. /**
  26. * @var ValueFactory
  27. */
  28. private $valueFactory;
  29. /**
  30. * @var MetadataPool
  31. */
  32. private $metadataPool;
  33. /**
  34. * @param Collection $bundleOptionCollection
  35. * @param ValueFactory $valueFactory
  36. * @param MetadataPool $metadataPool
  37. */
  38. public function __construct(
  39. Collection $bundleOptionCollection,
  40. ValueFactory $valueFactory,
  41. MetadataPool $metadataPool
  42. ) {
  43. $this->bundleOptionCollection = $bundleOptionCollection;
  44. $this->valueFactory = $valueFactory;
  45. $this->metadataPool = $metadataPool;
  46. }
  47. /**
  48. * Fetch and format bundle option items.
  49. *
  50. * {@inheritDoc}
  51. */
  52. public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
  53. {
  54. $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
  55. if ($value['type_id'] !== Type::TYPE_CODE
  56. || !isset($value[$linkField])
  57. || !isset($value[ProductInterface::SKU])
  58. ) {
  59. $result = function () {
  60. return null;
  61. };
  62. return $this->valueFactory->create($result);
  63. }
  64. $this->bundleOptionCollection->addParentFilterData(
  65. (int)$value[$linkField],
  66. (int)$value['entity_id'],
  67. $value[ProductInterface::SKU]
  68. );
  69. $result = function () use ($value, $linkField) {
  70. return $this->bundleOptionCollection->getOptionsByParentId((int)$value[$linkField]);
  71. };
  72. return $this->valueFactory->create($result);
  73. }
  74. }