Commit | Line | Data |
---|---|---|
914a49bf EM |
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 | */ | |
ef1c2283 | 19 | private $cache = array(); |
914a49bf EM |
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 | |
507d3b64 | 33 | * @throws \CRM_Core_Exception |
914a49bf EM |
34 | */ |
35 | public function getByProcessor($processor) { | |
36 | $id = $processor['id']; | |
37 | ||
ef1c2283 EM |
38 | if (!isset($this->cache[$id])) { |
39 | if (!isset($this->cache[$id])) { | |
914a49bf | 40 | //does this config need to be called? |
1337f9e2 EM |
41 | $config = \CRM_Core_Config::singleton(); |
42 | $ext = \CRM_Extension_System::singleton()->getMapper(); | |
914a49bf EM |
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)) { | |
507d3b64 | 50 | throw new \CRM_Core_Exception('no class provided'); |
914a49bf EM |
51 | } |
52 | require_once str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php'; | |
53 | } | |
54 | ||
ef1c2283 | 55 | $this->cache[$id] = new $paymentClass($processor['is_test'] ? 'test' : 'live', $processor); |
914a49bf EM |
56 | } |
57 | } | |
ef1c2283 | 58 | return $this->cache[$id]; |
914a49bf | 59 | } |
46bcf597 | 60 | |
914a49bf | 61 | /** |
df8d3074 | 62 | * @param int $id |
914a49bf EM |
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 | } | |
96025800 | 79 | |
914a49bf | 80 | } |