-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
157 additions
and
116 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/Sylius/Bundle/ApiBundle/EventSubscriber/CatalogPromotionEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/Sylius/Bundle/ApiBundle/spec/EventSubscriber/CatalogPromotionEventSubscriberSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
src/Sylius/Bundle/CoreBundle/Listener/CatalogPromotionConfigurationRemovedListener.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
...lius/Bundle/CoreBundle/spec/Listener/CatalogPromotionConfigurationRemovedListenerSpec.php
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
src/Sylius/Component/Promotion/Event/CatalogPromotionConfigurationRemoved.php
This file was deleted.
Oops, something went wrong.