vendor/ibexa/checkout/src/bundle/EventSubscriber/CheckoutWorkflowInitializeStepSubscriber.php line 51

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\Checkout\EventSubscriber;
  8. use Ibexa\Checkout\Exception\NotFoundException;
  9. use Ibexa\Contracts\Cart\Event\AddEntryEvent;
  10. use Ibexa\Contracts\Cart\Event\MergeCartsEvent;
  11. use Ibexa\Contracts\Cart\Event\RemoveEntryEvent;
  12. use Ibexa\Contracts\Cart\Value\CartInterface;
  13. use Ibexa\Contracts\Checkout\CheckoutServiceInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. final class CheckoutWorkflowInitializeStepSubscriber implements EventSubscriberInterface
  16. {
  17. private CheckoutServiceInterface $checkoutService;
  18. private string $checkoutName;
  19. private string $initializeStep;
  20. public function __construct(
  21. CheckoutServiceInterface $checkoutService,
  22. string $checkoutName,
  23. string $initializeStep
  24. ) {
  25. $this->checkoutService = $checkoutService;
  26. $this->checkoutName = $checkoutName;
  27. $this->initializeStep = $initializeStep;
  28. }
  29. public static function getSubscribedEvents(): array
  30. {
  31. return [
  32. AddEntryEvent::class => ['onAddEntry', -100],
  33. RemoveEntryEvent::class => ['onRemoveEntry', -100],
  34. MergeCartsEvent::class => ['onMergeCarts', -100],
  35. ];
  36. }
  37. public function onAddEntry(AddEntryEvent $event): void
  38. {
  39. $this->goToInitializeStep($event->getCartResult());
  40. }
  41. public function onRemoveEntry(RemoveEntryEvent $event): void
  42. {
  43. $this->goToInitializeStep($event->getCartResult());
  44. }
  45. public function onMergeCarts(MergeCartsEvent $event): void
  46. {
  47. $this->goToInitializeStep($event->getResultCart());
  48. }
  49. private function goToInitializeStep(CartInterface $cart): void
  50. {
  51. try {
  52. $checkout = $this->checkoutService->getCheckoutForCart($cart);
  53. } catch (NotFoundException $exception) {
  54. return;
  55. }
  56. if ($this->checkoutName !== $checkout->getCheckoutName()) {
  57. return;
  58. }
  59. $this->checkoutService->updateCheckout(
  60. $checkout,
  61. $this->checkoutService->newCheckoutUpdateStruct(
  62. $this->initializeStep,
  63. $checkout->getContext()->toArray()
  64. )
  65. );
  66. }
  67. }