Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DownloadableOptions.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 use Magento\Downloadable\Helper\Data as DownloadableHelper;
14 use Magento\Downloadable\Model\Product\Type as Downloadable;
21 
28 {
32  private $enumLookup;
33 
37  private $downloadableHelper;
38 
42  private $sampleCollection;
43 
47  private $linkCollection;
48 
55  public function __construct(
56  EnumLookup $enumLookup,
57  DownloadableHelper $downloadableHelper,
58  SampleCollection $sampleCollection,
59  LinkCollection $linkCollection
60  ) {
61  $this->enumLookup = $enumLookup;
62  $this->downloadableHelper = $downloadableHelper;
63  $this->sampleCollection = $sampleCollection;
64  $this->linkCollection = $linkCollection;
65  }
66 
72  public function resolve(
73  Field $field,
74  $context,
76  array $value = null,
77  array $args = null
78  ) {
79  if (!isset($value['model'])) {
80  throw new GraphQlInputException(__('"model" value should be specified'));
81  }
82 
84  $product = $value['model'];
85 
86  $data = null;
87  if ($product->getTypeId() === Downloadable::TYPE_DOWNLOADABLE) {
88  if ($field->getName() === 'downloadable_product_links') {
89  $links = $this->linkCollection->addTitleToResult($product->getStoreId())
90  ->addPriceToResult($product->getStore()->getWebsiteId())
91  ->addProductToFilter($product->getId());
92  $data = $this->formatLinks(
93  $links
94  );
95  } elseif ($field->getName() === 'downloadable_product_samples') {
96  $samples = $this->sampleCollection->addTitleToResult($product->getStoreId())
97  ->addProductToFilter($product->getId());
98  $data = $this->formatSamples(
99  $samples
100  );
101  }
102  }
103 
104  return $data;
105  }
106 
113  private function formatLinks(LinkCollection $links) : array
114  {
115  $resultData = [];
116  foreach ($links as $linkKey => $link) {
118  $resultData[$linkKey]['id'] = $link->getId();
119  $resultData[$linkKey]['title'] = $link->getTitle();
120  $resultData[$linkKey]['sort_order'] = $link->getSortOrder();
121  $resultData[$linkKey]['is_shareable'] = $this->downloadableHelper->getIsShareable($link);
122  $resultData[$linkKey]['price'] = $link->getPrice();
123  $resultData[$linkKey]['number_of_downloads'] = $link->getNumberOfDownloads();
124  $sampleType = $link->getSampleType();
125  $linkType = $link->getLinkType();
126 
127  if ($linkType !== null) {
128  $resultData[$linkKey]['link_type']
129  = $this->enumLookup->getEnumValueFromField('DownloadableFileTypeEnum', $linkType);
130  }
131 
132  if ($sampleType !== null) {
133  $resultData[$linkKey]['sample_type']
134  = $this->enumLookup->getEnumValueFromField('DownloadableFileTypeEnum', $sampleType);
135  }
136 
137  $resultData[$linkKey]['sample_file'] = $link->getSampleFile();
138  $resultData[$linkKey]['sample_url'] = $link->getSampleUrl();
139  }
140  return $resultData;
141  }
142 
149  private function formatSamples(Collection $samples) : array
150  {
151  $resultData = [];
152  foreach ($samples as $sampleKey => $sample) {
154  $resultData[$sampleKey]['id'] = $sample->getId();
155  $resultData[$sampleKey]['title'] = $sample->getTitle();
156  $resultData[$sampleKey]['sort_order'] = $sample->getSortOrder();
157  $resultData[$sampleKey]['sample_type']
158  = $this->enumLookup->getEnumValueFromField('DownloadableFileTypeEnum', $sample->getSampleType());
159  $resultData[$sampleKey]['sample_file'] = $sample->getSampleFile();
160  $resultData[$sampleKey]['sample_url'] = $sample->getSampleUrl();
161  }
162  return $resultData;
163  }
164 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
__construct(EnumLookup $enumLookup, DownloadableHelper $downloadableHelper, SampleCollection $sampleCollection, LinkCollection $linkCollection)
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
resolve(Field $field, $context, ResolveInfo $info, array $value=null, array $args=null)