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\Link;
  7. use Magento\Downloadable\Api\LinkRepositoryInterface as LinkRepository;
  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 LinkRepository
  17. */
  18. protected $linkRepository;
  19. /**
  20. * @param LinkRepository $linkRepository
  21. */
  22. public function __construct(LinkRepository $linkRepository)
  23. {
  24. $this->linkRepository = $linkRepository;
  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\LinkInterface[] $links */
  39. $links = $entity->getExtensionAttributes()->getDownloadableProductLinks() ?: [];
  40. foreach ($links as $link) {
  41. $link->setId(null);
  42. $this->linkRepository->save($entity->getSku(), $link, !(bool)$entity->getStoreId());
  43. }
  44. return $entity;
  45. }
  46. }