platform/src/Core/Checkout/Payment/DataAbstractionLayer/PaymentHandlerIdentifierSubscriber.php line 24

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Payment\DataAbstractionLayer;
  3. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandlerInterface;
  4. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\PreparedPaymentHandlerInterface;
  5. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\SynchronousPaymentHandlerInterface;
  6. use Shopware\Core\Checkout\Payment\PaymentEvents;
  7. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\Feature;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
  12. class PaymentHandlerIdentifierSubscriber implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             PaymentEvents::PAYMENT_METHOD_LOADED_EVENT => 'formatHandlerIdentifier',
  18.         ];
  19.     }
  20.     public function formatHandlerIdentifier(EntityLoadedEvent $event): void
  21.     {
  22.         /** @var PaymentMethodEntity $entity */
  23.         foreach ($event->getEntities() as $entity) {
  24.             if (Feature::isActive('FEATURE_NEXT_16769')) {
  25.                 $this->setPaymentMethodHandlerRuntimeFields($entity);
  26.             }
  27.             $explodedHandlerIdentifier explode('\\'$entity->getHandlerIdentifier());
  28.             $last $explodedHandlerIdentifier[\count($explodedHandlerIdentifier) - 1];
  29.             $entity->setShortName((new CamelCaseToSnakeCaseNameConverter())->normalize($last));
  30.             if (\count($explodedHandlerIdentifier) < 2) {
  31.                 $entity->setFormattedHandlerIdentifier($entity->getHandlerIdentifier());
  32.                 continue;
  33.             }
  34.             /** @var string|null $firstHandlerIdentifier */
  35.             $firstHandlerIdentifier array_shift($explodedHandlerIdentifier);
  36.             $lastHandlerIdentifier array_pop($explodedHandlerIdentifier);
  37.             if ($firstHandlerIdentifier === null || $lastHandlerIdentifier === null) {
  38.                 continue;
  39.             }
  40.             $formattedHandlerIdentifier 'handler_'
  41.                 mb_strtolower($firstHandlerIdentifier)
  42.                 . '_'
  43.                 mb_strtolower($lastHandlerIdentifier);
  44.             $entity->setFormattedHandlerIdentifier($formattedHandlerIdentifier);
  45.         }
  46.     }
  47.     private function setPaymentMethodHandlerRuntimeFields(PaymentMethodEntity $entity): void
  48.     {
  49.         $handlerIdentifier $entity->getHandlerIdentifier();
  50.         if (\is_a($handlerIdentifierSynchronousPaymentHandlerInterface::class, true)) {
  51.             $entity->setSynchronous(true);
  52.         }
  53.         if (\is_a($handlerIdentifierAsynchronousPaymentHandlerInterface::class, true)) {
  54.             $entity->setAsynchronous(true);
  55.         }
  56.         if (\is_a($handlerIdentifierPreparedPaymentHandlerInterface::class, true)) {
  57.             $entity->setPrepared(true);
  58.         }
  59.     }
  60. }