src/V4/EventSubscriber/Module/ModuleQuoteLine/ModuleQuoteLineDeactivationSubscriber.php line 63

Open in your IDE?
  1. <?php
  2. namespace App\V4\EventSubscriber\Module\ModuleQuoteLine;
  3. use ApiPlatform\Core\DataPersister\DataPersisterInterface;
  4. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  5. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  6. use App\Model\Module\Module;
  7. use App\V4\DataPersister\AbstractWithoutRequestDataPersister;
  8. use App\V4\Event\Module\ModuleDeactivationEvent;
  9. use App\V4\Model\Listing\Listing;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ModuleQuoteLineDeactivationSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var CollectionDataProviderInterface
  15.      */
  16.     private $collectionDataProvider;
  17.     /**
  18.      * @var DataPersisterInterface
  19.      */
  20.     private $dataPersister;
  21.     /**
  22.      * @param CollectionDataProviderInterface $collectionDataProvider
  23.      * @param DataPersisterInterface          $dataPersister
  24.      */
  25.     public function __construct(CollectionDataProviderInterface $collectionDataProviderDataPersisterInterface $dataPersister)
  26.     {
  27.         $this->collectionDataProvider $collectionDataProvider;
  28.         $this->dataPersister $dataPersister;
  29.     }
  30.     /**
  31.      * @return string[]
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             ModuleDeactivationEvent::NAME => 'onModuleActivation',
  37.         ];
  38.     }
  39.     /**
  40.      * @param ModuleDeactivationEvent $event
  41.      *
  42.      * @return bool
  43.      */
  44.     public function support(ModuleDeactivationEvent $event): bool
  45.     {
  46.         return Module::MODULE_QUOTE_LINE === $event->getModule()->getId();
  47.     }
  48.     /**
  49.      * @param ModuleDeactivationEvent $event
  50.      *
  51.      * @return void
  52.      *
  53.      * @throws ResourceClassNotSupportedException
  54.      */
  55.     public function onModuleActivation(ModuleDeactivationEvent $event)
  56.     {
  57.         if (!$this->support($event)) {
  58.             return;
  59.         }
  60.         $listings $this->collectionDataProvider->getCollection(Listing::class, null, [
  61.             'filters' => [
  62.                 'code' => Listing::CODE_LISTING_QUOTE_SECTION,
  63.                 'customerId' => $event->getCustomerId(),
  64.             ],
  65.         ]);
  66.         if (empty($listings)) {
  67.             return;
  68.         }
  69.         $this->dataPersister->remove($listings[0], [AbstractWithoutRequestDataPersister::CONTEXT_IS_V4 => true]);
  70.     }
  71. }