Skip to content

Commit

Permalink
Change the event to API events
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Sep 28, 2021
1 parent 2e189ec commit 547f3b6
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\EventSubscriber;

use ApiPlatform\Core\EventListener\EventPriorities;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Promotion\Event\CatalogPromotionUpdated;
use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface;
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 CatalogPromotionEventSubscriber implements EventSubscriberInterface
{
private MessageBusInterface $eventBus;

public function __construct(MessageBusInterface $eventBus)
{
$this->eventBus = $eventBus;
}

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

public function postWrite(ViewEvent $event): void
{
$entity = $event->getControllerResult();
$method = $event->getRequest()->getMethod();

if ($method === Request::METHOD_PUT || $method === Request::METHOD_PATCH) {
if ($entity instanceof CatalogPromotionInterface) {
$this->eventBus->dispatch(new CatalogPromotionUpdated($entity->getCode()));
}

if ($entity instanceof CatalogPromotionActionInterface) {
$this->eventBus->dispatch(new CatalogPromotionUpdated($entity->getCatalogPromotion()->getCode()));
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<tag name="kernel.event_subscriber" />
</service>

<service id="Sylius\Bundle\ApiBundle\EventSubscriber\CatalogPromotionEventSubscriber">
<argument type="service" id="sylius.event_bus" />
<tag name="kernel.event_subscriber" />
</service>

<service id="Sylius\Bundle\ApiBundle\EventSubscriber\KernelRequestEventSubscriber">
<argument>%sylius_api.enabled%</argument>
<argument>%sylius.security.new_api_route%</argument>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ApiBundle\EventSubscriber;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Core\Event\ProductVariantUpdated;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Promotion\Event\CatalogPromotionUpdated;
use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class CatalogPromotionEventSubscriberSpec extends ObjectBehavior
{
function let(MessageBusInterface $eventBus): void
{
$this->beConstructedWith($eventBus);
}

function it_dispatches_catalog_promotion_updated_after_writing_catalog_promotion(
MessageBusInterface $eventBus,
CatalogPromotionInterface $catalogPromotion,
HttpKernelInterface $kernel,
Request $request
): void {
$request->getMethod()->willReturn(Request::METHOD_PUT);

$catalogPromotion->getCode()->willReturn('Winter_sale');

$message = new CatalogPromotionUpdated('Winter_sale');
$eventBus->dispatch($message)->willReturn(new Envelope($message))->shouldBeCalled();

$this->postWrite(new ViewEvent(
$kernel->getWrappedObject(),
$request->getWrappedObject(),
HttpKernelInterface::MASTER_REQUEST,
$catalogPromotion->getWrappedObject()
));
}

function it_dispatches_catalog_promotion_updated_after_writing_catalog_promotion_action(
MessageBusInterface $eventBus,
CatalogPromotionActionInterface $catalogPromotionAction,
CatalogPromotionInterface $catalogPromotion,
HttpKernelInterface $kernel,
Request $request
): void {
$request->getMethod()->willReturn(Request::METHOD_PUT);

$catalogPromotionAction->getCatalogPromotion()->willReturn($catalogPromotion);
$catalogPromotion->getCode()->willReturn('Winter_sale');

$message = new CatalogPromotionUpdated('Winter_sale');
$eventBus->dispatch($message)->willReturn(new Envelope($message))->shouldBeCalled();

$this->postWrite(new ViewEvent(
$kernel->getWrappedObject(),
$request->getWrappedObject(),
HttpKernelInterface::MASTER_REQUEST,
$catalogPromotionAction->getWrappedObject()
));
}

function it_does_nothing_after_writing_other_entity(
MessageBusInterface $eventBus,
HttpKernelInterface $kernel,
Request $request
): void {
$eventBus->dispatch(Argument::any())->shouldNotBeCalled();

$this->postWrite(new ViewEvent(
$kernel->getWrappedObject(),
$request->getWrappedObject(),
HttpKernelInterface::MASTER_REQUEST,
new \stdClass()
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Sylius\Bundle\CoreBundle\EventListener;

use Doctrine\Persistence\Event\LifecycleEventArgs;
use Sylius\Component\Promotion\Event\CatalogPromotionConfigurationRemoved;
use Sylius\Component\Promotion\Event\CatalogPromotionUpdated;
use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionInterface;
Expand Down Expand Up @@ -50,13 +49,4 @@ public function postUpdate(LifecycleEventArgs $args): void
$this->eventBus->dispatch(new CatalogPromotionUpdated($entity->getCatalogPromotion()->getCode()));
}
}

public function postRemove(LifecycleEventArgs $args): void
{
$entity = $args->getObject();

if ($entity instanceof CatalogPromotionActionInterface) {
$this->eventBus->dispatch(new CatalogPromotionConfigurationRemoved());
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
<argument type="service" id="sylius.event_bus" />
<tag name="doctrine.event_listener" event="postPersist" connection="default" />
<tag name="doctrine.event_listener" event="postUpdate" connection="default" />
<tag name="doctrine.event_listener" event="postRemove" connection="default" />
</service>

<service id="Sylius\Bundle\CoreBundle\Listener\CatalogPromotionUpdateListener">
Expand All @@ -111,12 +110,6 @@
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface" />
<argument type="service" id="sylius.manager.channel_pricing" />
<tag name="messenger.message_handler" bus="sylius.event_bus" />
</service>

<service id="Sylius\Bundle\CoreBundle\Listener\CatalogPromotionConfigurationRemovedListener">
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionReprocessorInterface" />
<argument type="service" id="doctrine.orm.entity_manager" />
<tag name="messenger.message_handler" bus="sylius.event_bus" />
</service>
</services>
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 547f3b6

Please sign in to comment.