diff --git a/modules/payment/src/Entity/PaymentGateway.php b/modules/payment/src/Entity/PaymentGateway.php index f93a1fbaae..270d7a992e 100644 --- a/modules/payment/src/Entity/PaymentGateway.php +++ b/modules/payment/src/Entity/PaymentGateway.php @@ -6,6 +6,7 @@ use Drupal\commerce\ConditionGroup; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\Core\Config\Entity\ConfigEntityBase; +use Drupal\Core\Entity\EntityStorageInterface; /** * Defines the payment gateway entity class. @@ -253,9 +254,43 @@ public function set($property_name, $value) { protected function getPluginCollection() { if (!$this->pluginCollection) { $plugin_manager = \Drupal::service('plugin.manager.commerce_payment_gateway'); - $this->pluginCollection = new CommerceSinglePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id); + $this->pluginCollection = new CommerceSinglePluginCollection( + $plugin_manager, + $this->plugin, + $this->configuration, + $this->id + ); } return $this->pluginCollection; } + /** + * {@inheritdoc} + */ + public static function postDelete(EntityStorageInterface $storage, array $entities) { + parent::postDelete($storage, $entities); + + /** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */ + $payment_method_storage = \Drupal::service('entity_type.manager') + ->getStorage('commerce_payment_method'); + + foreach ($entities as $payment_gateway) { + $payment_methods = $payment_method_storage->loadByPaymentGateway($payment_gateway); + if (empty($payment_methods)) { + continue; + } + if (count($payment_methods) < 50) { + // If there is less than 50 payment methods, we delete them straight away. + $payment_method_storage->delete($payment_methods); + } + else { + // Else we queue them for deletion. + $queue = \Drupal::queue('payment_methods_delete_queue'); + foreach (array_chunk($payment_methods, 50) as $payment_methods_chunk) { + $queue->createItem($payment_methods_chunk); + } + } + } + } + } diff --git a/modules/payment/src/PaymentMethodStorage.php b/modules/payment/src/PaymentMethodStorage.php index 8330b3d2d7..7c1b2362c3 100644 --- a/modules/payment/src/PaymentMethodStorage.php +++ b/modules/payment/src/PaymentMethodStorage.php @@ -125,4 +125,20 @@ protected function doCreate(array $values) { return parent::doCreate($values); } + /** + * {@inheritdoc} + */ + public function loadByPaymentGateway(PaymentGatewayInterface $payment_gateway) { + if (!($payment_gateway->getPlugin() instanceof SupportsStoredPaymentMethodsInterface)) { + return []; + } + $query = $this->getQuery() + ->condition('payment_gateway', $payment_gateway->id()); + $result = $query->execute(); + if (empty($result)) { + return []; + } + return $this->loadMultiple($result); + } + } diff --git a/modules/payment/src/PaymentMethodStorageInterface.php b/modules/payment/src/PaymentMethodStorageInterface.php index c4f8870260..a68317ac54 100644 --- a/modules/payment/src/PaymentMethodStorageInterface.php +++ b/modules/payment/src/PaymentMethodStorageInterface.php @@ -28,4 +28,15 @@ interface PaymentMethodStorageInterface extends ContentEntityStorageInterface { */ public function loadReusable(UserInterface $account, PaymentGatewayInterface $payment_gateway, array $billing_countries = []); + /** + * Loads the payment methods for the given payment gateway. + * + * @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway + * The payment gateway. + * + * @return \Drupal\commerce_payment\Entity\PaymentMethodInterface[] + * The payment methods. + */ + public function loadByPaymentGateway(PaymentGatewayInterface $payment_gateway); + } diff --git a/modules/payment/src/Plugin/QueueWorker/PaymentMethodDeleteQueue.php b/modules/payment/src/Plugin/QueueWorker/PaymentMethodDeleteQueue.php new file mode 100644 index 0000000000..b802682746 --- /dev/null +++ b/modules/payment/src/Plugin/QueueWorker/PaymentMethodDeleteQueue.php @@ -0,0 +1,28 @@ +getStorage('commerce_payment_method'); + $payment_method_storage->delete($data); + } + +}