/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function save($sku, SampleInterface $sample, $isGlobalScopeContent = true) { $product = $this->productRepository->get($sku, true); if ($product->getTypeId() !== Type::TYPE_DOWNLOADABLE) { throw new InputException(__('Product type of the product must be \'downloadable\'.')); } //if (!$this->contentValidator->isValid($sample)) { // throw new InputException(__('Provided sample information is invalid.')); //} if (!in_array($sample->getSampleType(), ['url', 'file'])) { throw new InputException(__('Invalid sample type.')); } $title = $sample->getTitle(); if (empty($title)) { throw new InputException(__('Sample title cannot be empty.')); } $metadata = $this->metadataPool->getMetadata(ProductInterface::class); $sample->setProductId($product->getData($metadata->getLinkField())); $this->setFiles($sample); return $this->resourceModel->save($sample); }
/** * @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(); }