DownloadableOptions.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\DownloadableGraphQl\Model\Resolver\Product;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
  10. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  11. use Magento\Catalog\Model\Product;
  12. use Magento\Downloadable\Helper\Data as DownloadableHelper;
  13. use Magento\Downloadable\Model\Product\Type as Downloadable;
  14. use Magento\Downloadable\Model\ResourceModel\Link\Collection as LinkCollection;
  15. use Magento\Downloadable\Model\ResourceModel\Sample\Collection as SampleCollection;
  16. use Magento\Framework\Data\Collection;
  17. use Magento\Framework\GraphQl\Config\Element\Field;
  18. use Magento\Framework\GraphQl\Query\EnumLookup;
  19. use Magento\Framework\GraphQl\Query\ResolverInterface;
  20. /**
  21. * @inheritdoc
  22. *
  23. * Format for downloadable product types
  24. */
  25. class DownloadableOptions implements ResolverInterface
  26. {
  27. /**
  28. * @var EnumLookup
  29. */
  30. private $enumLookup;
  31. /**
  32. * @var DownloadableHelper
  33. */
  34. private $downloadableHelper;
  35. /**
  36. * @var SampleCollection
  37. */
  38. private $sampleCollection;
  39. /**
  40. * @var LinkCollection
  41. */
  42. private $linkCollection;
  43. /**
  44. * @param EnumLookup $enumLookup
  45. * @param DownloadableHelper $downloadableHelper
  46. * @param SampleCollection $sampleCollection
  47. * @param LinkCollection $linkCollection
  48. */
  49. public function __construct(
  50. EnumLookup $enumLookup,
  51. DownloadableHelper $downloadableHelper,
  52. SampleCollection $sampleCollection,
  53. LinkCollection $linkCollection
  54. ) {
  55. $this->enumLookup = $enumLookup;
  56. $this->downloadableHelper = $downloadableHelper;
  57. $this->sampleCollection = $sampleCollection;
  58. $this->linkCollection = $linkCollection;
  59. }
  60. /**
  61. * @inheritdoc
  62. *
  63. * Add downloadable options to configurable types
  64. *
  65. * @param \Magento\Framework\GraphQl\Config\Element\Field $field
  66. * @param ContextInterface $context
  67. * @param ResolveInfo $info
  68. * @param array|null $value
  69. * @param array|null $args
  70. * @throws \Exception
  71. * @return null|array
  72. */
  73. public function resolve(
  74. Field $field,
  75. $context,
  76. ResolveInfo $info,
  77. array $value = null,
  78. array $args = null
  79. ) {
  80. if (!isset($value['model'])) {
  81. throw new LocalizedException(__('"model" value should be specified'));
  82. }
  83. /** @var Product $product */
  84. $product = $value['model'];
  85. $data = null;
  86. if ($product->getTypeId() === Downloadable::TYPE_DOWNLOADABLE) {
  87. if ($field->getName() === 'downloadable_product_links') {
  88. $links = $this->linkCollection->addTitleToResult($product->getStoreId())
  89. ->addPriceToResult($product->getStore()->getWebsiteId())
  90. ->addProductToFilter($product->getId());
  91. $data = $this->formatLinks(
  92. $links
  93. );
  94. } elseif ($field->getName() === 'downloadable_product_samples') {
  95. $samples = $this->sampleCollection->addTitleToResult($product->getStoreId())
  96. ->addProductToFilter($product->getId());
  97. $data = $this->formatSamples(
  98. $samples
  99. );
  100. }
  101. }
  102. return $data;
  103. }
  104. /**
  105. * Format links from collection as array
  106. *
  107. * @param LinkCollection $links
  108. * @return array
  109. */
  110. private function formatLinks(LinkCollection $links) : array
  111. {
  112. $resultData = [];
  113. foreach ($links as $linkKey => $link) {
  114. /** @var \Magento\Downloadable\Model\Link $link */
  115. $resultData[$linkKey]['id'] = $link->getId();
  116. $resultData[$linkKey]['title'] = $link->getTitle();
  117. $resultData[$linkKey]['sort_order'] = $link->getSortOrder();
  118. $resultData[$linkKey]['is_shareable'] = $this->downloadableHelper->getIsShareable($link);
  119. $resultData[$linkKey]['price'] = $link->getPrice();
  120. $resultData[$linkKey]['number_of_downloads'] = $link->getNumberOfDownloads();
  121. $sampleType = $link->getSampleType();
  122. $linkType = $link->getLinkType();
  123. if ($linkType !== null) {
  124. $resultData[$linkKey]['link_type']
  125. = $this->enumLookup->getEnumValueFromField('DownloadableFileTypeEnum', $linkType);
  126. }
  127. if ($sampleType !== null) {
  128. $resultData[$linkKey]['sample_type']
  129. = $this->enumLookup->getEnumValueFromField('DownloadableFileTypeEnum', $sampleType);
  130. }
  131. $resultData[$linkKey]['sample_file'] = $link->getSampleFile();
  132. $resultData[$linkKey]['sample_url'] = $link->getSampleUrl();
  133. }
  134. return $resultData;
  135. }
  136. /**
  137. * Format links from collection as array
  138. *
  139. * @param Collection $samples
  140. * @return array
  141. */
  142. private function formatSamples(Collection $samples) : array
  143. {
  144. $resultData = [];
  145. foreach ($samples as $sampleKey => $sample) {
  146. /** @var \Magento\Downloadable\Model\Sample $sample */
  147. $resultData[$sampleKey]['id'] = $sample->getId();
  148. $resultData[$sampleKey]['title'] = $sample->getTitle();
  149. $resultData[$sampleKey]['sort_order'] = $sample->getSortOrder();
  150. $resultData[$sampleKey]['sample_type']
  151. = $this->enumLookup->getEnumValueFromField('DownloadableFileTypeEnum', $sample->getSampleType());
  152. $resultData[$sampleKey]['sample_file'] = $sample->getSampleFile();
  153. $resultData[$sampleKey]['sample_url'] = $sample->getSampleUrl();
  154. }
  155. return $resultData;
  156. }
  157. }