custom/plugins/SwagPayPal/src/Checkout/PayUponInvoice/PayUponInvoiceSubscriber.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Checkout\PayUponInvoice;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  10. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntitySearchResultLoadedEvent;
  11. use Swag\PayPal\Payment\PayPalPuiPaymentHandler;
  12. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  13. use Swag\PayPal\Setting\Service\SettingsServiceInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class PayUponInvoiceSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SettingsServiceInterface
  19.      */
  20.     private $settingsService;
  21.     public function __construct(SettingsServiceInterface $settingsService)
  22.     {
  23.         $this->settingsService $settingsService;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             'sales_channel.payment_method.search.result.loaded' => ['onSearchResultLoaded', -1],
  29.         ];
  30.     }
  31.     public function onSearchResultLoaded(SalesChannelEntitySearchResultLoadedEvent $event): void
  32.     {
  33.         /** @var PaymentMethodCollection $paymentMethodCollection */
  34.         $paymentMethodCollection $event->getResult()->getEntities();
  35.         if (!$this->collectionContainsPuiPaymentMethod($paymentMethodCollection)) {
  36.             return;
  37.         }
  38.         try {
  39.             $settings $this->settingsService->getSettings(
  40.                 $event->getSalesChannelContext()->getSalesChannel()->getId()
  41.             );
  42.         } catch (PayPalSettingsInvalidException $e) {
  43.             return;
  44.         }
  45.         if ($settings->getSpbCheckoutEnabled() && $settings->getSpbAlternativePaymentMethodsEnabled()) {
  46.             return;
  47.         }
  48.         $paymentMethodCollection->filterAndReduceByProperty('handlerIdentifier'PayPalPuiPaymentHandler::class);
  49.     }
  50.     private function collectionContainsPuiPaymentMethod(PaymentMethodCollection $paymentMethodCollection): bool
  51.     {
  52.         return $paymentMethodCollection->filter(
  53.             static function (PaymentMethodEntity $paymentMethod) {
  54.                 return $paymentMethod->getHandlerIdentifier() === PayPalPuiPaymentHandler::class;
  55.             }
  56.         )->count() !== 0;
  57.     }
  58. }