Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SampleRepository.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Downloadable\Api\Data\SampleInterfaceFactory;
23 
29 {
33  protected $productRepository;
34 
38  protected $contentValidator;
39 
43  protected $downloadableType;
44 
48  protected $sampleFactory;
49 
54 
59 
63  protected $jsonEncoder;
64 
68  private $sampleTypeHandler;
69 
73  private $metadataPool;
74 
84  public function __construct(
87  SampleInterfaceFactory $sampleDataObjectFactory,
91  SampleFactory $sampleFactory
92  ) {
93  $this->productRepository = $productRepository;
94  $this->downloadableType = $downloadableType;
95  $this->contentValidator = $contentValidator;
96  $this->fileContentUploader = $fileContentUploader;
97  $this->jsonEncoder = $jsonEncoder;
98  $this->sampleFactory = $sampleFactory;
99  $this->sampleDataObjectFactory = $sampleDataObjectFactory;
100  }
101 
105  public function getList($sku)
106  {
108  $product = $this->productRepository->get($sku);
109  return $this->getSamplesByProduct($product);
110  }
111 
118  protected function buildSample($resourceData)
119  {
120  $sample = $this->sampleDataObjectFactory->create();
121  $this->setBasicFields($resourceData, $sample);
122  return $sample;
123  }
124 
132  protected function setBasicFields($resourceData, $dataObject)
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  }
147 
154  public function getSamplesByProduct(\Magento\Catalog\Api\Data\ProductInterface $product)
155  {
156  $sampleList = [];
157  $samples = $this->downloadableType->getSamples($product);
159  foreach ($samples as $sample) {
160  $sampleList[] = $this->buildSample($sample);
161  }
162  return $sampleList;
163  }
164 
175  public function save(
176  $sku,
177  SampleInterface $sample,
178  $isGlobalScopeContent = true
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  }
210 
217  protected function saveSample(
218  \Magento\Catalog\Api\Data\ProductInterface $product,
219  SampleInterface $sample,
220  $isGlobalScopeContent
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  }
258 
269  protected function updateSample(
270  \Magento\Catalog\Api\Data\ProductInterface $product,
271  SampleInterface $sample,
272  $isGlobalScopeContent
273  ) {
274  $sampleId = $sample->getId();
276  $existingSample = $this->sampleFactory->create()->load($sampleId);
277 
278  if (!$existingSample->getId()) {
279  throw new NoSuchEntityException(
280  __('No downloadable sample with the provided ID was found. Verify the ID and try again.')
281  );
282  }
283  $linkFieldValue = $product->getData(
284  $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()
285  );
286  if ($existingSample->getProductId() != $linkFieldValue) {
287  throw new InputException(
288  __("The downloadable sample isn't related to the product. Verify the link and try again.")
289  );
290  }
291 
292  $validateFileContent = $sample->getSampleFileContent() === null ? false : true;
293  if (!$this->contentValidator->isValid($sample, $validateFileContent)) {
294  throw new InputException(__('The sample information is invalid. Verify the information and try again.'));
295  }
296  if ($isGlobalScopeContent) {
297  $product->setStoreId(0);
298  }
299 
300  $title = $sample->getTitle();
301  if (empty($title)) {
302  if ($isGlobalScopeContent) {
303  throw new InputException(__('The sample title is empty. Enter the title and try again.'));
304  }
305  // use title from GLOBAL scope
306  $existingSample->setTitle(null);
307  } else {
308  $existingSample->setTitle($sample->getTitle());
309  }
310 
311  if ($sample->getSampleType() === 'file' && $sample->getSampleFileContent() === null) {
312  $sample->setSampleFile($existingSample->getSampleFile());
313  }
314  $this->saveSample($product, $sample, $isGlobalScopeContent);
315  return $existingSample->getId();
316  }
317 
321  public function delete($id)
322  {
324  $sample = $this->sampleFactory->create()->load($id);
325  if (!$sample->getId()) {
326  throw new NoSuchEntityException(
327  __('No downloadable sample with the provided ID was found. Verify the ID and try again.')
328  );
329  }
330  try {
331  $sample->delete();
332  } catch (\Exception $exception) {
333  throw new StateException(__('The sample with "%1" ID can\'t be deleted.', $sample->getId()), $exception);
334  }
335  return true;
336  }
337 
344  private function getMetadataPool()
345  {
346  if (!$this->metadataPool) {
347  $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
348  }
349 
350  return $this->metadataPool;
351  }
352 
359  private function getSampleTypeHandler()
360  {
361  if (!$this->sampleTypeHandler) {
362  $this->sampleTypeHandler = ObjectManager::getInstance()->get(SampleHandler::class);
363  }
364 
365  return $this->sampleTypeHandler;
366  }
367 }
$title
Definition: default.phtml:14
getSamplesByProduct(\Magento\Catalog\Api\Data\ProductInterface $product)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
return false
Definition: gallery.phtml:36
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
__construct(ProductRepositoryInterface $productRepository, Type $downloadableType, SampleInterfaceFactory $sampleDataObjectFactory, ContentValidator $contentValidator, ContentUploaderInterface $fileContentUploader, EncoderInterface $jsonEncoder, SampleFactory $sampleFactory)
saveSample(\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent)
save( $sku, SampleInterface $sample, $isGlobalScopeContent=true)