<?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\ModuleActivationEvent;
use App\V4\Model\Listing\Listing;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ModuleQuoteLineActivationSubscriber 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 [
ModuleActivationEvent::NAME => 'onModuleActivation',
];
}
/**
* @param ModuleActivationEvent $event
*
* @return bool
*/
public function support(ModuleActivationEvent $event): bool
{
return Module::MODULE_QUOTE_LINE === $event->getModule()->getId();
}
/**
* @param ModuleActivationEvent $event
*
* @return void
*
* @throws ResourceClassNotSupportedException
*/
public function onModuleActivation(ModuleActivationEvent $event)
{
if (!$this->support($event)) {
return;
}
$listings = $this->collectionDataProvider->getCollection(Listing::class, null, [
'filters' => [
'code' => Listing::CODE_LISTING_QUOTE_SECTION,
'customerId' => $event->getCustomerId(),
],
'transformResponse' => false,
]);
if (!empty($listings)) {
return;
}
$listingQuoteSections = (new Listing())
->setCustomerId($event->getCustomerId())
->setIsActive(true)
->setCode(Listing::CODE_LISTING_QUOTE_SECTION)
->setName(Listing::NAME_LISTING_QUOTE_SECTION)
;
$this->dataPersister->persist($listingQuoteSections, [AbstractWithoutRequestDataPersister::CONTEXT_IS_V4 => true]);
}
}