vendor/ibexa/product-catalog/src/bundle/EventSubscriber/DeleteProductSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\ProductCatalog\EventSubscriber;
  8. use Ibexa\Contracts\Core\Repository\Events\Content\BeforeDeleteContentEvent;
  9. use Ibexa\Contracts\ProductCatalog\Local\LocalProductServiceInterface;
  10. use Ibexa\Contracts\ProductCatalog\ProductAvailabilityServiceInterface;
  11. use Ibexa\Contracts\ProductCatalog\ProductPriceServiceInterface;
  12. use Ibexa\Contracts\ProductCatalog\Values\Price\Delete\Struct\ProductPriceDeleteStruct;
  13. use Ibexa\Contracts\ProductCatalog\Values\ProductInterface;
  14. use Ibexa\Core\Repository\ContentService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. final class DeleteProductSubscriber implements EventSubscriberInterface
  17. {
  18. private ProductPriceServiceInterface $priceService;
  19. private ProductAvailabilityServiceInterface $productAvailabilityService;
  20. private ContentService $contentService;
  21. private LocalProductServiceInterface $localProductService;
  22. public function __construct(
  23. ProductPriceServiceInterface $priceService,
  24. ProductAvailabilityServiceInterface $productAvailabilityService,
  25. LocalProductServiceInterface $localProductService,
  26. ContentService $contentService
  27. ) {
  28. $this->priceService = $priceService;
  29. $this->productAvailabilityService = $productAvailabilityService;
  30. $this->localProductService = $localProductService;
  31. $this->contentService = $contentService;
  32. }
  33. public static function getSubscribedEvents(): array
  34. {
  35. return [
  36. BeforeDeleteContentEvent::class => 'onBeforeDeleteContent',
  37. ];
  38. }
  39. public function onBeforeDeleteContent(BeforeDeleteContentEvent $event): void
  40. {
  41. // works in the context of `sudo` method
  42. $contentInfo = $event->getContentInfo();
  43. $content = $this->contentService->loadContent($contentInfo->id);
  44. if (!$this->localProductService->isProduct($content)) {
  45. return;
  46. }
  47. $product = $this->localProductService->getProductFromContent($content);
  48. if ($product->isBaseProduct()) {
  49. $this->deletePricesForVariants($product);
  50. }
  51. $this->deletePrices($product->getCode());
  52. $this->productAvailabilityService->deleteProductAvailability($product);
  53. }
  54. private function deletePricesForVariants(ProductInterface $baseProduct): void
  55. {
  56. foreach ($this->localProductService->findProductVariants($baseProduct) as $variant) {
  57. $this->deletePrices($variant->getCode());
  58. }
  59. }
  60. private function deletePrices(string $productCode): void
  61. {
  62. $prices = $this->priceService->findPricesByProductCode($productCode);
  63. foreach ($prices as $price) {
  64. $struct = new ProductPriceDeleteStruct($price);
  65. $this->priceService->deletePrice($struct);
  66. }
  67. }
  68. }