<?php
namespace App\V4\EventSubscriber\Module\ModuleQuoteLine;
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Model\Module\Module;
use App\V4\DataPersister\AbstractWithoutRequestDataPersister;
use App\V4\Event\Module\ModuleDeactivationEvent;
use App\V4\Model\Listing\Listing;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ModuleQuoteLineDeactivationSubscriber implements EventSubscriberInterface
{
/**
* @var CollectionDataProviderInterface
*/
private $collectionDataProvider;
/**
* @var DataPersisterInterface
*/
private $dataPersister;
/**
* @param CollectionDataProviderInterface $collectionDataProvider
* @param DataPersisterInterface $dataPersister
*/
public function __construct(CollectionDataProviderInterface $collectionDataProvider, DataPersisterInterface $dataPersister)
{
$this->collectionDataProvider = $collectionDataProvider;
$this->dataPersister = $dataPersister;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
ModuleDeactivationEvent::NAME => 'onModuleActivation',
];
}
/**
* @param ModuleDeactivationEvent $event
*
* @return bool
*/
public function support(ModuleDeactivationEvent $event): bool
{
return Module::MODULE_QUOTE_LINE === $event->getModule()->getId();
}
/**
* @param ModuleDeactivationEvent $event
*
* @return void
*
* @throws ResourceClassNotSupportedException
*/
public function onModuleActivation(ModuleDeactivationEvent $event)
{
if (!$this->support($event)) {
return;
}
$listings = $this->collectionDataProvider->getCollection(Listing::class, null, [
'filters' => [
'code' => Listing::CODE_LISTING_QUOTE_SECTION,
'customerId' => $event->getCustomerId(),
],
]);
if (empty($listings)) {
return;
}
$this->dataPersister->remove($listings[0], [AbstractWithoutRequestDataPersister::CONTEXT_IS_V4 => true]);
}
}