Skip to content

Commit 4e6fc89

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-52170' into MAGETWO-52374
2 parents a5e6889 + 8c18808 commit 4e6fc89

File tree

9 files changed

+804
-92
lines changed

9 files changed

+804
-92
lines changed

app/code/Magento/Downloadable/Controller/Adminhtml/Product/Initialization/Helper/Plugin/Downloadable.php

Lines changed: 84 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
use Magento\Downloadable\Api\Data\SampleInterfaceFactory as SampleFactory;
99
use Magento\Downloadable\Api\Data\LinkInterfaceFactory as LinkFactory;
10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\App\RequestInterface;
11-
use Magento\Framework\Json\Helper\Data as JsonHelper;
1212

1313
/**
1414
* Class Downloadable
@@ -23,34 +23,30 @@ class Downloadable
2323
/**
2424
* @var SampleFactory
2525
*/
26-
protected $sampleFactory;
26+
private $sampleFactory;
2727

2828
/**
2929
* @var LinkFactory
3030
*/
31-
protected $linkFactory;
31+
private $linkFactory;
3232

3333
/**
34-
* @var JsonHelper
34+
* @var \Magento\Downloadable\Model\Sample\Builder
3535
*/
36-
protected $jsonHelper;
36+
private $sampleBuilder;
37+
38+
/**
39+
* @var \Magento\Downloadable\Model\Link\Builder
40+
*/
41+
private $linkBuilder;
3742

3843
/**
3944
* @param RequestInterface $request
40-
* @param SampleFactory $sampleFactory
41-
* @param LinkFactory $linkFactory
42-
* @param JsonHelper $jsonHelper
4345
*/
4446
public function __construct(
45-
RequestInterface $request,
46-
SampleFactory $sampleFactory,
47-
LinkFactory $linkFactory,
48-
JsonHelper $jsonHelper
47+
RequestInterface $request
4948
) {
5049
$this->request = $request;
51-
$this->linkFactory = $linkFactory;
52-
$this->sampleFactory = $sampleFactory;
53-
$this->jsonHelper = $jsonHelper;
5450
}
5551

5652
/**
@@ -74,49 +70,14 @@ public function afterInitialize(
7470
if (isset($downloadable['link']) && is_array($downloadable['link'])) {
7571
$links = [];
7672
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'])) {
7874
continue;
7975
} 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+
);
12081
}
12182
}
12283
$extension->setDownloadableProductLinks($links);
@@ -127,25 +88,11 @@ public function afterInitialize(
12788
if (!$sampleData || (isset($sampleData['is_delete']) && (bool)$sampleData['is_delete'])) {
12889
continue;
12990
} 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+
);
14996
}
15097
}
15198
$extension->setDownloadableProductSamples($samples);
@@ -159,4 +106,66 @@ public function afterInitialize(
159106
}
160107
return $product;
161108
}
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+
}
162171
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Downloadable\Model\Link;
7+
8+
use Magento\Downloadable\Helper\File;
9+
use Magento\Downloadable\Model\Link;
10+
use Magento\Downloadable\Model\LinkFactory;
11+
use Magento\Framework\Api\DataObjectHelper;
12+
use Magento\Framework\DataObject\Copy;
13+
14+
/**
15+
* Class Builder
16+
*/
17+
class Builder
18+
{
19+
/**
20+
* @var Link
21+
*/
22+
private $component;
23+
24+
/**
25+
* @var File
26+
*/
27+
private $downloadableFile;
28+
29+
/**
30+
* @var Copy
31+
*/
32+
private $objectCopyService;
33+
34+
/**
35+
* @var DataObjectHelper
36+
*/
37+
private $dataObjectHelper;
38+
39+
/**
40+
* @var LinkFactory
41+
*/
42+
private $componentFactory;
43+
44+
/**
45+
* @var array
46+
*/
47+
private $data = [];
48+
49+
/**
50+
* Mapper constructor.
51+
*
52+
* @param File $downloadableFile
53+
* @param Copy $objectCopyService
54+
* @param DataObjectHelper $dataObjectHelper
55+
* @param LinkFactory $componentFactory
56+
*/
57+
public function __construct(
58+
File $downloadableFile,
59+
Copy $objectCopyService,
60+
DataObjectHelper $dataObjectHelper,
61+
LinkFactory $componentFactory
62+
) {
63+
$this->downloadableFile = $downloadableFile;
64+
$this->objectCopyService = $objectCopyService;
65+
$this->dataObjectHelper = $dataObjectHelper;
66+
$this->componentFactory = $componentFactory;
67+
}
68+
69+
/**
70+
* @param array $data
71+
* @return $this
72+
*/
73+
public function setData(array $data)
74+
{
75+
$this->data = $data;
76+
return $this;
77+
}
78+
79+
/**
80+
* @param \Magento\Downloadable\Api\Data\LinkInterface $link
81+
* @return \Magento\Downloadable\Api\Data\LinkInterface
82+
* @throws \Magento\Framework\Exception\LocalizedException
83+
*/
84+
public function build(\Magento\Downloadable\Api\Data\LinkInterface $link)
85+
{
86+
$downloadableData = $this->objectCopyService->getDataFromFieldset(
87+
'downloadable_data',
88+
'to_link',
89+
$this->data
90+
);
91+
$downloadableLinkSampleData = $this->objectCopyService->getDataFromFieldset(
92+
'downloadable_link_sample_data',
93+
'to_link_sample',
94+
$this->data['sample']
95+
);
96+
$this->dataObjectHelper->populateWithArray(
97+
$link,
98+
array_merge(
99+
$this->data,
100+
$downloadableData,
101+
$downloadableLinkSampleData
102+
),
103+
\Magento\Downloadable\Api\Data\LinkInterface::class
104+
);
105+
if ($link->getLinkType() === \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
106+
if (!isset($this->data['file'])) {
107+
throw new \Magento\Framework\Exception\LocalizedException(__('Link file not provided'));
108+
}
109+
$linkFileName = $this->downloadableFile->moveFileFromTmp(
110+
$this->getComponent()->getBaseTmpPath(),
111+
$this->getComponent()->getBasePath(),
112+
$this->data['file']
113+
);
114+
$link->setLinkFile($linkFileName);
115+
$link->setLinkUrl(null);
116+
}
117+
118+
$link = $this->buildSample($link, $this->data['sample']);
119+
120+
if (!$link->getSortOrder()) {
121+
$link->setSortOrder(1);
122+
}
123+
124+
if (!is_numeric($link->getPrice())) {
125+
$link->setPrice(0);
126+
}
127+
128+
if (isset($this->data['is_unlimited']) && $this->data['is_unlimited']) {
129+
$link->setNumberOfDownloads(0);
130+
}
131+
$this->resetData();
132+
133+
return $link;
134+
}
135+
136+
/**
137+
* @return void
138+
*/
139+
private function resetData()
140+
{
141+
$this->data = [];
142+
}
143+
144+
/**
145+
* @return Link
146+
*/
147+
private function getComponent()
148+
{
149+
if (!$this->component) {
150+
$this->component = $this->componentFactory->create();
151+
}
152+
return $this->component;
153+
}
154+
155+
/**
156+
* @param \Magento\Downloadable\Api\Data\LinkInterface $link
157+
* @param array $sample
158+
* @return \Magento\Downloadable\Api\Data\LinkInterface
159+
* @throws \Magento\Framework\Exception\LocalizedException
160+
*/
161+
private function buildSample(\Magento\Downloadable\Api\Data\LinkInterface $link, array $sample)
162+
{
163+
if (!empty($sample['url']) || !empty($sample['file'])) {
164+
if ($link->getSampleType() === \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
165+
$linkSampleFileName = $this->downloadableFile->moveFileFromTmp(
166+
$this->getComponent()->getBaseSampleTmpPath(),
167+
$this->getComponent()->getBaseSamplePath(),
168+
$sample['file']
169+
);
170+
$link->setSampleFile($linkSampleFileName);
171+
}
172+
}
173+
174+
return $link;
175+
}
176+
}

0 commit comments

Comments
 (0)