Completed
Pull Request — master (#76)
by Thibaud
02:50
created

ApiResponse   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 163
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A getResult() 0 4 1
A getStatusCode() 0 4 1
A isOk() 0 4 1
A isEmpty() 0 4 1
A getErrorMessage() 0 4 1
A getErrorDetails() 0 4 1
A getResponseTime() 0 4 1
A getUri() 0 6 1
A getMethod() 0 6 1
A getCharset() 0 4 1
A getApiVersion() 0 4 1
A hasProperty() 0 4 1
A getProperty() 0 4 2
1
<?php
2
3
/*
4
 * This file is part of Phraseanet SDK.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhraseanetSDK\Http;
13
14
use PhraseanetSDK\Exception\InvalidArgumentException;
15
16
/**
17
 * Response object from a Phraseanet API call
18
 */
19
class ApiResponse
20
{
21
    /**
22
     * @var \stdClass
23
     */
24
    protected $result;
25
26
    /**
27
     * @var \stdClass
28
     */
29
    private $meta;
30 71
31
    /**
32 71
     * @param \stdClass $response
33 1
     */
34
    public function __construct(\stdClass $response)
35
    {
36 70
        if (!isset($response->meta) || !isset($response->response)) {
37 70
            throw new InvalidArgumentException('Malformed API response');
38 70
        }
39
40
        $this->meta = $response->meta;
41
        $this->result = $response->response;
42
    }
43
44
    /**
45 16
     * Returns the result of the response
46
     *
47 16
     * @return \stdClass
48
     */
49
    public function getResult()
50
    {
51
        return $this->result;
52
    }
53
54
    /**
55 2
     * Returns the HTTP code
56
     *
57 2
     * @return integer
58
     */
59
    public function getStatusCode()
60
    {
61
        return (int)$this->meta->http_code;
62
    }
63
64
    /**
65 1
     * Returns true is the response is successful
66
     *
67 1
     * @return Boolean
68
     */
69
    public function isOk()
70
    {
71
        return $this->getStatusCode() < 400;
72
    }
73
74
    /**
75 4
     * Returns true if the response content is empty
76
     *
77 4
     * @return Boolean
78
     */
79
    public function isEmpty()
80
    {
81
        return count(get_object_vars($this->result)) === 0;
82
    }
83
84
    /**
85 1
     * Returns the error message
86
     *
87 1
     * @return string|null
88
     */
89
    public function getErrorMessage()
90
    {
91
        return $this->meta->error_message;
92
    }
93
94
    /**
95 1
     * Returns error details
96
     *
97 1
     * @return string|null
98
     */
99
    public function getErrorDetails()
100
    {
101
        return $this->meta->error_details;
102
    }
103
104
    /**
105 1
     * Returns the response datetime
106
     *
107 1
     * @return \DateTime
108
     */
109
    public function getResponseTime()
110
    {
111
        return \DateTime::createFromFormat(DATE_ATOM, $this->meta->response_time);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor...->meta->response_time); of type DateTime|false adds false to the return on line 111 which is incompatible with the return type documented by PhraseanetSDK\Http\ApiResponse::getResponseTime of type DateTime. It seems like you forgot to handle an error condition.
Loading history...
112
    }
113
114
    /**
115 1
     * Returns the requested URI
116
     *
117 1
     * @return string
118
     */
119 1
    public function getUri()
120
    {
121
        $request = explode(' ', $this->meta->request);
122
123
        return $request[1];
124
    }
125
126
    /**
127 1
     * Returns the requested method
128
     *
129 1
     * @return string
130
     */
131 1
    public function getMethod()
132
    {
133
        $request = explode(' ', $this->meta->request);
134
135
        return $request[0];
136
    }
137
138
    /**
139 1
     * Returns the response charset
140
     *
141 1
     * @return string
142
     */
143
    public function getCharset()
144
    {
145
        return $this->meta->charset;
146
    }
147
148
    /**
149 1
     * Returns the API version
150
     *
151 1
     * @return string
152
     */
153
    public function getApiVersion()
154
    {
155
        return $this->meta->api_version;
156
    }
157
158
    /**
159
     * Returns true if the response has the given property
160
     *
161 49
     * @param string $property The property name
162
     *
163 49
     * @return Boolean
164
     */
165
    public function hasProperty($property)
166
    {
167
        return property_exists($this->result, $property);
168
    }
169
170
    /**
171
     * Returns the response property, null if the property does not exist
172
     *
173 28
     * @param string $property The property name
174
     *
175 28
     * @return \stdClass|\stdClass[]|null
176
     */
177
    public function getProperty($property)
178
    {
179
        return $this->hasProperty($property) ? $this->result->{$property} : null;
180
    }
181
}
182