/** * Validate sample resource (file or URL) * * @param SampleInterface $sample * @throws InputException * @return void */ protected function validateSampleResource(SampleInterface $sample) { $sampleFile = $sample->getSampleFileContent(); if ($sample->getSampleType() == 'file' && (!$sampleFile || !$this->fileContentValidator->isValid($sampleFile))) { throw new InputException(__('Provided file content must be valid base64 encoded data.')); } if ($sample->getSampleType() == 'url' && !$this->urlValidator->isValid($sample->getSampleUrl())) { throw new InputException(__('Sample URL must have valid format.')); } }
/** * Load file and set path to sample * * @param SampleInterface $sample * @return void */ protected function setFiles(SampleInterface $sample) { if ($sample->getSampleType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE && $sample->getFile()) { $sampleFileName = $this->downloadableFile->moveFileFromTmp($sample->getBaseTmpPath(), $sample->getBasePath(), $this->jsonHelper->jsonDecode($sample->getFile())); $sample->setSampleFile($sampleFileName); $sample->setSampleUrl(null); } }
/** * Add Sample File info into $sampleData * * @param array $sampleData * @param SampleInterface $sample * @return array */ protected function addSampleFile(array $sampleData, SampleInterface $sample) { $sampleFile = $sample->getSampleFile(); if ($sampleFile) { $file = $this->downloadableFile->getFilePath($this->sampleModel->getBasePath(), $sampleFile); if ($this->downloadableFile->ensureFileInFilesystem($file)) { $sampleData['file'][0] = ['file' => $sampleFile, 'name' => $this->downloadableFile->getFileFromPathFile($sampleFile), 'size' => $this->downloadableFile->getFileSize($file), 'status' => 'old', 'url' => $this->urlBuilder->addSessionParam()->getUrl('adminhtml/downloadable_product_edit/sample', ['id' => $sample->getId(), '_secure' => true])]; } } return $sampleData; }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param SampleInterface $sample * @param bool $isGlobalScopeContent * @return int * @throws InputException * @throws NoSuchEntityException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function updateSample(\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent) { $sampleId = $sample->getId(); /** @var $existingSample \Magento\Downloadable\Model\Sample */ $existingSample = $this->sampleFactory->create()->load($sampleId); if (!$existingSample->getId()) { throw new NoSuchEntityException(__('There is no downloadable sample with provided ID.')); } if ($existingSample->getProductId() != $product->getId()) { throw new InputException(__('Provided downloadable sample is not related to given product.')); } $validateFileContent = $sample->getSampleFileContent() === null ? false : true; if (!$this->contentValidator->isValid($sample, $validateFileContent)) { throw new InputException(__('Provided sample information is invalid.')); } if ($isGlobalScopeContent) { $product->setStoreId(0); } $title = $sample->getTitle(); if (empty($title)) { if ($isGlobalScopeContent) { throw new InputException(__('Sample title cannot be empty.')); } // use title from GLOBAL scope $existingSample->setTitle(null); } else { $existingSample->setTitle($sample->getTitle()); } if ($sample->getSampleType() === 'file' && $sample->getSampleFileContent() === null) { $sample->setSampleFile($existingSample->getSampleFile()); } $this->saveSample($product, $sample, $isGlobalScopeContent); return $existingSample->getId(); }
/** * 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()); }