<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Bundle\OrderManagement\EventSubscriber;
use Ibexa\Contracts\OrderManagement\Value\Struct\OrderCreateStruct;
use Ibexa\OrderManagement\Stock\StockManagerInterface;
use Ibexa\OrderManagement\Workflow\WorkflowCoordinator;
use Ibexa\OrderManagement\Workflow\WorkflowCoordinatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
final class StockReducingSubscriber implements EventSubscriberInterface
{
private WorkflowCoordinatorInterface $workflowCoordinator;
private StockManagerInterface $stockManager;
public function __construct(
WorkflowCoordinatorInterface $workflowCoordinator,
StockManagerInterface $stockManager
) {
$this->workflowCoordinator = $workflowCoordinator;
$this->stockManager = $stockManager;
}
public static function getSubscribedEvents(): array
{
return [
'workflow.entered' => 'onEntered',
];
}
public function onEntered(Event $event): void
{
$order = $event->getSubject();
if (!$order instanceof OrderCreateStruct) {
return;
}
$reduceStockParameter = $this->workflowCoordinator->getPlaceMetadataParameter(
$order,
$order->getStatus(),
WorkflowCoordinator::REDUCE_STOCK_METADATA_PARAMETER
);
if ($reduceStockParameter === true) {
$this->stockManager->reduce($order);
}
}
}