Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinkRepository.php
Go to the documentation of this file.
1 <?php
8 
20 
26 {
30  protected $productRepository;
31 
36 
40  protected $linkFactory;
41 
45  protected $contentValidator;
46 
50  protected $downloadableType;
51 
56 
60  protected $jsonEncoder;
61 
65  private $linkTypeHandler;
66 
70  private $metadataPool;
71 
81  public function __construct(
83  \Magento\Downloadable\Model\Product\Type $downloadableType,
84  \Magento\Downloadable\Api\Data\LinkInterfaceFactory $linkDataObjectFactory,
85  LinkFactory $linkFactory,
89  ) {
90  $this->productRepository = $productRepository;
91  $this->downloadableType = $downloadableType;
92  $this->linkDataObjectFactory = $linkDataObjectFactory;
93  $this->linkFactory = $linkFactory;
94  $this->contentValidator = $contentValidator;
95  $this->jsonEncoder = $jsonEncoder;
96  $this->fileContentUploader = $fileContentUploader;
97  }
98 
102  public function getList($sku)
103  {
105  $product = $this->productRepository->get($sku);
106  return $this->getLinksByProduct($product);
107  }
108 
113  public function getLinksByProduct(\Magento\Catalog\Api\Data\ProductInterface $product)
114  {
115  $linkList = [];
116  $links = $this->downloadableType->getLinks($product);
118  foreach ($links as $link) {
119  $linkList[] = $this->buildLink($link);
120  }
121  return $linkList;
122  }
123 
130  protected function buildLink($resourceData)
131  {
133  $link = $this->linkDataObjectFactory->create();
134  $this->setBasicFields($resourceData, $link);
135  $link->setPrice($resourceData->getPrice());
136  $link->setNumberOfDownloads($resourceData->getNumberOfDownloads());
137  $link->setIsShareable($resourceData->getIsShareable());
138  $link->setLinkType($resourceData->getLinkType());
139  $link->setLinkFile($resourceData->getLinkFile());
140  $link->setLinkUrl($resourceData->getLinkUrl());
141 
142  return $link;
143  }
144 
152  protected function setBasicFields($resourceData, $dataObject)
153  {
154  $dataObject->setId($resourceData->getId());
155  $storeTitle = $resourceData->getStoreTitle();
156  $title = $resourceData->getTitle();
157  if (!empty($storeTitle)) {
158  $dataObject->setTitle($storeTitle);
159  } else {
160  $dataObject->setTitle($title);
161  }
162  $dataObject->setSortOrder($resourceData->getSortOrder());
163  $dataObject->setSampleType($resourceData->getSampleType());
164  $dataObject->setSampleFile($resourceData->getSampleFile());
165  $dataObject->setSampleUrl($resourceData->getSampleUrl());
166  }
167 
173  public function save($sku, LinkInterface $link, $isGlobalScopeContent = true)
174  {
175  $product = $this->productRepository->get($sku, true);
176  if ($link->getId() !== null) {
177  return $this->updateLink($product, $link, $isGlobalScopeContent);
178  } else {
180  throw new InputException(
181  __('The product needs to be the downloadable type. Verify the product and try again.')
182  );
183  }
184  $validateLinkContent = !($link->getLinkType() === 'file' && $link->getLinkFile());
185  $validateSampleContent = !($link->getSampleType() === 'file' && $link->getSampleFile());
186  if (!$this->contentValidator->isValid($link, $validateLinkContent, $validateSampleContent)) {
187  throw new InputException(__('The link information is invalid. Verify the link and try again.'));
188  }
189 
190  if (!in_array($link->getLinkType(), ['url', 'file'], true)) {
191  throw new InputException(__('The link type is invalid. Verify and try again.'));
192  }
193  $title = $link->getTitle();
194  if (empty($title)) {
195  throw new InputException(__('The link title is empty. Enter the link title and try again.'));
196  }
197  return $this->saveLink($product, $link, $isGlobalScopeContent);
198  }
199  }
200 
207  protected function saveLink(
208  \Magento\Catalog\Api\Data\ProductInterface $product,
210  $isGlobalScopeContent
211  ) {
212  $linkData = [
213  'link_id' => (int)$link->getId(),
214  'is_delete' => 0,
215  'type' => $link->getLinkType(),
216  'sort_order' => $link->getSortOrder(),
217  'title' => $link->getTitle(),
218  'price' => $link->getPrice(),
219  'number_of_downloads' => $link->getNumberOfDownloads(),
220  'is_shareable' => $link->getIsShareable(),
221  ];
222 
223  if ($link->getLinkType() == 'file' && $link->getLinkFile() === null) {
224  $linkData['file'] = $this->jsonEncoder->encode(
225  [
226  $this->fileContentUploader->upload($link->getLinkFileContent(), 'link_file'),
227  ]
228  );
229  } elseif ($link->getLinkType() === 'url') {
230  $linkData['link_url'] = $link->getLinkUrl();
231  } else {
232  //existing link file
233  $linkData['file'] = $this->jsonEncoder->encode(
234  [
235  [
236  'file' => $link->getLinkFile(),
237  'status' => 'old',
238  ]
239  ]
240  );
241  }
242 
243  if ($link->getSampleType() == 'file') {
244  $linkData['sample']['type'] = 'file';
245  if ($link->getSampleFile() === null) {
246  $fileData = [
247  $this->fileContentUploader->upload($link->getSampleFileContent(), 'link_sample_file'),
248  ];
249  } else {
250  $fileData = [
251  [
252  'file' => $link->getSampleFile(),
253  'status' => 'old',
254  ]
255  ];
256  }
257  $linkData['sample']['file'] = $this->jsonEncoder->encode($fileData);
258  } elseif ($link->getSampleType() == 'url') {
259  $linkData['sample']['type'] = 'url';
260  $linkData['sample']['url'] = $link->getSampleUrl();
261  }
262 
263  $downloadableData = ['link' => [$linkData]];
264  if ($isGlobalScopeContent) {
265  $product->setStoreId(0);
266  }
267  $this->getLinkTypeHandler()->save($product, $downloadableData);
268  return $product->getLastAddedLinkId();
269  }
270 
281  protected function updateLink(
282  \Magento\Catalog\Api\Data\ProductInterface $product,
284  $isGlobalScopeContent
285  ) {
287  $existingLink = $this->linkFactory->create()->load($link->getId());
288  if (!$existingLink->getId()) {
289  throw new NoSuchEntityException(
290  __('No downloadable link with the provided ID was found. Verify the ID and try again.')
291  );
292  }
293  $linkFieldValue = $product->getData(
294  $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()
295  );
296  if ($existingLink->getProductId() != $linkFieldValue) {
297  throw new InputException(
298  __("The downloadable link isn't related to the product. Verify the link and try again.")
299  );
300  }
301  $validateLinkContent = !($link->getLinkFileContent() === null);
302  $validateSampleContent = !($link->getSampleFileContent() === null);
303  if (!$this->contentValidator->isValid($link, $validateLinkContent, $validateSampleContent)) {
304  throw new InputException(__('The link information is invalid. Verify the link and try again.'));
305  }
306  if ($isGlobalScopeContent) {
307  $product->setStoreId(0);
308  }
309  $title = $link->getTitle();
310  if (empty($title)) {
311  if ($isGlobalScopeContent) {
312  throw new InputException(__('The link title is empty. Enter the link title and try again.'));
313  }
314  }
315 
316  if ($link->getLinkType() == 'file' && $link->getLinkFileContent() === null && !$link->getLinkFile()) {
317  $link->setLinkFile($existingLink->getLinkFile());
318  }
319  if ($link->getSampleType() == 'file' && $link->getSampleFileContent() === null && !$link->getSampleFile()) {
320  $link->setSampleFile($existingLink->getSampleFile());
321  }
322 
323  $this->saveLink($product, $link, $isGlobalScopeContent);
324  return $existingLink->getId();
325  }
326 
330  public function delete($id)
331  {
333  $link = $this->linkFactory->create()->load($id);
334  if (!$link->getId()) {
335  throw new NoSuchEntityException(
336  __('No downloadable link with the provided ID was found. Verify the ID and try again.')
337  );
338  }
339  try {
340  $link->delete();
341  } catch (\Exception $exception) {
342  throw new StateException(__('The link with "%1" ID can\'t be deleted.', $link->getId()), $exception);
343  }
344  return true;
345  }
346 
353  private function getMetadataPool()
354  {
355  if (!$this->metadataPool) {
356  $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
357  }
358 
359  return $this->metadataPool;
360  }
361 
368  private function getLinkTypeHandler()
369  {
370  if (!$this->linkTypeHandler) {
371  $this->linkTypeHandler = ObjectManager::getInstance()->get(LinkHandler::class);
372  }
373 
374  return $this->linkTypeHandler;
375  }
376 }
$title
Definition: default.phtml:14
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13