1 <?php
2
3 namespace PayPal\Rest;
4
5 use PayPal\Core\PayPalConfigManager;
6 use PayPal\Core\PayPalCredentialManager;
7
8 /**
9 * Class ApiContext
10 *
11 * Call level parameters such as request id, credentials etc
12 *
13 * @package PayPal\Rest
14 */
15 class ApiContext
16 {
17
18 /**
19 * Unique request id to be used for this call
20 * The user can either generate one as per application
21 * needs or let the SDK generate one
22 *
23 * @var null|string $requestId
24 */
25 private $requestId;
26
27 /**
28 * This is a placeholder for holding credential for the request
29 * If the value is not set, it would get the value from @\PayPal\Core\PayPalCredentialManager
30 *
31 * @var \PayPal\Auth\OAuthTokenCredential
32 */
33 private $credential;
34
35
36 /**
37 * Construct
38 *
39 * @param \PayPal\Auth\OAuthTokenCredential $credential
40 * @param string|null $requestId
41 */
42 public function __construct($credential = null, $requestId = null)
43 {
44 $this->requestId = $requestId;
45 $this->credential = $credential;
46 }
47
48 /**
49 * Get Credential
50 *
51 * @return \PayPal\Auth\OAuthTokenCredential
52 */
53 public function getCredential()
54 {
55 if ($this->credential == null) {
56 return PayPalCredentialManager::getInstance()->getCredentialObject();
57 }
58 return $this->credential;
59 }
60
61 public function getRequestHeaders()
62 {
63 $result = PayPalConfigManager::getInstance()->get('http.headers');
64 $headers = array();
65 foreach ($result as $header => $value) {
66 $headerName = ltrim($header, 'http.headers');
67 $headers[$headerName] = $value;
68 }
69 return $headers;
70 }
71
72 public function addRequestHeader($name, $value)
73 {
74 // Determine if the name already has a 'http.headers' prefix. If not, add one.
75 if (!(substr($name, 0, strlen('http.headers')) === 'http.headers')) {
76 $name = 'http.headers.' . $name;
77 }
78 PayPalConfigManager::getInstance()->addConfigs(array($name => $value));
79 }
80
81 /**
82 * Get Request ID
83 *
84 * @return string
85 */
86 public function getRequestId()
87 {
88 return $this->requestId;
89 }
90
91 /**
92 * Sets the request ID
93 *
94 * @param string $requestId the PayPal-Request-Id value to use
95 */
96 public function setRequestId($requestId)
97 {
98 $this->requestId = $requestId;
99 }
100
101 /**
102 * Resets the requestId that can be used to set the PayPal-request-id
103 * header used for idempotency. In cases where you need to make multiple create calls
104 * using the same ApiContext object, you need to reset request Id.
105 * @deprecated Call setRequestId with a unique value.
106 *
107 * @return string
108 */
109 public function resetRequestId()
110 {
111 $this->requestId = $this->generateRequestId();
112 return $this->getRequestId();
113 }
114
115 /**
116 * Sets Config
117 *
118 * @param array $config SDK configuration parameters
119 */
120 public function setConfig(array $config)
121 {
122 PayPalConfigManager::getInstance()->addConfigs($config);
123 }
124
125 /**
126 * Gets Configurations
127 *
128 * @return array
129 */
130 public function getConfig()
131 {
132 return PayPalConfigManager::getInstance()->getConfigHashmap();
133 }
134
135 /**
136 * Gets a specific configuration from key
137 *
138 * @param $searchKey
139 * @return mixed
140 */
141 public function get($searchKey)
142 {
143 return PayPalConfigManager::getInstance()->get($searchKey);
144 }
145
146 /**
147 * Generates a unique per request id that
148 * can be used to set the PayPal-Request-Id header
149 * that is used for idempotency
150 * @deprecated
151 *
152 * @return string
153 */
154 private function generateRequestId()
155 {
156 static $pid = -1;
157 static $addr = -1;
158
159 if ($pid == -1) {
160 $pid = getmypid();
161 }
162
163 if ($addr == -1) {
164 if (array_key_exists('SERVER_ADDR', $_SERVER)) {
165 $addr = ip2long($_SERVER['SERVER_ADDR']);
166 } else {
167 $addr = php_uname('n');
168 }
169 }
170
171 return $addr . $pid . $_SERVER['REQUEST_TIME'] . mt_rand(0, 0xffff);
172 }
173 }
174