コード例 #1
0
 /**
  * @param string|int|float $sortOrder
  * @dataProvider getInvalidSortOrder
  * @expectedException \Magento\Framework\Exception\InputException
  * @expectedExceptionMessage Sort order must be a positive integer.
  */
 public function testIsValidThrowsExceptionIfSortOrderIsInvalid($sortOrder)
 {
     $sampleContentData = ['title' => 'Title', 'sort_order' => $sortOrder, 'sample_type' => 'file'];
     $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
     $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
     $this->validator->isValid($this->getSampleContentMock($sampleContentData));
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save($productSku, SampleContentInterface $sampleContent, $sampleId = null, $isGlobalScopeContent = false)
 {
     $product = $this->productRepository->get($productSku, true);
     if ($sampleId) {
         /** @var $sample \Magento\Downloadable\Model\Sample */
         $sample = $this->sampleFactory->create()->load($sampleId);
         if (!$sample->getId()) {
             throw new NoSuchEntityException(__('There is no downloadable sample with provided ID.'));
         }
         if ($sample->getProductId() != $product->getId()) {
             throw new InputException(__('Provided downloadable sample is not related to given product.'));
         }
         if (!$this->contentValidator->isValid($sampleContent)) {
             throw new InputException(__('Provided sample information is invalid.'));
         }
         if ($isGlobalScopeContent) {
             $product->setStoreId(0);
         }
         $title = $sampleContent->getTitle();
         if (empty($title)) {
             if ($isGlobalScopeContent) {
                 throw new InputException(__('Sample title cannot be empty.'));
             }
             // use title from GLOBAL scope
             $sample->setTitle(null);
         } else {
             $sample->setTitle($sampleContent->getTitle());
         }
         $sample->setProductId($product->getId())->setStoreId($product->getStoreId())->setSortOrder($sampleContent->getSortOrder())->save();
         return $sample->getId();
     } else {
         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($sampleContent)) {
             throw new InputException(__('Provided sample information is invalid.'));
         }
         if (!in_array($sampleContent->getSampleType(), ['url', 'file'])) {
             throw new InputException(__('Invalid sample type.'));
         }
         $title = $sampleContent->getTitle();
         if (empty($title)) {
             throw new InputException(__('Sample title cannot be empty.'));
         }
         $sampleData = ['sample_id' => 0, 'is_delete' => 0, 'type' => $sampleContent->getSampleType(), 'sort_order' => $sampleContent->getSortOrder(), 'title' => $sampleContent->getTitle()];
         if ($sampleContent->getSampleType() == 'file') {
             $sampleData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($sampleContent->getSampleFile(), 'sample')]);
         } else {
             $sampleData['sample_url'] = $sampleContent->getSampleUrl();
         }
         $downloadableData = ['sample' => [$sampleData]];
         $product->setDownloadableData($downloadableData);
         if ($isGlobalScopeContent) {
             $product->setStoreId(0);
         }
         $product->save();
         return $product->getLastAddedSampleId();
     }
 }
コード例 #3
0
ファイル: SampleRepository.php プロジェクト: opexsw/magento2
 /**
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param SampleInterface $sample
  * @param bool $isGlobalScopeContent
  * @return int
  * @throws InputException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function updateSample(\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent)
 {
     $sampleId = $sample->getId();
     /** @var $existingSample \Magento\Downloadable\Model\Sample */
     $existingSample = $this->sampleFactory->create()->load($sampleId);
     if (!$existingSample->getId()) {
         throw new NoSuchEntityException(__('There is no downloadable sample with provided ID.'));
     }
     if ($existingSample->getProductId() != $product->getId()) {
         throw new InputException(__('Provided downloadable sample is not related to given product.'));
     }
     $validateFileContent = $sample->getSampleFileContent() === null ? false : true;
     if (!$this->contentValidator->isValid($sample, $validateFileContent)) {
         throw new InputException(__('Provided sample information is invalid.'));
     }
     if ($isGlobalScopeContent) {
         $product->setStoreId(0);
     }
     $title = $sample->getTitle();
     if (empty($title)) {
         if ($isGlobalScopeContent) {
             throw new InputException(__('Sample title cannot be empty.'));
         }
         // use title from GLOBAL scope
         $existingSample->setTitle(null);
     } else {
         $existingSample->setTitle($sample->getTitle());
     }
     if ($sample->getSampleType() === 'file' && $sample->getSampleFileContent() === null) {
         $sample->setSampleFile($existingSample->getSampleFile());
     }
     $this->saveSample($product, $sample, $isGlobalScopeContent);
     return $existingSample->getId();
 }