/** * 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.')); } }
/** * @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(); }