/** * 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 */ protected function saveSample(\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent) { $sampleData = ['sample_id' => $sample->getid() === null ? 0 : $sample->getid(), 'is_delete' => 0, 'type' => $sample->getSampleType(), 'sort_order' => $sample->getSortOrder(), 'title' => $sample->getTitle()]; if ($sample->getSampleType() == 'file' && $sample->getSampleFile() === null) { $sampleData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($sample->getSampleFileContent(), 'sample')]); } elseif ($sample->getSampleType() === 'url') { $sampleData['sample_url'] = $sample->getSampleUrl(); } else { //existing file $sampleData['file'] = $this->jsonEncoder->encode([['file' => $sample->getSampleFile(), 'status' => 'old']]); } $downloadableData = ['sample' => [$sampleData]]; $product->setDownloadableData($downloadableData); if ($isGlobalScopeContent) { $product->setStoreId(0); } $this->downloadableType->save($product); return $product->getLastAddedSampleId(); }