CreateHandler.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\Sample;
  7. use Magento\Downloadable\Api\SampleRepositoryInterface as SampleRepository;
  8. use Magento\Downloadable\Model\Product\Type;
  9. use Magento\Framework\EntityManager\Operation\ExtensionInterface;
  10. /**
  11. * Class CreateHandler
  12. */
  13. class CreateHandler implements ExtensionInterface
  14. {
  15. /**
  16. * @var SampleRepository
  17. */
  18. protected $sampleRepository;
  19. /**
  20. * @param SampleRepository $sampleRepository
  21. */
  22. public function __construct(SampleRepository $sampleRepository)
  23. {
  24. $this->sampleRepository = $sampleRepository;
  25. }
  26. /**
  27. * @param object $entity
  28. * @param array $arguments
  29. * @return \Magento\Catalog\Api\Data\ProductInterface|object
  30. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  31. */
  32. public function execute($entity, $arguments = [])
  33. {
  34. /** @var $entity \Magento\Catalog\Api\Data\ProductInterface */
  35. if ($entity->getTypeId() != Type::TYPE_DOWNLOADABLE) {
  36. return $entity;
  37. }
  38. /** @var \Magento\Downloadable\Api\Data\SampleInterface[] $samples */
  39. $samples = $entity->getExtensionAttributes()->getDownloadableProductSamples() ?: [];
  40. foreach ($samples as $sample) {
  41. $sample->setId(null);
  42. $this->sampleRepository->save($entity->getSku(), $sample, !(bool)$entity->getStoreId());
  43. }
  44. return $entity;
  45. }
  46. }