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

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 Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Swag\PayPal\Checkout\Payment\PayPalPuiPaymentHandler;
  13. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  14. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  15. use Swag\PayPal\Setting\Settings;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class PayUponInvoiceSubscriber implements EventSubscriberInterface
  18. {
  19.     private SettingsValidationServiceInterface $settingsValidationService;
  20.     private SystemConfigService $systemConfigService;
  21.     public function __construct(
  22.         SettingsValidationServiceInterface $settingsValidationService,
  23.         SystemConfigService $systemConfigService
  24.     ) {
  25.         $this->settingsValidationService $settingsValidationService;
  26.         $this->systemConfigService $systemConfigService;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             'sales_channel.payment_method.search.result.loaded' => ['onSearchResultLoaded', -1],
  32.         ];
  33.     }
  34.     public function onSearchResultLoaded(SalesChannelEntitySearchResultLoadedEvent $event): void
  35.     {
  36.         /** @var PaymentMethodCollection $paymentMethodCollection */
  37.         $paymentMethodCollection $event->getResult()->getEntities();
  38.         if (!$this->collectionContainsPuiPaymentMethod($paymentMethodCollection)) {
  39.             return;
  40.         }
  41.         $salesChannelId $event->getSalesChannelContext()->getSalesChannelId();
  42.         try {
  43.             $this->settingsValidationService->validate($salesChannelId);
  44.         } catch (PayPalSettingsInvalidException $e) {
  45.             return;
  46.         }
  47.         if ($this->systemConfigService->getBool(Settings::SPB_CHECKOUT_ENABLED$salesChannelId)
  48.             && $this->systemConfigService->getBool(Settings::SPB_ALTERNATIVE_PAYMENT_METHODS_ENABLED$salesChannelId)) {
  49.             return;
  50.         }
  51.         $paymentMethodCollection->filterAndReduceByProperty('handlerIdentifier'PayPalPuiPaymentHandler::class);
  52.     }
  53.     private function collectionContainsPuiPaymentMethod(PaymentMethodCollection $paymentMethodCollection): bool
  54.     {
  55.         return $paymentMethodCollection->filter(
  56.             static function (PaymentMethodEntity $paymentMethod) {
  57.                 return $paymentMethod->getHandlerIdentifier() === PayPalPuiPaymentHandler::class;
  58.             }
  59.         )->count() !== 0;
  60.     }
  61. }