CRM-15771 towards factory class
authorEileen McNaughton <eileen@fuzion.co.nz>
Wed, 7 Jan 2015 03:04:36 +0000 (16:04 +1300)
committerEileen McNaughton <eileen@fuzion.co.nz>
Wed, 7 Jan 2015 03:04:36 +0000 (16:04 +1300)
CRM/Core/Payment.php
Civi/Payment/System.php [new file with mode: 0644]

index 84d5602d3c9f003f9827528e9417a35da22e71c7..273dbbfdcd6372b8dbd766911518293f9fc97f5f 100644 (file)
@@ -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 (file)
index 0000000..41b05e5
--- /dev/null
@@ -0,0 +1,78 @@
+<?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);
+  }
+}