custom/plugins/UnzerPayment6/src/EventListeners/Account/PaymentMethodPageEventListener.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace UnzerPayment6\EventListeners\Account;
  4. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use UnzerPayment6\Components\ConfigReader\ConfigReader;
  7. use UnzerPayment6\Components\ConfigReader\ConfigReaderInterface;
  8. use UnzerPayment6\Components\Struct\PageExtension\Account\PaymentMethodPageExtension;
  9. use UnzerPayment6\DataAbstractionLayer\Entity\PaymentDevice\UnzerPaymentDeviceEntity;
  10. use UnzerPayment6\DataAbstractionLayer\Repository\PaymentDevice\UnzerPaymentDeviceRepositoryInterface;
  11. class PaymentMethodPageEventListener implements EventSubscriberInterface
  12. {
  13.     /** @var ConfigReaderInterface */
  14.     private $configReader;
  15.     /** @var UnzerPaymentDeviceRepositoryInterface */
  16.     private $deviceRepository;
  17.     public function __construct(ConfigReaderInterface $configReaderUnzerPaymentDeviceRepositoryInterface $deviceRepository)
  18.     {
  19.         $this->configReader     $configReader;
  20.         $this->deviceRepository $deviceRepository;
  21.     }
  22.     /**
  23.      * {@inheritdoc}
  24.      */
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             AccountPaymentMethodPageLoadedEvent::class => 'onLoadAccountPaymentMethod',
  29.         ];
  30.     }
  31.     public function onLoadAccountPaymentMethod(AccountPaymentMethodPageLoadedEvent $event): void
  32.     {
  33.         $salesChannelContext $event->getSalesChannelContext();
  34.         if (!$salesChannelContext->getCustomer()) {
  35.             return;
  36.         }
  37.         $salesChannelId $salesChannelContext->getSalesChannel()->getId();
  38.         $registerCreditCards $this->configReader->read($salesChannelId)->get(ConfigReader::CONFIG_KEY_REGISTER_CARDfalse);
  39.         $registerPayPal      $this->configReader->read($salesChannelId)->get(ConfigReader::CONFIG_KEY_REGISTER_PAYPALfalse);
  40.         $registerDirectDebit $this->configReader->read($salesChannelId)->get(ConfigReader::CONFIG_KEY_REGISTER_DIRECT_DEBITfalse);
  41.         $extension           = new PaymentMethodPageExtension();
  42.         $devices             $this->deviceRepository->getCollectionByCustomer($salesChannelContext->getCustomer(), $salesChannelContext->getContext());
  43.         $extension->setDeviceRemoved((bool) $event->getRequest()->get('deviceRemoved'));
  44.         if ($registerCreditCards && $salesChannelContext->getCustomer() !== null) {
  45.             $creditCards $devices->filterByProperty('deviceType'UnzerPaymentDeviceEntity::DEVICE_TYPE_CREDIT_CARD)->getElements();
  46.             $extension->addPaymentDevices($creditCards);
  47.         }
  48.         if ($registerPayPal && $salesChannelContext->getCustomer() !== null) {
  49.             $payPalAccounts $devices->filterByProperty('deviceType'UnzerPaymentDeviceEntity::DEVICE_TYPE_PAYPAL)->getElements();
  50.             $extension->addPaymentDevices($payPalAccounts);
  51.         }
  52.         if ($registerDirectDebit && $salesChannelContext->getCustomer() !== null) {
  53.             $directDebitDevices        $devices->filterByProperty('deviceType'UnzerPaymentDeviceEntity::DEVICE_TYPE_DIRECT_DEBIT)->getElements();
  54.             $directDebitSecuredDevices $devices->filterByProperty('deviceType'UnzerPaymentDeviceEntity::DEVICE_TYPE_DIRECT_DEBIT_SECURED)->getElements();
  55.             $extension->addPaymentDevices($directDebitDevices);
  56.             $extension->addPaymentDevices($directDebitSecuredDevices);
  57.         }
  58.         $event->getPage()->addExtension(PaymentMethodPageExtension::EXTENSION_NAME$extension);
  59.     }
  60. }