Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
HttpPaginatedResponse
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
 parseHeaders
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2namespace Ably\Models;
3
4use Ably\Exceptions\AblyRequestException;
5
6/**
7 * This class is used as a container for response data from AblyRest::request
8 * It provides automatic pagination.
9 */
10class HttpPaginatedResponse extends PaginatedResult {
11    /**
12     * @var integer HTTP status code of the page
13     */
14    public $statusCode;
15
16    /**
17     * @var boolean True when the HTTP status code indicates sucess (200 <= statusCode < 300)
18     */
19    public $success;
20
21    /**
22     * @var integer Ably error code
23     */
24    public $errorCode;
25
26    /**
27     * @var string Error message
28     */
29    public $errorMessage;
30
31    /**
32     * @var Array Array of key value pairs for each response header
33     */
34    public $headers;
35
36    /**
37     * Constructor.
38     * @param \Ably\AblyRest $ably Ably API instance
39     * @param mixed $model Name of a class that will be instantiated for returned results. It must implement a fromJSON() method.
40     * @param CipherParams|null $cipherParams Optional cipher parameters if data should be decoded
41     * @param string $method HTTP method
42     * @param string $path Request path
43     * @param array $params Parameters to be sent with the request
44     * @param array $headers Headers to be sent with the request
45     * @throws AblyRequestException Thrown when the server and all the fallbacks are unreachable
46     */
47    public function __construct( \Ably\AblyRest $ably, $model, $cipherParams, $method, $path, $params = [], $headers = [] ) {
48        try {
49            parent::__construct( $ably, $model, $cipherParams, $method, $path, $params, $headers );
50        } catch (AblyRequestException $ex) {
51            $this->response = $ex->getResponse();
52
53            if ($ex->getCode() >= 50000) { // all fallback hosts failed, rethrow exception
54                throw $ex;
55            }
56        }
57
58        $this->parseHeaders($this->response['headers']);
59
60        if ($this->statusCode < 200 | $this->statusCode >= 300) {
61            $this->success = false;
62
63            if ( isset($this->headers['X-Ably-Errorcode']) ) {
64                $this->errorCode = $this->headers['X-Ably-Errorcode'] * 1;
65            }
66
67            if ( isset($this->headers['X-Ably-Errormessage']) ) {
68                $this->errorMessage = $this->headers['X-Ably-Errormessage'];
69            }
70        } else {
71            $this->success = true;
72        }
73    }
74
75    private function parseHeaders( $headers ) {
76        $headers = explode("\n", $headers);
77        $http = array_shift($headers);
78        $http = explode(' ', $http);
79
80        $this->statusCode = $http[1] * 1;
81        $this->headers = [];
82
83        foreach($headers as $header) {
84            if(!trim($header)) continue;
85            list($key, $value) = explode(':', $header, 2);
86            $this->headers[trim($key)] = trim($value);
87        }
88    }
89}