|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File: Builder.php |
|
7
|
|
|
* |
|
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
|
9
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LizardMedia\ProductAttachment\Model\Attachment; |
|
13
|
|
|
|
|
14
|
|
|
use \LizardMedia\ProductAttachment\Api\Data\AttachmentFactoryInterface; |
|
15
|
|
|
use \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface; |
|
16
|
|
|
use LizardMedia\ProductAttachment\Model\Attachment; |
|
17
|
|
|
use \Magento\Downloadable\Helper\Download; |
|
18
|
|
|
use \Magento\Downloadable\Helper\File; |
|
19
|
|
|
use \Magento\Framework\Api\DataObjectHelper; |
|
20
|
|
|
use \Magento\Framework\Exception\LocalizedException; |
|
21
|
|
|
use \Magento\Framework\DataObject\Copy; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Builder |
|
25
|
|
|
* @package LizardMedia\ProductAttachment\Model\Attachment |
|
26
|
|
|
*/ |
|
27
|
|
|
class Builder |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \LizardMedia\ProductAttachment\Api\Data\AttachmentFactoryInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $componentFactory; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $component; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \Magento\Downloadable\Helper\File |
|
43
|
|
|
*/ |
|
44
|
|
|
private $downloadableFile; |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var \Magento\Framework\Api\DataObjectHelper |
|
49
|
|
|
*/ |
|
50
|
|
|
private $dataObjectHelper; |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var \Magento\Framework\DataObject\Copy |
|
55
|
|
|
*/ |
|
56
|
|
|
private $objectCopyService; |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
private $data = []; |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param \LizardMedia\ProductAttachment\Api\Data\AttachmentFactoryInterface $componentFactory |
|
67
|
|
|
* @param \Magento\Downloadable\Helper\File $downloadableFile |
|
68
|
|
|
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper |
|
69
|
|
|
* @param \Magento\Framework\DataObject\Copy $objectCopyService |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct( |
|
72
|
|
|
AttachmentFactoryInterface $componentFactory, |
|
73
|
|
|
File $downloadableFile, |
|
74
|
|
|
DataObjectHelper $dataObjectHelper, |
|
75
|
|
|
Copy $objectCopyService |
|
76
|
|
|
) { |
|
77
|
|
|
$this->componentFactory = $componentFactory; |
|
78
|
|
|
$this->downloadableFile = $downloadableFile; |
|
79
|
|
|
$this->dataObjectHelper = $dataObjectHelper; |
|
80
|
|
|
$this->objectCopyService = $objectCopyService; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $data |
|
86
|
|
|
* |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setData(array $data) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->data = $data; |
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface $attachment |
|
98
|
|
|
* |
|
99
|
|
|
* @throws \Magento\Framework\Exception\LocalizedException |
|
100
|
|
|
* |
|
101
|
|
|
* @return \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface $attachment |
|
102
|
|
|
*/ |
|
103
|
|
|
public function build(AttachmentInterface $attachment) |
|
104
|
|
|
{ |
|
105
|
|
|
$downloadableData = $this->objectCopyService->getDataFromFieldset( |
|
106
|
|
|
'downloadable_data', |
|
107
|
|
|
'to_attachment', |
|
108
|
|
|
$this->data |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
$this->dataObjectHelper->populateWithArray( |
|
113
|
|
|
$attachment, |
|
114
|
|
|
array_merge( |
|
115
|
|
|
$this->data, |
|
116
|
|
|
$downloadableData |
|
117
|
|
|
), |
|
118
|
|
|
AttachmentInterface::class |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
if ($attachment->getAttachmentType() === Download::LINK_TYPE_FILE) { |
|
123
|
|
|
if (!isset($this->data['file'])) { |
|
124
|
|
|
throw new LocalizedException(__('Attachment file not provided')); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
$fileName = $this->downloadableFile->moveFileFromTmp( |
|
129
|
|
|
$this->getComponent()->getBaseTmpPath(), |
|
130
|
|
|
$this->getComponent()->getBasePath(), |
|
131
|
|
|
$this->data['file'] |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
$attachment->setAttachmentFile($fileName); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (!$attachment->getSortOrder()) { |
|
138
|
|
|
$attachment->setSortOrder(1); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->resetData(); |
|
142
|
|
|
|
|
143
|
|
|
return $attachment; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return void |
|
149
|
|
|
*/ |
|
150
|
|
|
private function resetData() : void |
|
151
|
|
|
{ |
|
152
|
|
|
$this->data = []; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface |
|
158
|
|
|
*/ |
|
159
|
|
|
private function getComponent() |
|
160
|
|
|
{ |
|
161
|
|
|
if (!$this->component) { |
|
162
|
|
|
$this->component = $this->componentFactory->create(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $this->component; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|