7
7
8
8
use Magento \Downloadable \Api \Data \SampleInterfaceFactory as SampleFactory ;
9
9
use Magento \Downloadable \Api \Data \LinkInterfaceFactory as LinkFactory ;
10
+ use Magento \Framework \App \ObjectManager ;
10
11
use Magento \Framework \App \RequestInterface ;
11
- use Magento \Framework \Json \Helper \Data as JsonHelper ;
12
12
13
13
/**
14
14
* Class Downloadable
@@ -23,34 +23,30 @@ class Downloadable
23
23
/**
24
24
* @var SampleFactory
25
25
*/
26
- protected $ sampleFactory ;
26
+ private $ sampleFactory ;
27
27
28
28
/**
29
29
* @var LinkFactory
30
30
*/
31
- protected $ linkFactory ;
31
+ private $ linkFactory ;
32
32
33
33
/**
34
- * @var JsonHelper
34
+ * @var \Magento\Downloadable\Model\Sample\Builder
35
35
*/
36
- protected $ jsonHelper ;
36
+ private $ sampleBuilder ;
37
+
38
+ /**
39
+ * @var \Magento\Downloadable\Model\Link\Builder
40
+ */
41
+ private $ linkBuilder ;
37
42
38
43
/**
39
44
* @param RequestInterface $request
40
- * @param SampleFactory $sampleFactory
41
- * @param LinkFactory $linkFactory
42
- * @param JsonHelper $jsonHelper
43
45
*/
44
46
public function __construct (
45
- RequestInterface $ request ,
46
- SampleFactory $ sampleFactory ,
47
- LinkFactory $ linkFactory ,
48
- JsonHelper $ jsonHelper
47
+ RequestInterface $ request
49
48
) {
50
49
$ this ->request = $ request ;
51
- $ this ->linkFactory = $ linkFactory ;
52
- $ this ->sampleFactory = $ sampleFactory ;
53
- $ this ->jsonHelper = $ jsonHelper ;
54
50
}
55
51
56
52
/**
@@ -74,49 +70,14 @@ public function afterInitialize(
74
70
if (isset ($ downloadable ['link ' ]) && is_array ($ downloadable ['link ' ])) {
75
71
$ links = [];
76
72
foreach ($ downloadable ['link ' ] as $ linkData ) {
77
- if (!$ linkData || (isset ($ linkData ['is_delete ' ]) && ( bool ) $ linkData ['is_delete ' ])) {
73
+ if (!$ linkData || (isset ($ linkData ['is_delete ' ]) && $ linkData ['is_delete ' ])) {
78
74
continue ;
79
75
} else {
80
- // TODO: need to implement setLinkFileContent()
81
- $ link = $ this ->linkFactory ->create (['data ' => $ linkData ]);
82
- if (isset ($ linkData ['type ' ])) {
83
- $ link ->setLinkType ($ linkData ['type ' ]);
84
- }
85
- if (isset ($ linkData ['file ' ])) {
86
- $ link ->setFile ($ this ->jsonHelper ->jsonEncode ($ linkData ['file ' ]));
87
- }
88
- if (isset ($ linkData ['file_content ' ])) {
89
- $ link ->setLinkFileContent ($ linkData ['file_content ' ]);
90
- }
91
- $ link ->setId (null );
92
- if (isset ($ linkData ['link_id ' ])) {
93
- $ link ->setId ($ linkData ['link_id ' ]);
94
- }
95
- if (isset ($ linkData ['sample ' ]['type ' ])) {
96
- $ link ->setSampleType ($ linkData ['sample ' ]['type ' ]);
97
- }
98
- if (isset ($ linkData ['sample ' ]['file ' ])) {
99
- $ link ->setSampleFileData ($ this ->jsonHelper ->jsonEncode ($ linkData ['sample ' ]['file ' ]));
100
- }
101
- if (isset ($ linkData ['sample ' ]['url ' ])) {
102
- $ link ->setSampleUrl ($ linkData ['sample ' ]['url ' ]);
103
- }
104
- if (isset ($ linkData ['sample ' ]['file_content ' ])) {
105
- $ link ->setSampleFileContent ($ linkData ['file_content ' ]);
106
- }
107
- $ link ->setStoreId ($ product ->getStoreId ());
108
- $ link ->setWebsiteId ($ product ->getStore ()->getWebsiteId ());
109
- $ link ->setProductWebsiteIds ($ product ->getWebsiteIds ());
110
- if (!$ link ->getSortOrder ()) {
111
- $ link ->setSortOrder (1 );
112
- }
113
- if (null === $ link ->getPrice ()) {
114
- $ link ->setPrice (0 );
115
- }
116
- if ($ link ->getIsUnlimited ()) {
117
- $ link ->setNumberOfDownloads (0 );
118
- }
119
- $ links [] = $ link ;
76
+ $ links [] = $ this ->getLinkBuilder ()->setData (
77
+ $ linkData
78
+ )->build (
79
+ $ this ->getLinkFactory ()->create ()
80
+ );
120
81
}
121
82
}
122
83
$ extension ->setDownloadableProductLinks ($ links );
@@ -127,25 +88,11 @@ public function afterInitialize(
127
88
if (!$ sampleData || (isset ($ sampleData ['is_delete ' ]) && (bool )$ sampleData ['is_delete ' ])) {
128
89
continue ;
129
90
} else {
130
- $ sample = $ this ->sampleFactory ->create (['data ' => $ sampleData ]);
131
- $ sample ->setId (null );
132
- if (isset ($ sampleData ['sample_id ' ])) {
133
- $ sample ->setId ($ sampleData ['sample_id ' ]);
134
- }
135
- $ sample ->setStoreId ($ product ->getStoreId ());
136
- if (isset ($ sampleData ['type ' ])) {
137
- $ sample ->setSampleType ($ sampleData ['type ' ]);
138
- }
139
- if (isset ($ sampleData ['file ' ])) {
140
- $ sample ->setFile ($ this ->jsonHelper ->jsonEncode ($ sampleData ['file ' ]));
141
- }
142
- if (isset ($ sampleData ['sample_url ' ])) {
143
- $ sample ->setSampleUrl ($ sampleData ['sample_url ' ]);
144
- }
145
- if (!$ sample ->getSortOrder ()) {
146
- $ sample ->setSortOrder (1 );
147
- }
148
- $ samples [] = $ sample ;
91
+ $ samples [] = $ this ->getSampleBuilder ()->setData (
92
+ $ sampleData
93
+ )->build (
94
+ $ this ->getSampleFactory ()->create ()
95
+ );
149
96
}
150
97
}
151
98
$ extension ->setDownloadableProductSamples ($ samples );
@@ -159,4 +106,66 @@ public function afterInitialize(
159
106
}
160
107
return $ product ;
161
108
}
109
+
110
+ /**
111
+ * Get LinkBuilder instance
112
+ *
113
+ * @deprecated
114
+ * @return \Magento\Downloadable\Model\Link\Builder
115
+ */
116
+ private function getLinkBuilder ()
117
+ {
118
+ if (!$ this ->linkBuilder ) {
119
+ $ this ->linkBuilder = ObjectManager::getInstance ()->get (\Magento \Downloadable \Model \Link \Builder::class);
120
+ }
121
+
122
+ return $ this ->linkBuilder ;
123
+ }
124
+
125
+ /**
126
+ * Get SampleBuilder instance
127
+ *
128
+ * @deprecated
129
+ * @return \Magento\Downloadable\Model\Sample\Builder
130
+ */
131
+ private function getSampleBuilder ()
132
+ {
133
+ if (!$ this ->sampleBuilder ) {
134
+ $ this ->sampleBuilder = ObjectManager::getInstance ()->get (
135
+ \Magento \Downloadable \Model \Sample \Builder::class
136
+ );
137
+ }
138
+
139
+ return $ this ->sampleBuilder ;
140
+ }
141
+
142
+ /**
143
+ * Get LinkFactory instance
144
+ *
145
+ * @deprecated
146
+ * @return LinkFactory
147
+ */
148
+ private function getLinkFactory ()
149
+ {
150
+ if (!$ this ->linkFactory ) {
151
+ $ this ->linkFactory = ObjectManager::getInstance ()->get (LinkFactory::class);
152
+ }
153
+
154
+ return $ this ->linkFactory ;
155
+ }
156
+
157
+ /**
158
+ * Get Sample Factory
159
+ *
160
+ * @deprecated
161
+ * @return SampleFactory
162
+ */
163
+ private function getSampleFactory ()
164
+ {
165
+ if (!$ this ->sampleFactory ) {
166
+ $ this ->sampleFactory = ObjectManager::getInstance ()->get (SampleFactory::class);
167
+ }
168
+
169
+ return $ this ->sampleFactory ;
170
+ }
162
171
}
0 commit comments