vendor/shopware/core/Checkout/Payment/Cart/PaymentHandler/PaymentHandlerRegistry.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Payment\Cart\PaymentHandler;
  3. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  4. use Shopware\Core\Framework\App\Payment\Handler\AppAsyncPaymentHandler;
  5. use Shopware\Core\Framework\App\Payment\Handler\AppSyncPaymentHandler;
  6. use Symfony\Contracts\Service\ServiceProviderInterface;
  7. class PaymentHandlerRegistry
  8. {
  9.     /**
  10.      * @var array<string, PaymentHandlerInterface>
  11.      */
  12.     private array $handlers = [];
  13.     public function __construct(
  14.         ServiceProviderInterface $syncHandlers,
  15.         ServiceProviderInterface $asyncHandlers,
  16.         ServiceProviderInterface $preparedHandlers
  17.     ) {
  18.         foreach (\array_keys($syncHandlers->getProvidedServices()) as $serviceId) {
  19.             $handler $syncHandlers->get($serviceId);
  20.             $this->handlers[(string) $serviceId] = $handler;
  21.         }
  22.         foreach (\array_keys($asyncHandlers->getProvidedServices()) as $serviceId) {
  23.             $handler $asyncHandlers->get($serviceId);
  24.             $this->handlers[(string) $serviceId] = $handler;
  25.         }
  26.         foreach (\array_keys($preparedHandlers->getProvidedServices()) as $serviceId) {
  27.             $handler $preparedHandlers->get($serviceId);
  28.             $this->handlers[(string) $serviceId] = $handler;
  29.         }
  30.     }
  31.     /**
  32.      * @deprecated tag:v6.5.0 Will be removed. Use getHandlerForPaymentMethod instead.
  33.      *
  34.      * @return PaymentHandlerInterface|null
  35.      */
  36.     public function getHandler(string $handlerId)
  37.     {
  38.         if (!\array_key_exists($handlerId$this->handlers)) {
  39.             return null;
  40.         }
  41.         return $this->handlers[$handlerId];
  42.     }
  43.     /**
  44.      * @deprecated tag:v6.5.0 the return type will be native
  45.      *
  46.      * @return PaymentHandlerInterface|null
  47.      */
  48.     public function getHandlerForPaymentMethod(PaymentMethodEntity $paymentMethod)
  49.     {
  50.         if ($paymentMethod->getAppPaymentMethod() !== null) {
  51.             return $this->resolveAppHandler($paymentMethod);
  52.         }
  53.         $handlerId $paymentMethod->getHandlerIdentifier();
  54.         if (!\array_key_exists($handlerId$this->handlers)) {
  55.             return null;
  56.         }
  57.         return $this->handlers[$handlerId];
  58.     }
  59.     /**
  60.      * @deprecated tag:v6.5.0 Will be removed. Use getSyncHandlerForPaymentMethod instead.
  61.      */
  62.     public function getSyncHandler(string $handlerId): ?SynchronousPaymentHandlerInterface
  63.     {
  64.         $handler $this->getHandler($handlerId);
  65.         if (!$handler || !$handler instanceof SynchronousPaymentHandlerInterface) {
  66.             return null;
  67.         }
  68.         return $handler;
  69.     }
  70.     public function getSyncHandlerForPaymentMethod(PaymentMethodEntity $paymentMethod): ?SynchronousPaymentHandlerInterface
  71.     {
  72.         $handler $this->getHandlerForPaymentMethod($paymentMethod);
  73.         if (!$handler instanceof SynchronousPaymentHandlerInterface) {
  74.             return null;
  75.         }
  76.         return $handler;
  77.     }
  78.     /**
  79.      * @deprecated tag:v6.5.0 Will be removed. Use getAsyncHandlerForPaymentMethod instead.
  80.      */
  81.     public function getAsyncHandler(string $handlerId): ?AsynchronousPaymentHandlerInterface
  82.     {
  83.         $handler $this->getHandler($handlerId);
  84.         if (!$handler || !$handler instanceof AsynchronousPaymentHandlerInterface) {
  85.             return null;
  86.         }
  87.         return $handler;
  88.     }
  89.     public function getAsyncHandlerForPaymentMethod(PaymentMethodEntity $paymentMethod): ?AsynchronousPaymentHandlerInterface
  90.     {
  91.         $handler $this->getHandlerForPaymentMethod($paymentMethod);
  92.         if (!$handler instanceof AsynchronousPaymentHandlerInterface) {
  93.             return null;
  94.         }
  95.         return $handler;
  96.     }
  97.     public function getPreparedHandlerForPaymentMethod(PaymentMethodEntity $paymentMethod): ?PreparedPaymentHandlerInterface
  98.     {
  99.         $handler $this->getHandlerForPaymentMethod($paymentMethod);
  100.         if (!$handler instanceof PreparedPaymentHandlerInterface) {
  101.             return null;
  102.         }
  103.         return $handler;
  104.     }
  105.     private function resolveAppHandler(PaymentMethodEntity $paymentMethod): ?PaymentHandlerInterface
  106.     {
  107.         $appPaymentMethod $paymentMethod->getAppPaymentMethod();
  108.         if ($appPaymentMethod === null) {
  109.             return null;
  110.         }
  111.         if (empty($appPaymentMethod->getFinalizeUrl())) {
  112.             return $this->handlers[AppSyncPaymentHandler::class] ?? null;
  113.         }
  114.         return $this->handlers[AppAsyncPaymentHandler::class] ?? null;
  115.     }
  116. }