1 <?php
2
3 namespace PayPal\Core;
4
5 use PayPal\Auth\OAuthTokenCredential;
6 use PayPal\Exception\PayPalInvalidCredentialException;
7
8 9 10 11 12 13 14
15 class PayPalCredentialManager
16 {
17 18 19 20 21
22 private static $instance;
23
24 25 26 27 28
29 private $credentialHashmap = array();
30
31 32 33 34 35 36
37 private $defaultAccountName;
38
39 40 41 42 43 44
45 private function __construct($config)
46 {
47 try {
48 $this->initCredential($config);
49 } catch (\Exception $e) {
50 $this->credentialHashmap = array();
51 throw $e;
52 }
53 }
54
55 56 57 58 59 60
61 public static function getInstance($config = null)
62 {
63 if (!self::$instance) {
64 self::$instance = new self($config == null ? PayPalConfigManager::getInstance()->getConfigHashmap() : $config);
65 }
66 return self::$instance;
67 }
68
69 70 71 72 73
74 private function initCredential($config)
75 {
76 $suffix = 1;
77 $prefix = "acct";
78
79 $arr = array();
80 foreach ($config as $k => $v) {
81 if (strstr($k, $prefix)) {
82 $arr[$k] = $v;
83 }
84 }
85 $credArr = $arr;
86
87 $arr = array();
88 foreach ($config as $key => $value) {
89 $pos = strpos($key, '.');
90 if (strstr($key, "acct")) {
91 $arr[] = substr($key, 0, $pos);
92 }
93 }
94 $arrayPartKeys = array_unique($arr);
95
96 $key = $prefix . $suffix;
97 $userName = null;
98 while (in_array($key, $arrayPartKeys)) {
99 if (isset($credArr[$key . ".ClientId"]) && isset($credArr[$key . ".ClientSecret"])) {
100 $userName = $key;
101 $this->credentialHashmap[$userName] = new OAuthTokenCredential(
102 $credArr[$key . ".ClientId"],
103 $credArr[$key . ".ClientSecret"]
104 );
105 }
106 if ($userName && $this->defaultAccountName == null) {
107 if (array_key_exists($key . '.UserName', $credArr)) {
108 $this->defaultAccountName = $credArr[$key . '.UserName'];
109 } else {
110 $this->defaultAccountName = $key;
111 }
112 }
113 $suffix++;
114 $key = $prefix . $suffix;
115 }
116 }
117
118 119 120 121 122 123 124 125 126
127 public function setCredentialObject(OAuthTokenCredential $credential, $userId = null, $default = true)
128 {
129 $key = $userId == null ? 'default' : $userId;
130 $this->credentialHashmap[$key] = $credential;
131 if ($default) {
132 $this->defaultAccountName = $key;
133 }
134 return $this;
135 }
136
137 138 139 140 141 142 143
144 public function getCredentialObject($userId = null)
145 {
146 if ($userId == null && array_key_exists($this->defaultAccountName, $this->credentialHashmap)) {
147 $credObj = $this->credentialHashmap[$this->defaultAccountName];
148 } elseif (array_key_exists($userId, $this->credentialHashmap)) {
149 $credObj = $this->credentialHashmap[$userId];
150 }
151
152 if (empty($credObj)) {
153 throw new PayPalInvalidCredentialException("Credential not found for " . ($userId ? $userId : " default user") .
154 ". Please make sure your configuration/APIContext has credential information");
155 }
156 return $credObj;
157 }
158
159 160 161
162 public function __clone()
163 {
164 trigger_error('Clone is not allowed.', E_USER_ERROR);
165 }
166 }
167