-
-
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.
feature #13131 [CatalogPromotions][API] Reapply promotions after acti…
…on removal (arti0090) This PR was merged into the 1.11-dev branch. Discussion ---------- | Q | A | --------------- | ----- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | License | MIT Commits ------- 76641e8 [CatalogPromotions][API] Reapply promotions after action removal 074f306 Remove apply from removing listener 4780fa9 Remove apply from removing listener 0203c74 Refactor duplicated code to new class 2e189ec Rebase, commit before change to Event Subscribers 547f3b6 Change the event to API events 7d8b2f1 Remove unneded logic and hcange method name
- Loading branch information
Showing
12 changed files
with
290 additions
and
52 deletions.
There are no files selected for viewing
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
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
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
<?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 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(); | ||
|
||
if (!$entity instanceof CatalogPromotionInterface) { | ||
return; | ||
} | ||
|
||
$method = $event->getRequest()->getMethod(); | ||
|
||
if ($method === Request::METHOD_PUT || $method === Request::METHOD_PATCH) { | ||
$this->eventBus->dispatch(new CatalogPromotionUpdated($entity->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
71 changes: 71 additions & 0 deletions
71
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,71 @@ | ||
<?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_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
44 changes: 44 additions & 0 deletions
44
src/Sylius/Bundle/CoreBundle/Processor/AllCatalogPromotionsProcessor.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,44 @@ | ||
<?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\CoreBundle\Processor; | ||
|
||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
|
||
final class AllCatalogPromotionsProcessor implements AllCatalogPromotionsProcessorInterface | ||
{ | ||
private CatalogPromotionClearerInterface $catalogPromotionClearer; | ||
|
||
private CatalogPromotionProcessorInterface $catalogPromotionProcessor; | ||
|
||
private RepositoryInterface $catalogPromotionRepository; | ||
|
||
public function __construct( | ||
CatalogPromotionClearerInterface $catalogPromotionClearer, | ||
CatalogPromotionProcessorInterface $catalogPromotionProcessor, | ||
RepositoryInterface $catalogPromotionRepository | ||
) { | ||
$this->catalogPromotionClearer = $catalogPromotionClearer; | ||
$this->catalogPromotionProcessor = $catalogPromotionProcessor; | ||
$this->catalogPromotionRepository = $catalogPromotionRepository; | ||
} | ||
|
||
public function process(): void | ||
{ | ||
$this->catalogPromotionClearer->clear(); | ||
|
||
foreach ($this->catalogPromotionRepository->findAll() as $catalogPromotion) { | ||
$this->catalogPromotionProcessor->process($catalogPromotion); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Sylius/Bundle/CoreBundle/Processor/AllCatalogPromotionsProcessorInterface.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,19 @@ | ||
<?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\CoreBundle\Processor; | ||
|
||
interface AllCatalogPromotionsProcessorInterface | ||
{ | ||
public function process(): void; | ||
} |
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
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
Oops, something went wrong.