custom/plugins/SwagAmazonPay/src/Storefront/EventListeners/AmazonPayAvailabilityListener.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) shopware AG <info@shopware.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Swag\AmazonPay\Storefront\EventListeners;
  9. use Psr\Log\LoggerInterface;
  10. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  11. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntitySearchResultLoadedEvent;
  12. use Swag\AmazonPay\Components\Config\ConfigServiceInterface;
  13. use Swag\AmazonPay\Components\Config\Validation\Exception\ConfigValidationException;
  14. use Swag\AmazonPay\Installer\PaymentMethodInstaller;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. class AmazonPayAvailabilityListener implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var ConfigServiceInterface
  21.      */
  22.     private $configService;
  23.     /**
  24.      * @var LoggerInterface
  25.      */
  26.     private $logger;
  27.     /**
  28.      * @var RequestStack
  29.      */
  30.     private $requestStack;
  31.     public function __construct(
  32.         ConfigServiceInterface $configService,
  33.         LoggerInterface $logger,
  34.         RequestStack $requestStack
  35.     ) {
  36.         $this->configService $configService;
  37.         $this->logger $logger;
  38.         $this->requestStack $requestStack;
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             'sales_channel.payment_method.search.result.loaded' => ['onSalesChannelSearchResultLoaded', -1],
  47.         ];
  48.     }
  49.     public function onSalesChannelSearchResultLoaded(SalesChannelEntitySearchResultLoadedEvent $event): void
  50.     {
  51.         try {
  52.             $config $this->configService->getPluginConfig($event->getSalesChannelContext()->getSalesChannel()->getId());
  53.         } catch (ConfigValidationException $e) {
  54.             $this->logger->error('Invalid plugin configuration', ['field' => $e->getField()]);
  55.             $this->removeSwagAmazonPayPaymentMethod($event);
  56.             return;
  57.         }
  58.         if ($event->getSalesChannelContext()->getSalesChannel()->getPaymentMethodId() === PaymentMethodInstaller::AMAZON_PAYMENT_ID) {
  59.             return;
  60.         }
  61.         if ($config->hideOneClickCheckoutButtons() === true) {
  62.             $this->removeSwagAmazonPayPaymentMethod($event);
  63.         }
  64.     }
  65.     private function removeSwagAmazonPayPaymentMethod(SalesChannelEntitySearchResultLoadedEvent $event): void
  66.     {
  67.         $filter = static function (PaymentMethodEntity $entity): bool {
  68.             return $entity->getId() !== PaymentMethodInstaller::AMAZON_PAYMENT_ID;
  69.         };
  70.         $filteredPaymentMethods $event->getResult()->getEntities()->filter($filter);
  71.         $event->getResult()->assign([
  72.             'total' => \count($filteredPaymentMethods),
  73.             'entities' => $filteredPaymentMethods,
  74.             'elements' => $filteredPaymentMethods->getElements(),
  75.         ]);
  76.     }
  77. }