<?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\Checkout\EventSubscriber;
use Ibexa\Contracts\Cart\CartServiceInterface;
use Ibexa\Contracts\Checkout\Value\CheckoutInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\CompletedEvent;
final class LastCheckoutStepSubscriber implements EventSubscriberInterface
{
private CartServiceInterface $cartService;
public function __construct(
CartServiceInterface $cartService
) {
$this->cartService = $cartService;
}
public static function getSubscribedEvents(): array
{
return ['workflow.completed' => 'deleteCart'];
}
public function deleteCart(CompletedEvent $event): void
{
$checkout = $event->getSubject();
if (!$checkout instanceof CheckoutInterface) {
return;
}
$placeMetadata = $event->getWorkflow()->getMetadataStore()->getPlaceMetadata($checkout->getStatus());
if (!array_key_exists('is_final_step', $placeMetadata) || $placeMetadata['is_final_step'] !== true) {
return;
}
$this->cartService->deleteCart(
$this->cartService->getCart($checkout->getCartIdentifier())
);
}
}