65 private $linkTypeHandler;
70 private $metadataPool;
105 $product = $this->productRepository->get($sku);
119 $linkList[] = $this->buildLink(
$link);
130 protected function buildLink($resourceData)
133 $link = $this->linkDataObjectFactory->create();
135 $link->setPrice($resourceData->getPrice());
136 $link->setNumberOfDownloads($resourceData->getNumberOfDownloads());
137 $link->setIsShareable($resourceData->getIsShareable());
138 $link->setLinkType($resourceData->getLinkType());
139 $link->setLinkFile($resourceData->getLinkFile());
140 $link->setLinkUrl($resourceData->getLinkUrl());
154 $dataObject->setId($resourceData->getId());
155 $storeTitle = $resourceData->getStoreTitle();
156 $title = $resourceData->getTitle();
157 if (!empty($storeTitle)) {
158 $dataObject->setTitle($storeTitle);
160 $dataObject->setTitle(
$title);
162 $dataObject->setSortOrder($resourceData->getSortOrder());
163 $dataObject->setSampleType($resourceData->getSampleType());
164 $dataObject->setSampleFile($resourceData->getSampleFile());
165 $dataObject->setSampleUrl($resourceData->getSampleUrl());
175 $product = $this->productRepository->get($sku,
true);
176 if (
$link->getId() !==
null) {
177 return $this->updateLink(
$product,
$link, $isGlobalScopeContent);
181 __(
'The product needs to be the downloadable type. Verify the product and try again.')
184 $validateLinkContent = !(
$link->getLinkType() ===
'file' &&
$link->getLinkFile());
185 $validateSampleContent = !(
$link->getSampleType() ===
'file' &&
$link->getSampleFile());
186 if (!$this->contentValidator->isValid(
$link, $validateLinkContent, $validateSampleContent)) {
187 throw new InputException(
__(
'The link information is invalid. Verify the link and try again.'));
190 if (!in_array(
$link->getLinkType(), [
'url',
'file'],
true)) {
191 throw new InputException(
__(
'The link type is invalid. Verify and try again.'));
195 throw new InputException(
__(
'The link title is empty. Enter the link title and try again.'));
197 return $this->
saveLink($product,
$link, $isGlobalScopeContent);
210 $isGlobalScopeContent
213 'link_id' => (int)
$link->getId(),
215 'type' =>
$link->getLinkType(),
216 'sort_order' =>
$link->getSortOrder(),
217 'title' =>
$link->getTitle(),
218 'price' =>
$link->getPrice(),
219 'number_of_downloads' =>
$link->getNumberOfDownloads(),
220 'is_shareable' =>
$link->getIsShareable(),
223 if (
$link->getLinkType() ==
'file' &&
$link->getLinkFile() ===
null) {
224 $linkData[
'file'] = $this->jsonEncoder->encode(
226 $this->fileContentUploader->upload(
$link->getLinkFileContent(),
'link_file'),
233 $linkData[
'file'] = $this->jsonEncoder->encode(
236 'file' =>
$link->getLinkFile(),
243 if (
$link->getSampleType() ==
'file') {
245 if (
$link->getSampleFile() ===
null) {
247 $this->fileContentUploader->upload(
$link->getSampleFileContent(),
'link_sample_file'),
252 'file' =>
$link->getSampleFile(),
257 $linkData[
'sample'][
'file'] = $this->jsonEncoder->encode($fileData);
264 if ($isGlobalScopeContent) {
268 return $product->getLastAddedLinkId();
281 protected function updateLink(
284 $isGlobalScopeContent
287 $existingLink = $this->linkFactory->create()->load(
$link->getId());
288 if (!$existingLink->getId()) {
290 __(
'No downloadable link with the provided ID was found. Verify the ID and try again.')
293 $linkFieldValue =
$product->getData(
294 $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()
296 if ($existingLink->getProductId() != $linkFieldValue) {
297 throw new InputException(
298 __(
"The downloadable link isn't related to the product. Verify the link and try again.")
301 $validateLinkContent = !(
$link->getLinkFileContent() ===
null);
302 $validateSampleContent = !(
$link->getSampleFileContent() ===
null);
303 if (!$this->contentValidator->isValid(
$link, $validateLinkContent, $validateSampleContent)) {
304 throw new InputException(
__(
'The link information is invalid. Verify the link and try again.'));
306 if ($isGlobalScopeContent) {
311 if ($isGlobalScopeContent) {
312 throw new InputException(
__(
'The link title is empty. Enter the link title and try again.'));
316 if (
$link->getLinkType() ==
'file' &&
$link->getLinkFileContent() ===
null && !
$link->getLinkFile()) {
317 $link->setLinkFile($existingLink->getLinkFile());
319 if (
$link->getSampleType() ==
'file' &&
$link->getSampleFileContent() ===
null && !
$link->getSampleFile()) {
320 $link->setSampleFile($existingLink->getSampleFile());
324 return $existingLink->getId();
330 public function delete(
$id)
333 $link = $this->linkFactory->create()->load(
$id);
334 if (!
$link->getId()) {
335 throw new NoSuchEntityException(
336 __(
'No downloadable link with the provided ID was found. Verify the ID and try again.')
341 }
catch (\Exception $exception) {
342 throw new StateException(
__(
'The link with "%1" ID can\'t be deleted.',
$link->getId()), $exception);
353 private function getMetadataPool()
355 if (!$this->metadataPool) {
359 return $this->metadataPool;
368 private function getLinkTypeHandler()
370 if (!$this->linkTypeHandler) {
374 return $this->linkTypeHandler;
elseif(isset( $params[ 'redirect_parent']))
save($sku, LinkInterface $link, $isGlobalScopeContent=true)
getLinksByProduct(ProductInterface $product)
setBasicFields($resourceData, $dataObject)
__construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Downloadable\Model\Product\Type $downloadableType, \Magento\Downloadable\Api\Data\LinkInterfaceFactory $linkDataObjectFactory, LinkFactory $linkFactory, Link\ContentValidator $contentValidator, EncoderInterface $jsonEncoder, ContentUploaderInterface $fileContentUploader)
saveLink(\Magento\Catalog\Api\Data\ProductInterface $product, LinkInterface $link, $isGlobalScopeContent)