DeleteHandler.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\Link;
  7. use Magento\Downloadable\Api\LinkRepositoryInterface as LinkRepository;
  8. use Magento\Framework\EntityManager\Operation\ExtensionInterface;
  9. /**
  10. * Class DeleteHandler
  11. */
  12. class DeleteHandler implements ExtensionInterface
  13. {
  14. /**
  15. * @var LinkRepository
  16. */
  17. protected $linkRepository;
  18. /**
  19. * @param LinkRepository $linkRepository
  20. */
  21. public function __construct(LinkRepository $linkRepository)
  22. {
  23. $this->linkRepository = $linkRepository;
  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. if ($entity->getTypeId() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
  34. return $entity;
  35. }
  36. /** @var \Magento\Catalog\Api\Data\ProductInterface $entity */
  37. foreach ($this->linkRepository->getList($entity->getSku()) as $link) {
  38. $this->linkRepository->delete($link->getId());
  39. }
  40. return $entity;
  41. }
  42. }