Esempio n. 1
0
 /**
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param LinkInterface $link
  * @param bool $isGlobalScopeContent
  * @return mixed
  * @throws InputException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function updateLink(\Magento\Catalog\Api\Data\ProductInterface $product, LinkInterface $link, $isGlobalScopeContent)
 {
     /** @var $existingLink \Magento\Downloadable\Model\Link */
     $existingLink = $this->linkFactory->create()->load($link->getId());
     if (!$existingLink->getId()) {
         throw new NoSuchEntityException(__('There is no downloadable link with provided ID.'));
     }
     if ($existingLink->getProductId() != $product->getId()) {
         throw new InputException(__('Provided downloadable link is not related to given product.'));
     }
     $validateLinkContent = $link->getLinkFileContent() === null ? false : true;
     $validateSampleContent = $link->getSampleFileContent() === null ? false : true;
     if (!$this->contentValidator->isValid($link, $validateLinkContent, $validateSampleContent)) {
         throw new InputException(__('Provided link information is invalid.'));
     }
     if ($isGlobalScopeContent) {
         $product->setStoreId(0);
     }
     $title = $link->getTitle();
     if (empty($title)) {
         if ($isGlobalScopeContent) {
             throw new InputException(__('Link title cannot be empty.'));
         }
     }
     if ($link->getLinkType() == 'file' && $link->getLinkFileContent() === null) {
         $link->setLinkFile($existingLink->getLinkFile());
     }
     if ($link->getSampleType() == 'file' && $link->getSampleFileContent() === null) {
         $link->setSampleFile($existingLink->getSampleFile());
     }
     $this->saveLink($product, $link, $isGlobalScopeContent);
     return $existingLink->getId();
 }
Esempio n. 2
0
 /**
  * @param string|int|float $numberOfDownloads
  * @dataProvider getInvalidNumberOfDownloads
  * @expectedException \Magento\Framework\Exception\InputException
  * @expectedExceptionMessage Number of downloads must be a positive integer.
  */
 public function testIsValidThrowsExceptionIfNumberOfDownloadsIsInvalid($numberOfDownloads)
 {
     $linkContentData = ['title' => 'Title', 'sort_order' => 1, 'price' => 10.5, 'shareable' => true, 'number_of_downloads' => $numberOfDownloads, 'link_type' => 'file', 'sample_type' => 'file'];
     $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
     $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
     $contentMock = $this->getLinkMock($linkContentData);
     $this->validator->isValid($contentMock);
 }
Esempio n. 3
0
 /**
  * @expectedException \Magento\Framework\Exception\InputException
  * @expectedExceptionMessage Invalid link type.
  */
 public function testUpdateThrowsExceptionIfLinkTypeIsEmpty()
 {
     $linkId = 1;
     $productSku = 'simple';
     $productId = 1;
     $linkData = ['id' => $linkId, 'title' => 'Title', 'link_type' => '', 'sort_order' => 1, 'price' => 10.1, 'number_of_downloads' => 100, 'is_shareable' => true];
     $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)->willReturn($this->productMock);
     $this->productMock->expects($this->any())->method('getData')->with('id')->willReturn($productId);
     $linkContentMock = $this->getLinkMock($linkData);
     $this->contentValidatorMock->expects($this->any())->method('isValid')->with($linkContentMock)->willReturn(true);
     $this->service->save($productSku, $linkContentMock, true);
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function save($sku, LinkContentInterface $linkContent, $linkId = null, $isGlobalScopeContent = false)
 {
     $product = $this->productRepository->get($sku, true);
     if ($linkId) {
         /** @var $link \Magento\Downloadable\Model\Link */
         $link = $this->linkFactory->create()->load($linkId);
         if (!$link->getId()) {
             throw new NoSuchEntityException(__('There is no downloadable link with provided ID.'));
         }
         if ($link->getProductId() != $product->getId()) {
             throw new InputException(__('Provided downloadable link is not related to given product.'));
         }
         if (!$this->contentValidator->isValid($linkContent)) {
             throw new InputException(__('Provided link information is invalid.'));
         }
         if ($isGlobalScopeContent) {
             $product->setStoreId(0);
         }
         $title = $linkContent->getTitle();
         if (empty($title)) {
             if ($isGlobalScopeContent) {
                 throw new InputException(__('Link title cannot be empty.'));
             }
             // use title from GLOBAL scope
             $link->setTitle(null);
         } else {
             $link->setTitle($linkContent->getTitle());
         }
         $link->setProductId($product->getId())->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId())->setProductWebsiteIds($product->getWebsiteIds())->setSortOrder($linkContent->getSortOrder())->setPrice($linkContent->getPrice())->setIsShareable($linkContent->isShareable())->setNumberOfDownloads($linkContent->getNumberOfDownloads())->save();
         return $link->getId();
     } else {
         $product = $this->productRepository->get($sku, true);
         if ($product->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
             throw new InputException(__('Product type of the product must be \'downloadable\'.'));
         }
         if (!$this->contentValidator->isValid($linkContent)) {
             throw new InputException(__('Provided link information is invalid.'));
         }
         if (!in_array($linkContent->getLinkType(), ['url', 'file'])) {
             throw new InputException(__('Invalid link type.'));
         }
         $title = $linkContent->getTitle();
         if (empty($title)) {
             throw new InputException(__('Link title cannot be empty.'));
         }
         $linkData = ['link_id' => 0, 'is_delete' => 0, 'type' => $linkContent->getLinkType(), 'sort_order' => $linkContent->getSortOrder(), 'title' => $linkContent->getTitle(), 'price' => $linkContent->getPrice(), 'number_of_downloads' => $linkContent->getNumberOfDownloads(), 'is_shareable' => $linkContent->isShareable()];
         if ($linkContent->getLinkType() == 'file') {
             $linkData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($linkContent->getLinkFile(), 'link_file')]);
         } else {
             $linkData['link_url'] = $linkContent->getLinkUrl();
         }
         if ($linkContent->getSampleType() == 'file') {
             $linkData['sample']['type'] = 'file';
             $linkData['sample']['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($linkContent->getSampleFile(), 'link_sample_file')]);
         } elseif ($linkContent->getSampleType() == 'url') {
             $linkData['sample']['type'] = 'url';
             $linkData['sample']['url'] = $linkContent->getSampleUrl();
         }
         $downloadableData = ['link' => [$linkData]];
         $product->setDownloadableData($downloadableData);
         if ($isGlobalScopeContent) {
             $product->setStoreId(0);
         }
         $product->save();
         return $product->getLastAddedLinkId();
     }
 }