Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/future/PhabricatorTwilioFuture.php
12256 views
1
<?php
2
3
final class PhabricatorTwilioFuture extends FutureProxy {
4
5
private $future;
6
private $accountSID;
7
private $authToken;
8
private $method;
9
private $parameters;
10
private $timeout;
11
12
public function __construct() {
13
parent::__construct(null);
14
}
15
16
public function setAccountSID($account_sid) {
17
$this->accountSID = $account_sid;
18
return $this;
19
}
20
21
public function setAuthToken(PhutilOpaqueEnvelope $token) {
22
$this->authToken = $token;
23
return $this;
24
}
25
26
public function setMethod($method, array $parameters) {
27
$this->method = $method;
28
$this->parameters = $parameters;
29
return $this;
30
}
31
32
public function setTimeout($timeout) {
33
$this->timeout = $timeout;
34
return $this;
35
}
36
37
public function getTimeout() {
38
return $this->timeout;
39
}
40
41
protected function getProxiedFuture() {
42
if (!$this->future) {
43
if ($this->accountSID === null) {
44
throw new PhutilInvalidStateException('setAccountSID');
45
}
46
47
if ($this->authToken === null) {
48
throw new PhutilInvalidStateException('setAuthToken');
49
}
50
51
if ($this->method === null || $this->parameters === null) {
52
throw new PhutilInvalidStateException('setMethod');
53
}
54
55
$path = urisprintf(
56
'/%s/Accounts/%s/%s',
57
'2010-04-01',
58
$this->accountSID,
59
$this->method);
60
61
$uri = id(new PhutilURI('https://api.twilio.com/'))
62
->setPath($path);
63
64
$data = $this->parameters;
65
66
$future = id(new HTTPSFuture($uri, $data))
67
->setHTTPBasicAuthCredentials($this->accountSID, $this->authToken)
68
->setMethod('POST')
69
->addHeader('Accept', 'application/json');
70
71
$timeout = $this->getTimeout();
72
if ($timeout) {
73
$future->setTimeout($timeout);
74
}
75
76
$this->future = $future;
77
}
78
79
return $this->future;
80
}
81
82
protected function didReceiveResult($result) {
83
list($status, $body, $headers) = $result;
84
85
if ($status->isError()) {
86
throw $status;
87
}
88
89
try {
90
$data = phutil_json_decode($body);
91
} catch (PhutilJSONParserException $ex) {
92
throw new PhutilProxyException(
93
pht('Expected JSON response from Twilio.'),
94
$ex);
95
}
96
97
return $data;
98
}
99
100
}
101
102