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