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

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