4
4
* See COPYING.txt for license details.
5
5
*/
6
6
7
- namespace Magento \DownloadableGraphQl \Model \Resolver \Products \Query ;
7
+ namespace Magento \DownloadableGraphQl \Model \Resolver \Products \DataProvider \ Product \ Formatter ;
8
8
9
+ use Magento \Catalog \Model \Product ;
9
10
use Magento \Downloadable \Model \Product \Type as Downloadable ;
11
+ use Magento \CatalogGraphQl \Model \Resolver \Products \DataProvider \Product \FormatterInterface ;
10
12
use Magento \Framework \Data \Collection ;
11
13
use Magento \Framework \GraphQl \Query \EnumLookup ;
12
14
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 ;
13
17
14
18
/**
15
- * Retrieves simple product data for child products, and formats configurable data
19
+ * Post formatting plugin to continue formatting data for downloadable type products
16
20
*/
17
- class DownloadableProductPostProcessor implements \ Magento \ Framework \ GraphQl \ Query \PostFetchProcessorInterface
21
+ class DownloadableOptions implements FormatterInterface
18
22
{
19
23
/**
20
24
* @var EnumLookup
@@ -26,46 +30,65 @@ class DownloadableProductPostProcessor implements \Magento\Framework\GraphQl\Que
26
30
*/
27
31
private $ downloadableHelper ;
28
32
33
+ /**
34
+ * @var SampleCollection
35
+ */
36
+ private $ sampleCollection ;
37
+
38
+ /**
39
+ * @var LinkCollection
40
+ */
41
+ private $ linkCollection ;
42
+
29
43
/**
30
44
* @param EnumLookup $enumLookup
45
+ * @param DownloadableHelper $downloadableHelper
46
+ * @param SampleCollection $sampleCollection
47
+ * @param LinkCollection $linkCollection
31
48
*/
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
+ ) {
34
55
$ this ->enumLookup = $ enumLookup ;
35
56
$ this ->downloadableHelper = $ downloadableHelper ;
57
+ $ this ->sampleCollection = $ sampleCollection ;
58
+ $ this ->linkCollection = $ linkCollection ;
36
59
}
37
60
38
61
/**
39
- * Process all downloadable product data, including adding simple product data and formatting relevant attributes.
62
+ * Add downloadable options and options to configurable types
40
63
*
41
- * @param array $resultData
42
- * @return array
64
+ * {@inheritdoc}
43
65
*/
44
- public function process ( array $ resultData )
66
+ public function format ( Product $ product , array $ productData = [] )
45
67
{
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
+ );
57
80
}
58
81
59
- return $ resultData ;
82
+ return $ productData ;
60
83
}
61
84
62
85
/**
63
86
* Format links from collection as array
64
87
*
65
- * @param Collection $links
88
+ * @param \Magento\Downloadable\Api\Data\LinkInterface[] $links
66
89
* @return array
67
90
*/
68
- private function formatLinks (Collection $ links )
91
+ private function formatLinks ($ links )
69
92
{
70
93
$ resultData = [];
71
94
foreach ($ links as $ linkKey => $ link ) {
0 commit comments