Skip to content

Commit

Permalink
Add CatalogPromotionsSubscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Aug 23, 2021
1 parent 42147cf commit a86f298
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

<service id="Sylius\Bundle\PromotionBundle\Subscriber\CatalogPromotionSubscriber">
<argument type="service" id="sylius.event_bus" />
<tag name="kernel.event_subscriber" />
<tag name="doctrine.event_subscriber" connection="default" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@

namespace Sylius\Bundle\PromotionBundle\Subscriber;

use ApiPlatform\Core\EventListener\EventPriorities;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Sylius\Component\Promotion\Event\CatalogPromotionCreated;
use Sylius\Component\Promotion\Model\CatalogPromotionInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Messenger\MessageBusInterface;

final class CatalogPromotionSubscriber implements EventSubscriberInterface
Expand All @@ -31,22 +29,19 @@ public function __construct(MessageBusInterface $messageBus)
$this->messageBus = $messageBus;
}

public static function getSubscribedEvents()
public function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => ['dispatchCatalogPromotionCreated', EventPriorities::POST_WRITE],
];
return [Events::postPersist];
}

public function dispatchCatalogPromotionCreated(ViewEvent $event): void
public function postPersist(LifecycleEventArgs $args)
{
$catalogPromotion = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
$entity = $args->getObject();

if (!$catalogPromotion instanceof CatalogPromotionInterface || Request::METHOD_POST !== $method) {
if (!$entity instanceof CatalogPromotionInterface) {
return;
}

$this->messageBus->dispatch(new CatalogPromotionCreated($catalogPromotion->getCode()));
$this->messageBus->dispatch(new CatalogPromotionCreated($entity->getCode()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace spec\Sylius\Bundle\PromotionBundle\Subscriber;

use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use PhpSpec\ObjectBehavior;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;

final class CatalogPromotionSubscriberSpec extends ObjectBehavior
Expand Down

0 comments on commit a86f298

Please sign in to comment.