From fc62516602d36c546436c285363422c6c3d8c246 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 6 May 2021 15:52:13 -1000 Subject: [PATCH] (REF) Extract method ManagedEntities::isActivationSupported() --- CRM/Core/ManagedEntities.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/CRM/Core/ManagedEntities.php b/CRM/Core/ManagedEntities.php index d35ef2d104..4668273b50 100644 --- a/CRM/Core/ManagedEntities.php +++ b/CRM/Core/ManagedEntities.php @@ -305,13 +305,8 @@ class CRM_Core_ManagedEntities { * @throws \CiviCRM_API3_Exception */ public function disableEntity($dao): void { - $actions = civicrm_api3($dao->entity_type, 'getactions', [])['values']; - $supportsDisable = FALSE; - if (in_array('create', $actions, TRUE) && in_array('getfields', $actions)) { - $fields = civicrm_api3($dao->entity_type, 'getfields', ['action' => 'create'])['values']; - $supportsDisable = array_key_exists('is_active', $fields); - } - if ($supportsDisable) { + $entity_type = $dao->entity_type; + if ($this->isActivationSupported($entity_type)) { // FIXME cascading for payproc types? $params = [ 'version' => 3, @@ -486,4 +481,23 @@ class CRM_Core_ManagedEntities { throw new Exception('API error: ' . $result['error_message'] . ' on ' . $entity . '.' . $action); } + /** + * Determine if an entity supports APIv3-based activation/de-activation. + * @param string $entity_type + * + * @return bool + * @throws \CiviCRM_API3_Exception + */ + private function isActivationSupported(string $entity_type): bool { + if (!isset(Civi::$statics[__CLASS__][__FUNCTION__][$entity_type])) { + $actions = civicrm_api3($entity_type, 'getactions', [])['values']; + Civi::$statics[__CLASS__][__FUNCTION__][$entity_type] = FALSE; + if (in_array('create', $actions, TRUE) && in_array('getfields', $actions)) { + $fields = civicrm_api3($entity_type, 'getfields', ['action' => 'create'])['values']; + Civi::$statics[__CLASS__][__FUNCTION__][$entity_type] = array_key_exists('is_active', $fields); + } + } + return Civi::$statics[__CLASS__][__FUNCTION__][$entity_type]; + } + } -- 2.25.1