Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Builder.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Downloadable\Model\LinkFactory;
13 
19 class Builder
20 {
24  private $component;
25 
29  private $downloadableFile;
30 
34  private $objectCopyService;
35 
39  private $dataObjectHelper;
40 
44  private $componentFactory;
45 
49  private $data = [];
50 
59  public function __construct(
60  File $downloadableFile,
61  Copy $objectCopyService,
62  DataObjectHelper $dataObjectHelper,
63  LinkFactory $componentFactory
64  ) {
65  $this->downloadableFile = $downloadableFile;
66  $this->objectCopyService = $objectCopyService;
67  $this->dataObjectHelper = $dataObjectHelper;
68  $this->componentFactory = $componentFactory;
69  }
70 
76  public function setData(array $data)
77  {
78  $this->data = $data;
79  return $this;
80  }
81 
88  public function build(\Magento\Downloadable\Api\Data\LinkInterface $link)
89  {
90  $downloadableData = $this->objectCopyService->getDataFromFieldset(
91  'downloadable_data',
92  'to_link',
93  $this->data
94  );
95  $this->dataObjectHelper->populateWithArray(
96  $link,
97  array_merge(
98  $this->data,
100  ),
101  \Magento\Downloadable\Api\Data\LinkInterface::class
102  );
104  if (!isset($this->data['file'])) {
105  throw new \Magento\Framework\Exception\LocalizedException(__('Link file not provided'));
106  }
107  $linkFileName = $this->downloadableFile->moveFileFromTmp(
108  $this->getComponent()->getBaseTmpPath(),
109  $this->getComponent()->getBasePath(),
110  $this->data['file']
111  );
112  $link->setLinkFile($linkFileName);
113  $link->setLinkUrl(null);
114  }
115 
116  if (isset($this->data['sample'])) {
117  $link = $this->buildSample($link, $this->data['sample']);
118  }
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 
139  private function resetData()
140  {
141  $this->data = [];
142  }
143 
147  private function getComponent()
148  {
149  if (!$this->component) {
150  $this->component = $this->componentFactory->create();
151  }
152  return $this->component;
153  }
154 
161  private function buildSample(\Magento\Downloadable\Api\Data\LinkInterface $link, array $sample)
162  {
163  if (!empty($sample['url']) || !empty($sample['file'])) {
164  $downloadableLinkSampleData = $this->objectCopyService->getDataFromFieldset(
165  'downloadable_link_sample_data',
166  'to_link_sample',
167  $this->data['sample']
168  );
169  $this->dataObjectHelper->populateWithArray(
170  $link,
171  array_merge(
172  $this->data,
173  $downloadableLinkSampleData
174  ),
175  \Magento\Downloadable\Api\Data\LinkInterface::class
176  );
178  $linkSampleFileName = $this->downloadableFile->moveFileFromTmp(
179  $this->getComponent()->getBaseSampleTmpPath(),
180  $this->getComponent()->getBaseSamplePath(),
181  $sample['file']
182  );
183  $link->setSampleFile($linkSampleFileName);
184  }
185  }
186 
187  return $link;
188  }
189 }
__()
Definition: __.php:13