/** * Check if link content is valid * * @param LinkInterface $link * @param bool $validateLinkContent * @param bool $validateSampleContent * @return bool * @throws InputException */ public function isValid(LinkInterface $link, $validateLinkContent = true, $validateSampleContent = true) { if (!is_numeric($link->getPrice()) || $link->getPrice() < 0) { throw new InputException(__('Link price must have numeric positive value.')); } if (!is_int($link->getNumberOfDownloads()) || $link->getNumberOfDownloads() < 0) { throw new InputException(__('Number of downloads must be a positive integer.')); } if (!is_int($link->getSortOrder()) || $link->getSortOrder() < 0) { throw new InputException(__('Sort order must be a positive integer.')); } if ($validateLinkContent) { $this->validateLinkResource($link); } if ($validateSampleContent) { $this->validateSampleResource($link); } return true; }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param LinkInterface $link * @param bool $isGlobalScopeContent * @return int */ protected function saveLink(\Magento\Catalog\Api\Data\ProductInterface $product, LinkInterface $link, $isGlobalScopeContent) { $linkData = ['link_id' => $link->getid() === null ? 0 : $link->getid(), 'is_delete' => 0, 'type' => $link->getLinkType(), 'sort_order' => $link->getSortOrder(), 'title' => $link->getTitle(), 'price' => $link->getPrice(), 'number_of_downloads' => $link->getNumberOfDownloads(), 'is_shareable' => $link->getIsShareable()]; if ($link->getLinkType() == 'file' && $link->getLinkFile() === null) { $linkData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($link->getLinkFileContent(), 'link_file')]); } elseif ($link->getLinkType() === 'url') { $linkData['link_url'] = $link->getLinkUrl(); } else { //existing link file $linkData['file'] = $this->jsonEncoder->encode([['file' => $link->getLinkFile(), 'status' => 'old']]); } if ($link->getSampleType() == 'file' && $link->getSampleFile() === null) { $linkData['sample']['type'] = 'file'; $linkData['sample']['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($link->getSampleFileContent(), 'link_sample_file')]); } elseif ($link->getSampleType() == 'url') { $linkData['sample']['type'] = 'url'; $linkData['sample']['url'] = $link->getSampleUrl(); } $downloadableData = ['link' => [$linkData]]; $product->setDownloadableData($downloadableData); if ($isGlobalScopeContent) { $product->setStoreId(0); } $this->downloadableType->save($product); return $product->getLastAddedLinkId(); }