DeleteHandler.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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\Framework\EntityManager\Operation\ExtensionInterface;
  9. /**
  10. * Class DeleteHandler
  11. */
  12. class DeleteHandler implements ExtensionInterface
  13. {
  14. /**
  15. * @var SampleRepository
  16. */
  17. protected $sampleRepository;
  18. /**
  19. * @param SampleRepository $sampleRepository
  20. */
  21. public function __construct(SampleRepository $sampleRepository)
  22. {
  23. $this->sampleRepository = $sampleRepository;
  24. }
  25. /**
  26. * @param object $entity
  27. * @param array $arguments
  28. * @return \Magento\Catalog\Api\Data\ProductInterface|object
  29. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  30. */
  31. public function execute($entity, $arguments = [])
  32. {
  33. /** @var $entity \Magento\Catalog\Api\Data\ProductInterface */
  34. if ($entity->getTypeId() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
  35. return $entity;
  36. }
  37. /** @var \Magento\Catalog\Api\Data\ProductInterface $entity */
  38. foreach ($this->sampleRepository->getList($entity->getSku()) as $sample) {
  39. $this->sampleRepository->delete($sample->getId());
  40. }
  41. return $entity;
  42. }
  43. }