vendor/ibexa/order-management/src/bundle/EventSubscriber/StockReducingSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\OrderManagement\EventSubscriber;
  8. use Ibexa\Contracts\OrderManagement\Value\Struct\OrderCreateStruct;
  9. use Ibexa\OrderManagement\Stock\StockManagerInterface;
  10. use Ibexa\OrderManagement\Workflow\WorkflowCoordinator;
  11. use Ibexa\OrderManagement\Workflow\WorkflowCoordinatorInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Workflow\Event\Event;
  14. final class StockReducingSubscriber implements EventSubscriberInterface
  15. {
  16. private WorkflowCoordinatorInterface $workflowCoordinator;
  17. private StockManagerInterface $stockManager;
  18. public function __construct(
  19. WorkflowCoordinatorInterface $workflowCoordinator,
  20. StockManagerInterface $stockManager
  21. ) {
  22. $this->workflowCoordinator = $workflowCoordinator;
  23. $this->stockManager = $stockManager;
  24. }
  25. public static function getSubscribedEvents(): array
  26. {
  27. return [
  28. 'workflow.entered' => 'onEntered',
  29. ];
  30. }
  31. public function onEntered(Event $event): void
  32. {
  33. $order = $event->getSubject();
  34. if (!$order instanceof OrderCreateStruct) {
  35. return;
  36. }
  37. $reduceStockParameter = $this->workflowCoordinator->getPlaceMetadataParameter(
  38. $order,
  39. $order->getStatus(),
  40. WorkflowCoordinator::REDUCE_STOCK_METADATA_PARAMETER
  41. );
  42. if ($reduceStockParameter === true) {
  43. $this->stockManager->reduce($order);
  44. }
  45. }
  46. }