Skip to content

Commit 12a77ba

Browse files
committed
MAGETWO-87004: Graphql Downloadable product implementation
- refactoring
1 parent e24b24c commit 12a77ba

File tree

5 files changed

+57
-148
lines changed

5 files changed

+57
-148
lines changed

app/code/Magento/DownloadableGraphQl/Model/Plugin/Model/Resolver/Products/DataProvider/Product/Formatter/DownloadableOptions.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

app/code/Magento/DownloadableGraphQl/Model/Plugin/Model/Resolver/Products/DataProvider/ProductPlugin.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

app/code/Magento/DownloadableGraphQl/Model/Resolver/Products/Query/DownloadableProductPostProcessor.php renamed to app/code/Magento/DownloadableGraphQl/Model/Resolver/Products/DataProvider/Product/Formatter/DownloadableOptions.php

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
namespace Magento\DownloadableGraphQl\Model\Resolver\Products\Query;
7+
namespace Magento\DownloadableGraphQl\Model\Resolver\Products\DataProvider\Product\Formatter;
88

9+
use Magento\Catalog\Model\Product;
910
use Magento\Downloadable\Model\Product\Type as Downloadable;
11+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\FormatterInterface;
1012
use Magento\Framework\Data\Collection;
1113
use Magento\Framework\GraphQl\Query\EnumLookup;
1214
use Magento\Downloadable\Helper\Data as DownloadableHelper;
15+
use Magento\Downloadable\Model\ResourceModel\Sample\Collection as SampleCollection;
16+
use Magento\Downloadable\Model\ResourceModel\Link\Collection as LinkCollection;
1317

1418
/**
15-
* Retrieves simple product data for child products, and formats configurable data
19+
* Post formatting plugin to continue formatting data for downloadable type products
1620
*/
17-
class DownloadableProductPostProcessor implements \Magento\Framework\GraphQl\Query\PostFetchProcessorInterface
21+
class DownloadableOptions implements FormatterInterface
1822
{
1923
/**
2024
* @var EnumLookup
@@ -26,46 +30,65 @@ class DownloadableProductPostProcessor implements \Magento\Framework\GraphQl\Que
2630
*/
2731
private $downloadableHelper;
2832

33+
/**
34+
* @var SampleCollection
35+
*/
36+
private $sampleCollection;
37+
38+
/**
39+
* @var LinkCollection
40+
*/
41+
private $linkCollection;
42+
2943
/**
3044
* @param EnumLookup $enumLookup
45+
* @param DownloadableHelper $downloadableHelper
46+
* @param SampleCollection $sampleCollection
47+
* @param LinkCollection $linkCollection
3148
*/
32-
public function __construct(EnumLookup $enumLookup, DownloadableHelper $downloadableHelper)
33-
{
49+
public function __construct(
50+
EnumLookup $enumLookup,
51+
DownloadableHelper $downloadableHelper,
52+
SampleCollection $sampleCollection,
53+
LinkCollection $linkCollection
54+
) {
3455
$this->enumLookup = $enumLookup;
3556
$this->downloadableHelper = $downloadableHelper;
57+
$this->sampleCollection = $sampleCollection;
58+
$this->linkCollection = $linkCollection;
3659
}
3760

3861
/**
39-
* Process all downloadable product data, including adding simple product data and formatting relevant attributes.
62+
* Add downloadable options and options to configurable types
4063
*
41-
* @param array $resultData
42-
* @return array
64+
* {@inheritdoc}
4365
*/
44-
public function process(array $resultData)
66+
public function format(Product $product, array $productData = [])
4567
{
46-
foreach ($resultData as $productKey => $product) {
47-
if (isset($product['type_id']) && $product['type_id'] === Downloadable::TYPE_DOWNLOADABLE) {
48-
if (isset($product['downloadable_product_samples'])) {
49-
$resultData[$productKey]['downloadable_product_samples']
50-
= $this->formatSamples($product['downloadable_product_samples']);
51-
}
52-
if (isset($product['downloadable_product_links'])) {
53-
$resultData[$productKey]['downloadable_product_links']
54-
= $this->formatLinks($product['downloadable_product_links']);
55-
}
56-
}
68+
if ($product->getTypeId() === Downloadable::TYPE_DOWNLOADABLE) {
69+
$samples = $this->sampleCollection->addTitleToResult($product->getStoreId())
70+
->addProductToFilter($product->getId());
71+
$links = $this->linkCollection->addTitleToResult($product->getStoreId())
72+
->addPriceToResult($product->getStore()->getWebsiteId())
73+
->addProductToFilter($product->getId());
74+
$productData['downloadable_product_links'] = $this->formatLinks(
75+
$links
76+
);
77+
$productData['downloadable_product_samples'] = $this->formatSamples(
78+
$samples
79+
);
5780
}
5881

59-
return $resultData;
82+
return $productData;
6083
}
6184

6285
/**
6386
* Format links from collection as array
6487
*
65-
* @param Collection $links
88+
* @param \Magento\Downloadable\Api\Data\LinkInterface[] $links
6689
* @return array
6790
*/
68-
private function formatLinks(Collection $links)
91+
private function formatLinks($links)
6992
{
7093
$resultData = [];
7194
foreach ($links as $linkKey => $link) {

app/code/Magento/DownloadableGraphQl/etc/di.xml

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/code/Magento/DownloadableGraphQl/etc/graphql/di.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<type name="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\FormatterComposite">
1010
<arguments>
1111
<argument name="formatterInstances" xsi:type="array">
12-
<item name="add_downloadable_data" xsi:type="object">Magento\DownloadableGraphQl\Model\Plugin\Model\Resolver\Products\DataProvider\Product\Formatter\DownloadableOptions</item>
12+
<item name="add_downloadable_data" xsi:type="object">Magento\DownloadableGraphQl\Model\Resolver\Products\DataProvider\Product\Formatter\DownloadableOptions</item>
1313
</argument>
1414
</arguments>
1515
</type>
@@ -20,7 +20,7 @@
2020
</argument>
2121
</arguments>
2222
</type>
23-
<type name="\Magento\Framework\GraphQl\Type\Enum\DefaultDataMapper">
23+
<type name="Magento\Framework\GraphQl\Type\Enum\DefaultDataMapper">
2424
<arguments>
2525
<argument name="map" xsi:type="array">
2626
<item name="DownloadableFileTypeEnum" xsi:type="array">
@@ -30,4 +30,13 @@
3030
</argument>
3131
</arguments>
3232
</type>
33+
<type name="Magento\Framework\GraphQl\Type\Entity\DefaultMapper">
34+
<arguments>
35+
<argument name="map" xsi:type="array">
36+
<item name="catalog_product" xsi:type="array">
37+
<item name="downloadable" xsi:type="string">DownloadableProduct</item>
38+
</item>
39+
</argument>
40+
</arguments>
41+
</type>
3342
</config>

0 commit comments

Comments
 (0)