1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ciromattia\Teamwork; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as Guzzle; |
6
|
|
|
use Ciromattia\Teamwork\Contracts\RequestableInterface; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
class Client implements RequestableInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Guzzle |
14
|
|
|
*/ |
15
|
|
|
protected $client; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var RequestInterface |
19
|
|
|
*/ |
20
|
|
|
protected $request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ResponseInterface |
24
|
|
|
*/ |
25
|
|
|
protected $response; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* API Key |
29
|
|
|
* |
30
|
|
|
* The custom API key provided by Teamwork |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $key; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* URL |
38
|
|
|
* |
39
|
|
|
* The URL that is set to query the Teamwork API. |
40
|
|
|
* This is the account URL used to access the project |
41
|
|
|
* management system. This is passed in on construct. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $url; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Currently this package doesn't support XML |
49
|
|
|
* but overtime this would be part of that support |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $dataFormat = 'json'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Guzzle $client |
57
|
|
|
* @param $key |
58
|
|
|
* @param $url |
59
|
|
|
*/ |
60
|
|
|
public function __construct(Guzzle $client, $key, $url) |
61
|
|
|
{ |
62
|
|
|
$this->client = $client; |
63
|
|
|
$this->key = $key; |
64
|
|
|
$this->url = $url; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get |
69
|
|
|
* |
70
|
|
|
* @param $endpoint |
71
|
|
|
* |
72
|
|
|
* @return Client |
73
|
|
|
*/ |
74
|
|
|
public function get($endpoint, $query = null) |
75
|
|
|
{ |
76
|
|
|
$this->buildRequest($endpoint, 'GET', [], $query); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Post |
83
|
|
|
* |
84
|
|
|
* @param $endpoint |
85
|
|
|
* @param $data |
86
|
|
|
* |
87
|
|
|
* @return Client |
88
|
|
|
*/ |
89
|
|
|
public function post($endpoint, $data) |
90
|
|
|
{ |
91
|
|
|
return $this->buildRequest($endpoint, 'POST', $data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Put |
96
|
|
|
* |
97
|
|
|
* @param $endpoint |
98
|
|
|
* @param $data |
99
|
|
|
* |
100
|
|
|
* @return Client |
101
|
|
|
*/ |
102
|
|
|
public function put($endpoint, $data = []) |
103
|
|
|
{ |
104
|
|
|
return $this->buildRequest($endpoint, 'PUT', $data); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Delete |
109
|
|
|
* |
110
|
|
|
* @param $endpoint |
111
|
|
|
* |
112
|
|
|
* @return Client |
113
|
|
|
* @internal param $data |
114
|
|
|
* |
115
|
|
|
*/ |
116
|
|
|
public function delete($endpoint) |
117
|
|
|
{ |
118
|
|
|
return $this->buildRequest($endpoint, 'DELETE'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Build Request |
123
|
|
|
* |
124
|
|
|
* build up request including authentication, body, |
125
|
|
|
* and string queries if necessary. This is where the bulk |
126
|
|
|
* of the data is build up to connect to Teamwork with. |
127
|
|
|
* |
128
|
|
|
* @param $endpoint |
129
|
|
|
* @param string $action |
130
|
|
|
* @param array $params |
131
|
|
|
* @throws |
132
|
|
|
* |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function buildRequest($endpoint, $action, $params = [], $query = null) |
136
|
|
|
{ |
137
|
|
|
if (count($params) > 0) { |
138
|
|
|
$params = json_encode($params); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$options = ['auth' => [$this->key, 'X']]; |
142
|
|
|
|
143
|
|
|
if ($query != null) { |
144
|
|
|
$options['query'] = $query; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($action == 'POST' | $action == 'PUT') { |
|
|
|
|
148
|
|
|
$options = array_merge(['body' => $params], $options); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->request = $this->client->request($action, |
|
|
|
|
152
|
|
|
$this->buildUrl($endpoint), $options |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Response |
160
|
|
|
* |
161
|
|
|
* this send the request from the built response and |
162
|
|
|
* returns the response as a JSON stdClass. |
163
|
|
|
*/ |
164
|
|
|
public function response($fullResponse = false) |
165
|
|
|
{ |
166
|
|
|
if ($fullResponse) { |
167
|
|
|
return $this->request; |
168
|
|
|
} |
169
|
|
|
$this->response = $this->request->getBody(); |
|
|
|
|
170
|
|
|
$payload = json_decode($this->response->getContents()); |
171
|
|
|
|
172
|
|
|
return ($payload instanceof \stdClass || is_array($payload)) ? $payload : null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Build Url |
177
|
|
|
* |
178
|
|
|
* builds the url to make the request to Teamwork with |
179
|
|
|
* and passes it into Guzzle. Also checks if trailing slash |
180
|
|
|
* is present. |
181
|
|
|
* |
182
|
|
|
* @param $endpoint |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function buildUrl($endpoint) |
187
|
|
|
{ |
188
|
|
|
if (filter_var($endpoint, FILTER_VALIDATE_URL)) { |
189
|
|
|
return $endpoint . '.' . $this->dataFormat; |
190
|
|
|
} |
191
|
|
|
if (substr($this->url, -1) != '/') { |
192
|
|
|
$this->url = $this->url . '/'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $this->url . $endpoint . '.' . $this->dataFormat; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get Request |
200
|
|
|
* |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
public function getRequest() |
204
|
|
|
{ |
205
|
|
|
return $this->request; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.
Let’s take a look at these examples: