Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
SampleRepository Class Reference
Inheritance diagram for SampleRepository:
SampleRepositoryInterface

Public Member Functions

 __construct (ProductRepositoryInterface $productRepository, Type $downloadableType, SampleInterfaceFactory $sampleDataObjectFactory, ContentValidator $contentValidator, ContentUploaderInterface $fileContentUploader, EncoderInterface $jsonEncoder, SampleFactory $sampleFactory)
 
 save ( $sku, SampleInterface $sample, $isGlobalScopeContent=true)
 
- Public Member Functions inherited from SampleRepositoryInterface
 getList ($sku)
 
 getSamplesByProduct (\Magento\Catalog\Api\Data\ProductInterface $product)
 
 delete ($id)
 

Protected Member Functions

 buildSample ($resourceData)
 
 setBasicFields ($resourceData, $dataObject)
 
 saveSample (\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent)
 

Protected Attributes

 $productRepository
 
 $contentValidator
 
 $downloadableType
 
 $sampleFactory
 
 $sampleDataObjectFactory
 
 $fileContentUploader
 
 $jsonEncoder
 

Detailed Description

Class SampleRepository @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 28 of file SampleRepository.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ProductRepositoryInterface  $productRepository,
Type  $downloadableType,
SampleInterfaceFactory  $sampleDataObjectFactory,
ContentValidator  $contentValidator,
ContentUploaderInterface  $fileContentUploader,
EncoderInterface  $jsonEncoder,
SampleFactory  $sampleFactory 
)
Parameters
ProductRepositoryInterface$productRepository
Type$downloadableType
SampleInterfaceFactory$sampleDataObjectFactory
ContentValidator$contentValidator
ContentUploaderInterface$fileContentUploader
EncoderInterface$jsonEncoder
SampleFactory$sampleFactory

Definition at line 84 of file SampleRepository.php.

Member Function Documentation

◆ buildSample()

buildSample (   $resourceData)
protected

Build a sample data object

Parameters
\Magento\Downloadable\Model\Sample$resourceData
Returns
\Magento\Downloadable\Model\Sample

Definition at line 118 of file SampleRepository.php.

119  {
120  $sample = $this->sampleDataObjectFactory->create();
121  $this->setBasicFields($resourceData, $sample);
122  return $sample;
123  }

◆ save()

save (   $sku,
SampleInterface  $sample,
  $isGlobalScopeContent = true 
)

Update downloadable sample of the given product

Parameters
string$sku
\Magento\Downloadable\Api\Data\SampleInterface$sample
bool$isGlobalScopeContent
Returns
int
Exceptions
InputException
NoSuchEntityException

Implements SampleRepositoryInterface.

Definition at line 175 of file SampleRepository.php.

179  {
180  $product = $this->productRepository->get($sku, true);
181 
182  $sampleId = $sample->getId();
183  if ($sampleId) {
184  return $this->updateSample($product, $sample, $isGlobalScopeContent);
185  } else {
186  if ($product->getTypeId() !== Type::TYPE_DOWNLOADABLE) {
187  throw new InputException(
188  __('The product needs to be the downloadable type. Verify the product and try again.')
189  );
190  }
191  $validateSampleContent = !($sample->getSampleType() === 'file' && $sample->getSampleFile());
192  if (!$this->contentValidator->isValid($sample, $validateSampleContent)) {
193  throw new InputException(
194  __('The sample information is invalid. Verify the information and try again.')
195  );
196  }
197 
198  if (!in_array($sample->getSampleType(), ['url', 'file'], true)) {
199  throw new InputException(__('The sample type is invalid. Verify the sample type and try again.'));
200  }
201 
202  $title = $sample->getTitle();
203  if (empty($title)) {
204  throw new InputException(__('The sample title is empty. Enter the title and try again.'));
205  }
206 
207  return $this->saveSample($product, $sample, $isGlobalScopeContent);
208  }
209  }
$title
Definition: default.phtml:14
__()
Definition: __.php:13
saveSample(\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent)

◆ saveSample()

saveSample ( \Magento\Catalog\Api\Data\ProductInterface  $product,
SampleInterface  $sample,
  $isGlobalScopeContent 
)
protected
Parameters
\Magento\Catalog\Api\Data\ProductInterface$product
SampleInterface$sample
bool$isGlobalScopeContent
Returns
int

Definition at line 217 of file SampleRepository.php.

221  {
222  $sampleData = [
223  'sample_id' => (int)$sample->getId(),
224  'is_delete' => 0,
225  'type' => $sample->getSampleType(),
226  'sort_order' => $sample->getSortOrder(),
227  'title' => $sample->getTitle(),
228  ];
229 
230  if ($sample->getSampleType() === 'file' && $sample->getSampleFile() === null) {
231  $sampleData['file'] = $this->jsonEncoder->encode(
232  [
233  $this->fileContentUploader->upload($sample->getSampleFileContent(), 'sample'),
234  ]
235  );
236  } elseif ($sample->getSampleType() === 'url') {
237  $sampleData['sample_url'] = $sample->getSampleUrl();
238  } else {
239  //existing file
240  $sampleData['file'] = $this->jsonEncoder->encode(
241  [
242  [
243  'file' => $sample->getSampleFile(),
244  'status' => 'old',
245  ],
246  ]
247  );
248  }
249 
250  $downloadableData = ['sample' => [$sampleData]];
251 
252  if ($isGlobalScopeContent) {
253  $product->setStoreId(0);
254  }
255  $this->getSampleTypeHandler()->save($product, $downloadableData);
256  return $product->getLastAddedSampleId();
257  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ setBasicFields()

setBasicFields (   $resourceData,
  $dataObject 
)
protected

Subroutine for buildLink and buildSample

Parameters
\Magento\Downloadable\Model\Link | \Magento\Downloadable\Model\Sample$resourceData
\Magento\Downloadable\Api\Data\LinkInterface | \Magento\Downloadable\Api\Data\SampleInterface$dataObject
Returns
null

Definition at line 132 of file SampleRepository.php.

133  {
134  $dataObject->setId($resourceData->getId());
135  $storeTitle = $resourceData->getStoreTitle();
136  $title = $resourceData->getTitle();
137  if (!empty($storeTitle)) {
138  $dataObject->setTitle($storeTitle);
139  } else {
140  $dataObject->setTitle($title);
141  }
142  $dataObject->setSortOrder($resourceData->getSortOrder());
143  $dataObject->setSampleType($resourceData->getSampleType());
144  $dataObject->setSampleFile($resourceData->getSampleFile());
145  $dataObject->setSampleUrl($resourceData->getSampleUrl());
146  }
$title
Definition: default.phtml:14

Field Documentation

◆ $contentValidator

$contentValidator
protected

Definition at line 38 of file SampleRepository.php.

◆ $downloadableType

$downloadableType
protected

Definition at line 43 of file SampleRepository.php.

◆ $fileContentUploader

$fileContentUploader
protected

Definition at line 58 of file SampleRepository.php.

◆ $jsonEncoder

$jsonEncoder
protected

Definition at line 63 of file SampleRepository.php.

◆ $productRepository

$productRepository
protected

Definition at line 33 of file SampleRepository.php.

◆ $sampleDataObjectFactory

$sampleDataObjectFactory
protected

Definition at line 53 of file SampleRepository.php.

◆ $sampleFactory

$sampleFactory
protected

Definition at line 48 of file SampleRepository.php.


The documentation for this class was generated from the following file: