custom/plugins/CrswCleverReachOfficial/src/Subscriber/Extensions/RegisterExtension.php line 54

Open in your IDE?
  1. <?php /** @noinspection PhpUndefinedClassInspection */
  2. namespace Crsw\CleverReachOfficial\Subscriber\Extensions;
  3. use Crsw\CleverReachOfficial\Components\Utility\Bootstrap;
  4. use Crsw\CleverReachOfficial\Components\Utility\Initializer;
  5. use Crsw\CleverReachOfficial\Core\Infrastructure\Exceptions\BaseException;
  6. use Crsw\CleverReachOfficial\Core\Infrastructure\Logger\Logger;
  7. use Crsw\CleverReachOfficial\Service\BusinessLogic\Automation\AutomationService;
  8. use Crsw\CleverReachOfficial\Struct\AutomationData;
  9. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * Class RegisterExtension
  13.  *
  14.  * @package Crsw\CleverReachOfficial\Subscriber\Extensions
  15.  */
  16. class RegisterExtension implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var AutomationService
  20.      */
  21.     private $automationService;
  22.     /**
  23.      * RegisterExtension constructor.
  24.      *
  25.      * @param Initializer $initializer
  26.      * @param AutomationService $automationService
  27.      */
  28.     public function __construct(Initializer $initializerAutomationService $automationService)
  29.     {
  30.         Bootstrap::register();
  31.         $initializer->registerServices();
  32.         $this->automationService $automationService;
  33.     }
  34.     /**
  35.      * @inheritDoc
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoaded'
  41.         ];
  42.     }
  43.     /**
  44.      * Extends checkout register page.
  45.      *
  46.      * @param CheckoutRegisterPageLoadedEvent $event
  47.      */
  48.     public function onCheckoutRegisterPageLoaded(CheckoutRegisterPageLoadedEvent $event): void
  49.     {
  50.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  51.         try {
  52.             $automation $this->automationService->get($salesChannelId);
  53.             if ($automation && $automation->isActive()) {
  54.                 $event->getPage()
  55.                     ->addExtension('cleverreach', new AutomationData(['showCheckbox' => true]));
  56.                 return;
  57.             }
  58.         } catch (BaseException $e) {
  59.             Logger::logError('Failed to get automation data because: ' $e->getMessage(), 'Integration');
  60.         }
  61.         $event->getPage()->addExtension('cleverreach', new AutomationData(['showCheckbox' => false]));
  62.     }
  63. }