Path: blob/master/src/applications/metamta/future/PhabricatorTwilioFuture.php
12256 views
<?php12final class PhabricatorTwilioFuture extends FutureProxy {34private $future;5private $accountSID;6private $authToken;7private $method;8private $parameters;9private $timeout;1011public function __construct() {12parent::__construct(null);13}1415public function setAccountSID($account_sid) {16$this->accountSID = $account_sid;17return $this;18}1920public function setAuthToken(PhutilOpaqueEnvelope $token) {21$this->authToken = $token;22return $this;23}2425public function setMethod($method, array $parameters) {26$this->method = $method;27$this->parameters = $parameters;28return $this;29}3031public function setTimeout($timeout) {32$this->timeout = $timeout;33return $this;34}3536public function getTimeout() {37return $this->timeout;38}3940protected function getProxiedFuture() {41if (!$this->future) {42if ($this->accountSID === null) {43throw new PhutilInvalidStateException('setAccountSID');44}4546if ($this->authToken === null) {47throw new PhutilInvalidStateException('setAuthToken');48}4950if ($this->method === null || $this->parameters === null) {51throw new PhutilInvalidStateException('setMethod');52}5354$path = urisprintf(55'/%s/Accounts/%s/%s',56'2010-04-01',57$this->accountSID,58$this->method);5960$uri = id(new PhutilURI('https://api.twilio.com/'))61->setPath($path);6263$data = $this->parameters;6465$future = id(new HTTPSFuture($uri, $data))66->setHTTPBasicAuthCredentials($this->accountSID, $this->authToken)67->setMethod('POST')68->addHeader('Accept', 'application/json');6970$timeout = $this->getTimeout();71if ($timeout) {72$future->setTimeout($timeout);73}7475$this->future = $future;76}7778return $this->future;79}8081protected function didReceiveResult($result) {82list($status, $body, $headers) = $result;8384if ($status->isError()) {85throw $status;86}8788try {89$data = phutil_json_decode($body);90} catch (PhutilJSONParserException $ex) {91throw new PhutilProxyException(92pht('Expected JSON response from Twilio.'),93$ex);94}9596return $data;97}9899}100101102