vendor/shopware/core/Checkout/Payment/DataAbstractionLayer/PaymentMethodValidator.php line 36

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Payment\DataAbstractionLayer;
  3. use Doctrine\DBAL\ArrayParameterType;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Checkout\Payment\Exception\PluginPaymentMethodsDeleteRestrictionException;
  6. use Shopware\Core\Checkout\Payment\PaymentMethodDefinition;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * @internal
  12.  */
  13. #[Package('core')]
  14. final class PaymentMethodValidator implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @internal
  18.      */
  19.     public function __construct(private readonly Connection $connection)
  20.     {
  21.     }
  22.     /**
  23.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  24.      */
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             PreWriteValidationEvent::class => 'validate',
  29.         ];
  30.     }
  31.     public function validate(PreWriteValidationEvent $event): void
  32.     {
  33.         $ids $event->getDeletedPrimaryKeys(PaymentMethodDefinition::ENTITY_NAME);
  34.         $ids \array_column($ids'id');
  35.         if (empty($ids)) {
  36.             return;
  37.         }
  38.         $pluginIds $this->connection->fetchOne(
  39.             'SELECT id FROM payment_method WHERE id IN (:ids) AND plugin_id IS NOT NULL',
  40.             ['ids' => $ids],
  41.             ['ids' => ArrayParameterType::STRING]
  42.         );
  43.         if (!empty($pluginIds)) {
  44.             throw new PluginPaymentMethodsDeleteRestrictionException();
  45.         }
  46.     }
  47. }