~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to libphutil/src/conduit/ConduitFuture.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • mfrom: (0.2.1) (0.1.1)
  • Revision ID: package-import@ubuntu.com-20141023204926-vq80u1op4df44azb
Tags: 0~git20141023-1
Initial release (closes: #703046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
final class ConduitFuture extends FutureProxy {
 
4
 
 
5
  protected $client;
 
6
  protected $conduitMethod;
 
7
  private $profilerCallID;
 
8
 
 
9
  public function setClient(ConduitClient $client, $method) {
 
10
    $this->client = $client;
 
11
    $this->conduitMethod = $method;
 
12
    return $this;
 
13
  }
 
14
 
 
15
  public function beginProfile($data) {
 
16
    $profiler = PhutilServiceProfiler::getInstance();
 
17
    $this->profilerCallID = $profiler->beginServiceCall(
 
18
      array(
 
19
        'type'    => 'conduit',
 
20
        'method'  => $this->conduitMethod,
 
21
        'size'    => strlen(http_build_query($data, '', '&')),
 
22
      ));
 
23
    return $this;
 
24
  }
 
25
 
 
26
  protected function didReceiveResult($result) {
 
27
    if ($this->profilerCallID !== null) {
 
28
      $profiler = PhutilServiceProfiler::getInstance();
 
29
      $profiler->endServiceCall(
 
30
        $this->profilerCallID,
 
31
        array());
 
32
    }
 
33
 
 
34
    list($status, $body, $headers) = $result;
 
35
    if ($status->isError()) {
 
36
      throw $status;
 
37
    }
 
38
 
 
39
    $raw = $body;
 
40
 
 
41
    $shield = 'for(;;);';
 
42
    if (!strncmp($raw, $shield, strlen($shield))) {
 
43
      $raw = substr($raw, strlen($shield));
 
44
    }
 
45
 
 
46
    $data = json_decode($raw, true);
 
47
    if (!is_array($data)) {
 
48
      throw new Exception(
 
49
        "Host returned HTTP/200, but invalid JSON data in response to ".
 
50
        "a Conduit method call:\n{$raw}");
 
51
    }
 
52
 
 
53
    if ($data['error_code']) {
 
54
      throw new ConduitClientException(
 
55
        $data['error_code'],
 
56
        $data['error_info']);
 
57
    }
 
58
 
 
59
    $result = $data['result'];
 
60
 
 
61
    $result = $this->client->didReceiveResponse(
 
62
      $this->conduitMethod,
 
63
      $result);
 
64
 
 
65
    return $result;
 
66
  }
 
67
 
 
68
}