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

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\Struct\PageExtension\Account\PaymentMethodPageExtension;
  7. use UnzerPayment6\DataAbstractionLayer\Entity\PaymentDevice\UnzerPaymentDeviceEntity;
  8. use UnzerPayment6\DataAbstractionLayer\Repository\PaymentDevice\UnzerPaymentDeviceRepositoryInterface;
  9. class PaymentMethodPageEventListener implements EventSubscriberInterface
  10. {
  11.     /** @var UnzerPaymentDeviceRepositoryInterface */
  12.     private $deviceRepository;
  13.     public function __construct(UnzerPaymentDeviceRepositoryInterface $deviceRepository)
  14.     {
  15.         $this->deviceRepository $deviceRepository;
  16.     }
  17.     /**
  18.      * {@inheritdoc}
  19.      */
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             AccountPaymentMethodPageLoadedEvent::class => 'onLoadAccountPaymentMethod',
  24.         ];
  25.     }
  26.     public function onLoadAccountPaymentMethod(AccountPaymentMethodPageLoadedEvent $event): void
  27.     {
  28.         $salesChannelContext $event->getSalesChannelContext();
  29.         if (!$salesChannelContext->getCustomer()) {
  30.             return;
  31.         }
  32.         $extension = new PaymentMethodPageExtension();
  33.         $devices   $this->deviceRepository->getCollectionByCustomer($salesChannelContext->getCustomer(), $salesChannelContext->getContext());
  34.         $extension->setDeviceRemoved((bool) $event->getRequest()->get('deviceRemoved'));
  35.         if ($salesChannelContext->getCustomer() !== null) {
  36.             $creditCards               $devices->filterByProperty('deviceType'UnzerPaymentDeviceEntity::DEVICE_TYPE_CREDIT_CARD)->getElements();
  37.             $directDebitDevices        $devices->filterByProperty('deviceType'UnzerPaymentDeviceEntity::DEVICE_TYPE_DIRECT_DEBIT)->getElements();
  38.             $directDebitSecuredDevices $devices->filterByProperty('deviceType'UnzerPaymentDeviceEntity::DEVICE_TYPE_DIRECT_DEBIT_SECURED)->getElements();
  39.             $payPalAccounts            $devices->filterByProperty('deviceType'UnzerPaymentDeviceEntity::DEVICE_TYPE_PAYPAL)->getElements();
  40.             $extension->addPaymentDevices($creditCards);
  41.             $extension->addPaymentDevices($directDebitDevices);
  42.             $extension->addPaymentDevices($directDebitSecuredDevices);
  43.             $extension->addPaymentDevices($payPalAccounts);
  44.         }
  45.         $event->getPage()->addExtension(PaymentMethodPageExtension::EXTENSION_NAME$extension);
  46.     }
  47. }