src/Module/Cms/Subscriber/CheckoutSlotSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Module\Cms\Subscriber;
  4. use App\Module\Cms\Provider\CmsContentProvider;
  5. use App\Store\StoreContext;
  6. use App\Module\ShortSimpleCheckout\Event\View\BaseViewEvent;
  7. use App\Module\ShortSimpleCheckout\Events;
  8. use App\Module\ShortSimpleCheckout\Exception\VariableAlreadyDefinedException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11. * Class CheckoutSlotSubscriber
  12. *
  13. * @author André Varelmann <andre.varelmann@bestit-online.de>
  14. * @package App\Module\Cms\Subscriber
  15. */
  16. class CheckoutSlotSubscriber implements EventSubscriberInterface
  17. {
  18. private CmsContentProvider $provider;
  19. private StoreContext $storeContext;
  20. /**
  21. * @param CmsContentProvider $provider
  22. * @param StoreContext $storeContext
  23. */
  24. public function __construct(CmsContentProvider $provider, StoreContext $storeContext)
  25. {
  26. $this->provider = $provider;
  27. $this->storeContext = $storeContext;
  28. }
  29. /**
  30. * @return array|string[]
  31. */
  32. public static function getSubscribedEvents(): array
  33. {
  34. return [
  35. Events::CART_INDEX_DISPATCH => 'addCheckoutSlots',
  36. Events::SHIPPING_PAYMENT_INDEX_DISPATCH => 'addCheckoutSlots',
  37. Events::FINISH_INDEX_DISPATCH => 'addFinishSlot',
  38. ];
  39. }
  40. /**
  41. * @param BaseViewEvent $event
  42. *
  43. * @throws VariableAlreadyDefinedException
  44. *
  45. * @return void
  46. */
  47. public function addCheckoutSlots(BaseViewEvent $event): void
  48. {
  49. $cms = $this->provider->getContentBySlugForCurrentPage('checkout');
  50. if ($cms !== null) {
  51. $event->addVariable('slots', $cms->content);
  52. }
  53. }
  54. /**
  55. * Add cms slots for finish page. Remove order id from url first
  56. *
  57. * @param BaseViewEvent $event
  58. * @throws VariableAlreadyDefinedException
  59. *
  60. * @return void
  61. */
  62. public function addFinishSlot(BaseViewEvent $event): void
  63. {
  64. $localeInfo = $this->storeContext->getLocaleInfo();
  65. // remove order id from finish url (/checkout/finish/{orderId}
  66. $url = explode('/', $localeInfo->getPath());
  67. array_pop($url);
  68. $cms = $this->provider->getContentBySlug(
  69. $this->storeContext->getAlias(),
  70. $localeInfo->getCurrentLocale(),
  71. implode('/', $url),
  72. 'checkout'
  73. );
  74. if ($cms !== null) {
  75. $event->addVariable('slots', $cms->content);
  76. }
  77. }
  78. }