From 7d2b54634239d4ec61ce8b1193b2ec25a7510dbb Mon Sep 17 00:00:00 2001 From: Tilen Date: Mon, 23 Oct 2017 10:51:42 +0200 Subject: [PATCH 1/7] Issue #2898217 by tilenav: Delete payment methods when their payment gateway is deleted --- modules/payment/src/Entity/PaymentGateway.php | 29 +++++++++++++++++++ .../QueueWorker/PaymentMethodDeleteQueue.php | 28 ++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 modules/payment/src/Plugin/QueueWorker/PaymentMethodDeleteQueue.php diff --git a/modules/payment/src/Entity/PaymentGateway.php b/modules/payment/src/Entity/PaymentGateway.php index f93a1fbaae..e144919a30 100644 --- a/modules/payment/src/Entity/PaymentGateway.php +++ b/modules/payment/src/Entity/PaymentGateway.php @@ -258,4 +258,33 @@ protected function getPluginCollection() { 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'); + //$queue->createQueue(); + foreach (array_chunk($payment_methods, 50) as $payment_methods_chunk) { + $queue->createItem($payment_methods_chunk); + } + } + } + } + } 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); + } + +} From 9183654df5389a51ae38e482ac25bb43f688d6e4 Mon Sep 17 00:00:00 2001 From: Tilen Date: Mon, 23 Oct 2017 10:58:06 +0200 Subject: [PATCH 2/7] Issue #2898217 by tilenav: Delete payment methods when their payment gateway is deleted --- modules/payment/src/Entity/PaymentGateway.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/payment/src/Entity/PaymentGateway.php b/modules/payment/src/Entity/PaymentGateway.php index e144919a30..1be50f4e0f 100644 --- a/modules/payment/src/Entity/PaymentGateway.php +++ b/modules/payment/src/Entity/PaymentGateway.php @@ -267,7 +267,7 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti /** @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)) { @@ -279,7 +279,6 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti } else { // Else we queue them for deletion. $queue = \Drupal::queue('payment_methods_delete_queue'); - //$queue->createQueue(); foreach (array_chunk($payment_methods, 50) as $payment_methods_chunk) { $queue->createItem($payment_methods_chunk); } From 633b6e212f7e05ce80966cd46d84bf88fda66335 Mon Sep 17 00:00:00 2001 From: Tilen Date: Mon, 23 Oct 2017 12:40:29 +0200 Subject: [PATCH 3/7] Added PaymentMethod::loadByPaymentGateway --- modules/payment/src/PaymentMethodStorage.php | 16 ++++++++++++++++ .../src/PaymentMethodStorageInterface.php | 9 +++++++++ 2 files changed, 25 insertions(+) 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..8a28aaa4b7 100644 --- a/modules/payment/src/PaymentMethodStorageInterface.php +++ b/modules/payment/src/PaymentMethodStorageInterface.php @@ -28,4 +28,13 @@ interface PaymentMethodStorageInterface extends ContentEntityStorageInterface { */ public function loadReusable(UserInterface $account, PaymentGatewayInterface $payment_gateway, array $billing_countries = []); + /** + * @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $paymentGateway + * The payment gateway. + * + * @return \Drupal\commerce_payment\Entity\PaymentMethodInterface[] + * The payment methods. + */ + public function loadByPaymentGateway(PaymentGatewayInterface $payment_gateway); + } From 0cbb96fe2b78909b76c7e7bf09d70a15977ae460 Mon Sep 17 00:00:00 2001 From: Tilen Date: Mon, 23 Oct 2017 13:41:31 +0200 Subject: [PATCH 4/7] Issue #2898217: Added function explanation comment --- modules/payment/src/PaymentMethodStorageInterface.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/payment/src/PaymentMethodStorageInterface.php b/modules/payment/src/PaymentMethodStorageInterface.php index 8a28aaa4b7..d7b601db8f 100644 --- a/modules/payment/src/PaymentMethodStorageInterface.php +++ b/modules/payment/src/PaymentMethodStorageInterface.php @@ -29,6 +29,8 @@ 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 $paymentGateway * The payment gateway. * From 24c2f6c1e8d03eefe071f5661037b14a3acb7813 Mon Sep 17 00:00:00 2001 From: Tilen Date: Thu, 26 Oct 2017 09:40:28 +0200 Subject: [PATCH 5/7] Removed unnecessary comment --- modules/payment/src/Entity/PaymentGateway.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/payment/src/Entity/PaymentGateway.php b/modules/payment/src/Entity/PaymentGateway.php index 1be50f4e0f..e626b9dd1a 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,7 +254,12 @@ 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; } From bb66f345d1ac83827029f95b273d0cc3eb136b97 Mon Sep 17 00:00:00 2001 From: Tilen Date: Thu, 26 Oct 2017 11:04:46 +0200 Subject: [PATCH 6/7] Fixed parameter description --- modules/payment/src/PaymentMethodStorageInterface.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/payment/src/PaymentMethodStorageInterface.php b/modules/payment/src/PaymentMethodStorageInterface.php index d7b601db8f..a68317ac54 100644 --- a/modules/payment/src/PaymentMethodStorageInterface.php +++ b/modules/payment/src/PaymentMethodStorageInterface.php @@ -30,8 +30,8 @@ public function loadReusable(UserInterface $account, PaymentGatewayInterface $pa /** * Loads the payment methods for the given payment gateway. - * - * @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $paymentGateway + * + * @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway * The payment gateway. * * @return \Drupal\commerce_payment\Entity\PaymentMethodInterface[] From 51c7670981fc57b611580d1f8c8def56f6371d0f Mon Sep 17 00:00:00 2001 From: Tilen Date: Thu, 26 Oct 2017 11:39:02 +0200 Subject: [PATCH 7/7] Newline after closing brace --- modules/payment/src/Entity/PaymentGateway.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/payment/src/Entity/PaymentGateway.php b/modules/payment/src/Entity/PaymentGateway.php index e626b9dd1a..270d7a992e 100644 --- a/modules/payment/src/Entity/PaymentGateway.php +++ b/modules/payment/src/Entity/PaymentGateway.php @@ -282,7 +282,8 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti 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 { // Else we queue them for deletion. $queue = \Drupal::queue('payment_methods_delete_queue'); foreach (array_chunk($payment_methods, 50) as $payment_methods_chunk) {