Merge pull request #5047 from colemanw/CRM-15898
[civicrm-core.git] / Civi / Payment / System.php
1 <?php
2
3 namespace Civi\Payment;
4
5 /**
6 * Class System
7 * @package Civi\Payment
8 */
9 class System {
10
11 /**
12 * @var System
13 */
14 private static $singleton;
15
16 /**
17 * @var array cache
18 */
19 private $cache = array();
20
21 /**
22 * @return \Civi\Payment\System
23 */
24 public static function singleton() {
25 if (!self::$singleton) {
26 self::$singleton = new self();
27 }
28 return self::$singleton;
29 }
30
31 /**
32 * @param array $processor
33 * @throws \CRM_Core_Exception
34 */
35 public function getByProcessor($processor) {
36 $id = $processor['id'];
37
38 if (!isset($this->cache[$id])) {
39 if (!isset($this->cache[$id])) {
40 //does this config need to be called?
41 $config = \CRM_Core_Config::singleton();
42 $ext = \CRM_Extension_System::singleton()->getMapper();
43 if ($ext->isExtensionKey($processor['class_name'])) {
44 $paymentClass = $ext->keyToClass($processor['class_name'], 'payment');
45 require_once $ext->classToPath($paymentClass);
46 }
47 else {
48 $paymentClass = 'CRM_Core_' . $processor['class_name'];
49 if (empty($paymentClass)) {
50 throw new \CRM_Core_Exception('no class provided');
51 }
52 require_once str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php';
53 }
54
55 $this->cache[$id] = new $paymentClass($processor['is_test'] ? 'test' : 'live', $processor);
56 }
57 }
58 return $this->cache[$id];
59 }
60
61 /**
62 * @param int $id
63 * @throws \CiviCRM_API3_Exception
64 */
65 public function getById($id) {
66 $processor = civicrm_api3('payment_processor', 'get_single', array('id' => $id));
67 return self::getByProcessor($processor);
68 }
69
70 /**
71 * @param string $name
72 * @param bool $is_test
73 * @throws \CiviCRM_API3_Exception
74 */
75 public function getByName($name, $is_test) {
76 $processor = civicrm_api3('payment_processor', 'get_single', array('name' => $name, 'is_test' => $is_test));
77 return self::getByProcessor($processor);
78 }
79
80 /**
81 * Flush processors from static cache.
82 *
83 * This is particularly used for tests.
84 *
85 */
86 public function flushProcessors() {
87 $this->cache = array();
88 }
89
90 }