/** * Validate sample resource (file or URL) * * @param LinkInterface $link * @throws InputException * @return void */ protected function validateSampleResource(LinkInterface $link) { if ($link->getSampleType() == 'url' && !$this->urlValidator->isValid($link->getSampleUrl())) { throw new InputException(__('Sample URL must have valid format.')); } if ($link->getSampleType() == 'file' && (!$link->getSampleFileContent() || !$this->fileContentValidator->isValid($link->getSampleFileContent()))) { throw new InputException(__('Provided file content must be valid base64 encoded data.')); } }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param LinkInterface $link * @param bool $isGlobalScopeContent * @return mixed * @throws InputException * @throws NoSuchEntityException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function updateLink(\Magento\Catalog\Api\Data\ProductInterface $product, LinkInterface $link, $isGlobalScopeContent) { /** @var $existingLink \Magento\Downloadable\Model\Link */ $existingLink = $this->linkFactory->create()->load($link->getId()); if (!$existingLink->getId()) { throw new NoSuchEntityException(__('There is no downloadable link with provided ID.')); } if ($existingLink->getProductId() != $product->getId()) { throw new InputException(__('Provided downloadable link is not related to given product.')); } $validateLinkContent = $link->getLinkFileContent() === null ? false : true; $validateSampleContent = $link->getSampleFileContent() === null ? false : true; if (!$this->contentValidator->isValid($link, $validateLinkContent, $validateSampleContent)) { throw new InputException(__('Provided link information is invalid.')); } if ($isGlobalScopeContent) { $product->setStoreId(0); } $title = $link->getTitle(); if (empty($title)) { if ($isGlobalScopeContent) { throw new InputException(__('Link title cannot be empty.')); } } if ($link->getLinkType() == 'file' && $link->getLinkFileContent() === null) { $link->setLinkFile($existingLink->getLinkFile()); } if ($link->getSampleType() == 'file' && $link->getSampleFileContent() === null) { $link->setSampleFile($existingLink->getSampleFile()); } $this->saveLink($product, $link, $isGlobalScopeContent); return $existingLink->getId(); }
/** * Load files and set paths to link and sample of link * * @param LinkInterface $link * @return void */ protected function setFiles(LinkInterface $link) { if ($link->getSampleType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE && $link->getSampleFileData()) { $linkSampleFileName = $this->downloadableFile->moveFileFromTmp($link->getBaseSampleTmpPath(), $link->getBaseSamplePath(), $this->jsonHelper->jsonDecode($link->getSampleFileData())); $link->setSampleFile($linkSampleFileName); $link->setSampleUrl(null); } if ($link->getLinkType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE && $link->getFile()) { $linkFileName = $this->downloadableFile->moveFileFromTmp($link->getBaseTmpPath(), $link->getBasePath(), $this->jsonHelper->jsonDecode($link->getFile())); $link->setLinkFile($linkFileName); $link->setLinkUrl(null); } }
/** * Add Link File info into $linkData * * @param array $linkData * @param LinkInterface $link * @return array */ protected function addLinkFile(array $linkData, LinkInterface $link) { $linkFile = $link->getLinkFile(); if ($linkFile) { $file = $this->downloadableFile->getFilePath($this->linkModel->getBasePath(), $linkFile); if ($this->downloadableFile->ensureFileInFilesystem($file)) { $linkData['file'][0] = ['file' => $linkFile, 'name' => $this->downloadableFile->getFileFromPathFile($linkFile), 'size' => $this->downloadableFile->getFileSize($file), 'status' => 'old', 'url' => $this->urlBuilder->addSessionParam()->getUrl('adminhtml/downloadable_product_edit/link', ['id' => $link->getId(), 'type' => 'link', '_secure' => true])]; } } return $linkData; }
/** * Subroutine for buildLink and buildSample * * @param \Magento\Downloadable\Model\Link|\Magento\Downloadable\Model\Sample $resourceData * @param \Magento\Downloadable\Api\Data\LinkInterface|\Magento\Downloadable\Api\Data\SampleInterface $dataObject * @return null */ protected function setBasicFields($resourceData, $dataObject) { $dataObject->setId($resourceData->getId()); $storeTitle = $resourceData->getStoreTitle(); $title = $resourceData->getTitle(); if (!empty($storeTitle)) { $dataObject->setTitle($storeTitle); } else { $dataObject->setTitle($title); } $dataObject->setSortOrder($resourceData->getSortOrder()); $dataObject->setSampleType($resourceData->getSampleType()); $dataObject->setSampleFile($resourceData->getSampleFile()); $dataObject->setSampleUrl($resourceData->getSampleUrl()); }