Esempio n. 1
0
 /**
  * @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();
 }
Esempio n. 2
0
 /**
  * Add Sample File info into $sampleData
  *
  * @param array $sampleData
  * @param SampleInterface $sample
  * @return array
  */
 protected function addSampleFile(array $sampleData, SampleInterface $sample)
 {
     $sampleFile = $sample->getSampleFile();
     if ($sampleFile) {
         $file = $this->downloadableFile->getFilePath($this->sampleModel->getBasePath(), $sampleFile);
         if ($this->downloadableFile->ensureFileInFilesystem($file)) {
             $sampleData['file'][0] = ['file' => $sampleFile, 'name' => $this->downloadableFile->getFileFromPathFile($sampleFile), 'size' => $this->downloadableFile->getFileSize($file), 'status' => 'old', 'url' => $this->urlBuilder->addSessionParam()->getUrl('adminhtml/downloadable_product_edit/sample', ['id' => $sample->getId(), '_secure' => true])];
         }
     }
     return $sampleData;
 }