CRM-16692 follow-up fix, move assumptions back to calling-form
[civicrm-core.git] / Civi / Payment / System.php
CommitLineData
914a49bf
EM
1<?php
2
3namespace Civi\Payment;
4
5/**
6 * Class System
7 * @package Civi\Payment
8 */
9class 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 /**
428e38a4
EM
32 * Starting from the processor as an array retrieve the processor as an object.
33 *
34 * If there is no valid configuration it will not be retrieved.
35 *
914a49bf 36 * @param array $processor
6b812400
EM
37 * @param bool $force
38 * Override the config check. This is required in uninstall as no valid instances exist
39 * but will deliberately not work with any valid processors.
428e38a4
EM
40 *
41 * @return CRM_Core_Payment|NULL
42 *
507d3b64 43 * @throws \CRM_Core_Exception
914a49bf 44 */
6b812400
EM
45 public function getByProcessor($processor, $force = FALSE) {
46 $id = $force ? 0 : $processor['id'];
914a49bf 47
6b812400
EM
48 if (!isset($this->cache[$id]) || $force) {
49 $ext = \CRM_Extension_System::singleton()->getMapper();
50 if ($ext->isExtensionKey($processor['class_name'])) {
51 $paymentClass = $ext->keyToClass($processor['class_name'], 'payment');
52 require_once $ext->classToPath($paymentClass);
53 }
54 else {
55 $paymentClass = 'CRM_Core_' . $processor['class_name'];
56 if (empty($paymentClass)) {
57 throw new \CRM_Core_Exception('no class provided');
914a49bf 58 }
6b812400
EM
59 require_once str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php';
60 }
914a49bf 61
6b812400 62 $processorObject = new $paymentClass(!empty($processor['is_test']) ? 'test' : 'live', $processor);
4ecbf127 63 if (!$force && $processorObject->checkConfig()) {
6b812400
EM
64 $processorObject = NULL;
65 }
66 else {
67 $processorObject->setPaymentProcessor($processor);
f92d1e2a 68 }
4ecbf127 69 $this->cache[$id] = $processorObject;
914a49bf 70 }
4ecbf127 71
ef1c2283 72 return $this->cache[$id];
914a49bf 73 }
46bcf597 74
914a49bf 75 /**
df8d3074 76 * @param int $id
1b9f9ca3
EM
77 *
78 * @return \Civi\Payment\CRM_Core_Payment|NULL
914a49bf
EM
79 * @throws \CiviCRM_API3_Exception
80 */
81 public function getById($id) {
5cd2f5ea 82 $processor = civicrm_api3('payment_processor', 'getsingle', array('id' => $id, 'is_test' => NULL));
914a49bf
EM
83 return self::getByProcessor($processor);
84 }
85
86 /**
87 * @param string $name
88 * @param bool $is_test
1b9f9ca3
EM
89 *
90 * @return \Civi\Payment\CRM_Core_Payment|NULL
914a49bf
EM
91 * @throws \CiviCRM_API3_Exception
92 */
93 public function getByName($name, $is_test) {
09fbc53c 94 $processor = civicrm_api3('payment_processor', 'getsingle', array('name' => $name, 'is_test' => $is_test));
914a49bf
EM
95 return self::getByProcessor($processor);
96 }
96025800 97
1fee3ad2
EM
98 /**
99 * Flush processors from static cache.
100 *
101 * This is particularly used for tests.
102 *
103 */
104 public function flushProcessors() {
105 $this->cache = array();
d6944518
EM
106 \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors(NULL, TRUE);
107 \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors('test', TRUE);
1fee3ad2
EM
108 }
109
6b812400
EM
110 /**
111 * Sometimes we want to instantiate a processor object when no valid instance exists (eg. when uninstalling a
112 * processor).
113 *
114 * This function does not load instance specific details for the processor.
115 *
116 * @param string $className
117 *
118 * @return \Civi\Payment\CRM_Core_Payment|NULL
119 * @throws \CiviCRM_API3_Exception
120 */
121 public function getByClass($className) {
122 return $this->getByProcessor(array(
123 'class_name' => $className,
124 'id' => 0,
125 'is_test' => 0,
126 ),
127 TRUE);
128 }
129
914a49bf 130}