custom/plugins/VRPaymentVRPay/src/Subscriber/AccountSubscriber.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace VRPayment\VRPay\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  12. use VRPayment\VRPay\Core\Checkout\Customer\CustomerRegistrationsExtension;
  13. /**
  14.  * Class AccountSubscriber
  15.  * When the account payments page was loaded, we inject our saved registrations for the current customer into the page
  16.  *
  17.  * @package VRPayment\VRPay\Subscriber
  18.  */
  19. class AccountSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var SystemConfigService $systemConfigService
  23.      */
  24.     private $systemConfigService;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $registrationsRepository;
  29.     /**
  30.      * @return array
  31.      */
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             AccountPaymentMethodPageLoadedEvent::class => 'onAccountPaymentPageLoaded'
  36.         ];
  37.     }
  38.     public function __construct(SystemConfigService $systemConfigServiceEntityRepositoryInterface $registrationsRepository)
  39.     {
  40.         $this->systemConfigService $systemConfigService;
  41.         $this->registrationsRepository $registrationsRepository;
  42.     }
  43.     /**
  44.      * called when the account payments page was loaded.
  45.      * Reads all registrations from the db and displays it to the customer
  46.      *
  47.      * @param AccountPaymentMethodPageLoadedEvent $event
  48.      */
  49.     public function onAccountPaymentPageLoaded(AccountPaymentMethodPageLoadedEvent $event)
  50.     {
  51.         $enableRegistrations $this->systemConfigService->get(
  52.             'VRPay.config.saveRegistrations',
  53.             $event->getSalesChannelContext()->getSalesChannel()->getId()
  54.         );
  55.         $enableRegistrationInteraction $this->systemConfigService->get(
  56.             'VRPay.config.allowInteractRegistrations',
  57.             $event->getSalesChannelContext()->getSalesChannel()->getId()
  58.         );
  59.         if(!$enableRegistrations || !$enableRegistrationInteraction)
  60.         {
  61.             return;
  62.         }
  63.         $customer $event->getSalesChannelContext()->getCustomer();
  64.         if($customer->getGuest())
  65.         {
  66.             // registrations are always disabled for guests
  67.             return;
  68.         }
  69.         $extension = new CustomerRegistrationsExtension();
  70.         $extension->setRegistrations(
  71.             $this->loadCustomerRegistrations($customer$event->getSalesChannelContext())->getElements()
  72.         );
  73.         $page $event->getPage();
  74.         $page->addExtension('vrpaymentRegistrations'$extension);
  75.     }
  76.     /*
  77.      * internals
  78.      */
  79.     /**
  80.      * Loads all registrations that are currently stored for the given customer
  81.      *
  82.      * @param CustomerEntity $customer
  83.      * @param SalesChannelContext $salesChannelContext
  84.      *
  85.      * @return EntityCollection
  86.      */
  87.     private function loadCustomerRegistrations(CustomerEntity $customerSalesChannelContext $salesChannelContext): EntityCollection
  88.     {
  89.         return $this->registrationsRepository->search(
  90.             (new Criteria())
  91.                 ->addFilter(new EqualsFilter('customerId'$customer->getId()))
  92.                 ->setLimit(100),
  93.             $salesChannelContext->getContext()
  94.         )->getEntities();
  95.     }
  96. }