defer to Tim's wisdom
[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 \Civi\Payment\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 * @param integer $id
62 * @throws \CiviCRM_API3_Exception
63 */
64 public function getById($id) {
65 $processor = civicrm_api3('payment_processor', 'get_single', array('id' => $id));
66 return self::getByProcessor($processor);
67 }
68
69 /**
70 * @param string $name
71 * @param bool $is_test
72 * @throws \CiviCRM_API3_Exception
73 */
74 public function getByName($name, $is_test) {
75 $processor = civicrm_api3('payment_processor', 'get_single', array('name' => $name, 'is_test' => $is_test));
76 return self::getByProcessor($processor);
77 }
78 }