From 914a49bfd6e54b3f25224420816ccdbe264d88d5 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 7 Jan 2015 16:04:36 +1300 Subject: [PATCH] CRM-15771 towards factory class --- CRM/Core/Payment.php | 33 +++-------------- Civi/Payment/System.php | 78 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 Civi/Payment/System.php diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index 84d5602d3c..273dbbfdcd 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -25,6 +25,7 @@ +--------------------------------------------------------------------+ */ +use Civi\Payment\System; /** * * @package CRM @@ -85,6 +86,9 @@ abstract class CRM_Core_Payment { /** * Singleton function used to manage this object + * We will migrate to calling Civi\Payment\System::singleton()->getByProcessor($paymentProcessor) + * & Civi\Payment\System::singleton()->getById($paymentProcessor) directly as the main access methods & work + * to remove this function all together * * @param string $mode * The mode of operation: live or test. @@ -106,34 +110,7 @@ abstract class CRM_Core_Payment { if (empty($paymentProcessor)) { return CRM_Core_DAO::$_nullObject; } - - $cacheKey = "{$mode}_{$paymentProcessor['id']}_" . (int) isset($paymentForm); - - if (!isset(self::$_singleton[$cacheKey]) || $force) { - $config = CRM_Core_Config::singleton(); - $ext = CRM_Extension_System::singleton()->getMapper(); - if ($ext->isExtensionKey($paymentProcessor['class_name'])) { - $paymentClass = $ext->keyToClass($paymentProcessor['class_name'], 'payment'); - require_once $ext->classToPath($paymentClass); - } - else { - $paymentClass = 'CRM_Core_' . $paymentProcessor['class_name']; - if (empty($paymentClass)) { - throw new CRM_Core_Exception('no class provided'); - } - require_once str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php'; - } - - //load the object. - self::$_singleton[$cacheKey] = new $paymentClass($mode, $paymentProcessor); - } - - //load the payment form for required processor. - //if ($paymentForm !== NULL) { - //self::$_singleton[$cacheKey]->setForm($paymentForm); - //} - - return self::$_singleton[$cacheKey]; + Civi\Payment\System::singleton()->getByProcessor($paymentProcessor); } /** diff --git a/Civi/Payment/System.php b/Civi/Payment/System.php new file mode 100644 index 0000000000..41b05e5853 --- /dev/null +++ b/Civi/Payment/System.php @@ -0,0 +1,78 @@ +getMapper(); + if ($ext->isExtensionKey($processor['class_name'])) { + $paymentClass = $ext->keyToClass($processor['class_name'], 'payment'); + require_once $ext->classToPath($paymentClass); + } + else { + $paymentClass = 'CRM_Core_' . $processor['class_name']; + if (empty($paymentClass)) { + throw new CRM_Core_Exception('no class provided'); + } + require_once str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php'; + } + + self::$cachen[$id] = new $paymentClass($processor['is_test'] ? 'test' : 'live', $processor); + } + } + return self::$cache[$id]; + } + /** + * @param integer $id + * @throws \CiviCRM_API3_Exception + */ + public function getById($id) { + $processor = civicrm_api3('payment_processor', 'get_single', array('id' => $id)); + return self::getByProcessor($processor); + } + + /** + * @param string $name + * @param bool $is_test + * @throws \CiviCRM_API3_Exception + */ + public function getByName($name, $is_test) { + $processor = civicrm_api3('payment_processor', 'get_single', array('name' => $name, 'is_test' => $is_test)); + return self::getByProcessor($processor); + } +} -- 2.25.1