+--------------------------------------------------------------------+
*/
+use Civi\Payment\System;
/**
*
* @package CRM
/**
* 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.
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);
}
/**
--- /dev/null
+<?php
+
+namespace Civi\Payment;
+
+/**
+ * Class System
+ * @package Civi\Payment
+ */
+class System {
+
+ /**
+ * @var System
+ */
+ private static $singleton;
+
+ /**
+ * @var array cache
+ */
+ private static $cache = array();
+
+ /**
+ * @return \Civi\Payment\System
+ */
+ public static function singleton() {
+ if (!self::$singleton) {
+ self::$singleton = new self();
+ }
+ return self::$singleton;
+ }
+
+ /**
+ * @param array $processor
+ * @throws \Civi\Payment\CRM_Core_Exception
+ */
+ public function getByProcessor($processor) {
+ $id = $processor['id'];
+
+ if (!isset(self::$cache[$id])) {
+ if (!isset(self::$cache[$id])) {
+ //does this config need to be called?
+ $config = CRM_Core_Config::singleton();
+ $ext = CRM_Extension_System::singleton()->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);
+ }
+}