Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / metamta / future / PhabricatorTwilioFuture.php
blob8dc70329f8bc3ed53b315444161919895d3c10c0
1 <?php
3 final class PhabricatorTwilioFuture extends FutureProxy {
5 private $future;
6 private $accountSID;
7 private $authToken;
8 private $method;
9 private $parameters;
10 private $timeout;
12 public function __construct() {
13 parent::__construct(null);
16 public function setAccountSID($account_sid) {
17 $this->accountSID = $account_sid;
18 return $this;
21 public function setAuthToken(PhutilOpaqueEnvelope $token) {
22 $this->authToken = $token;
23 return $this;
26 public function setMethod($method, array $parameters) {
27 $this->method = $method;
28 $this->parameters = $parameters;
29 return $this;
32 public function setTimeout($timeout) {
33 $this->timeout = $timeout;
34 return $this;
37 public function getTimeout() {
38 return $this->timeout;
41 protected function getProxiedFuture() {
42 if (!$this->future) {
43 if ($this->accountSID === null) {
44 throw new PhutilInvalidStateException('setAccountSID');
47 if ($this->authToken === null) {
48 throw new PhutilInvalidStateException('setAuthToken');
51 if ($this->method === null || $this->parameters === null) {
52 throw new PhutilInvalidStateException('setMethod');
55 $path = urisprintf(
56 '/%s/Accounts/%s/%s',
57 '2010-04-01',
58 $this->accountSID,
59 $this->method);
61 $uri = id(new PhutilURI('https://api.twilio.com/'))
62 ->setPath($path);
64 $data = $this->parameters;
66 $future = id(new HTTPSFuture($uri, $data))
67 ->setHTTPBasicAuthCredentials($this->accountSID, $this->authToken)
68 ->setMethod('POST')
69 ->addHeader('Accept', 'application/json');
71 $timeout = $this->getTimeout();
72 if ($timeout) {
73 $future->setTimeout($timeout);
76 $this->future = $future;
79 return $this->future;
82 protected function didReceiveResult($result) {
83 list($status, $body, $headers) = $result;
85 if ($status->isError()) {
86 throw $status;
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);
97 return $data;