1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\Favro\Api; |
4
|
|
|
|
5
|
|
|
use seregazhuk\Favro\Api\Endpoints\Cards; |
6
|
|
|
use seregazhuk\Favro\Api\Endpoints\Comments; |
7
|
|
|
use seregazhuk\Favro\Api\Endpoints\TaskLists; |
8
|
|
|
use seregazhuk\Favro\Api\Endpoints\Tasks; |
9
|
|
|
use seregazhuk\Favro\Api\Endpoints\Users; |
10
|
|
|
use seregazhuk\Favro\Api\Endpoints\Columns; |
11
|
|
|
use seregazhuk\Favro\Api\Endpoints\Widgets; |
12
|
|
|
use seregazhuk\Favro\Api\Endpoints\Endpoint; |
13
|
|
|
use seregazhuk\Favro\Api\Endpoints\Collections; |
14
|
|
|
use seregazhuk\Favro\Api\Endpoints\Organizations; |
15
|
|
|
use seregazhuk\Favro\Contracts\HttpClient; |
16
|
|
|
use seregazhuk\Favro\Exceptions\WrongEndpoint; |
17
|
|
|
use seregazhuk\Favro\Exceptions\WrongOrganizationName; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Api |
21
|
|
|
* |
22
|
|
|
* @property Organizations $organizations |
23
|
|
|
* @property Users $users |
24
|
|
|
* @property Collections $collections |
25
|
|
|
* @property Widgets $widgets |
26
|
|
|
* @property Columns $columns |
27
|
|
|
* @property Cards $cards |
28
|
|
|
* @property Tasks $tasks |
29
|
|
|
* @property TaskLists $tasklists |
30
|
|
|
* @property Comments $comments |
31
|
|
|
*/ |
32
|
|
|
class Api |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $organizationId; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var HttpClient |
41
|
|
|
*/ |
42
|
|
|
private $httpClient; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Endpoint[] |
46
|
|
|
*/ |
47
|
|
|
private $endpoints; |
48
|
|
|
|
49
|
|
|
public function __construct(HttpClient $httpClient, Endpoint ...$endpoints) |
50
|
|
|
{ |
51
|
|
|
$this->httpClient = $httpClient; |
52
|
|
|
foreach ($endpoints as $endpoint) { |
53
|
|
|
$this->endpoints[$endpoint->endpoint()] = $endpoint; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Magic method to access different endpoints. |
59
|
|
|
* |
60
|
|
|
* @param string $endpoint |
61
|
|
|
* |
62
|
|
|
* @return Endpoint |
63
|
|
|
* @throws WrongEndpoint |
64
|
|
|
*/ |
65
|
|
|
public function __get($endpoint) |
66
|
|
|
{ |
67
|
|
|
$endpoint = $this->resolveEndpoint($endpoint); |
68
|
|
|
|
69
|
|
|
if (method_exists($endpoint, 'setOrganizationId')) { |
70
|
|
|
$endpoint->setOrganizationId($this->organizationId); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $endpoint; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $organizationName |
78
|
|
|
* @return $this |
79
|
|
|
* @throws WrongOrganizationName |
80
|
|
|
*/ |
81
|
|
|
public function setOrganization($organizationName) |
82
|
|
|
{ |
83
|
|
|
if($organization = $this->getOrganizationByName($organizationName)) { |
84
|
|
|
$this->setOrganizationId($organization['organizationId']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int $organizationId |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setOrganizationId($organizationId) |
95
|
|
|
{ |
96
|
|
|
$this->organizationId = $organizationId; |
|
|
|
|
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getOrganizationId() |
105
|
|
|
{ |
106
|
|
|
return $this->organizationId; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $organizationName |
111
|
|
|
* @return array|bool |
112
|
|
|
* @throws WrongOrganizationName |
113
|
|
|
*/ |
114
|
|
|
private function getOrganizationByName($organizationName) |
115
|
|
|
{ |
116
|
|
|
$organizations = $this->organizations->getAll(); |
117
|
|
|
foreach ($organizations['entities'] as $entity) { |
118
|
|
|
if ($entity['name'] == $organizationName) { |
119
|
|
|
return $entity; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
throw new WrongOrganizationName("Organization $organizationName not found!"); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $endpoint |
128
|
|
|
* @return Endpoint |
129
|
|
|
* @throws WrongEndpoint |
130
|
|
|
*/ |
131
|
|
|
private function resolveEndpoint($endpoint) |
132
|
|
|
{ |
133
|
|
|
$endpoint = strtolower($endpoint); |
134
|
|
|
|
135
|
|
|
if(isset($this->endpoints[$endpoint])) { |
136
|
|
|
return $this->endpoints[$endpoint]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
throw new WrongEndpoint("There is no endpoint called $endpoint."); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function getRateInfo() |
146
|
|
|
{ |
147
|
|
|
$responseHeaders = $this->httpClient->getResponseHeaders(); |
148
|
|
|
|
149
|
|
|
return [ |
150
|
|
|
'reset' => $responseHeaders['X-RateLimit-Reset'][0], |
151
|
|
|
'limit' => $responseHeaders['X-RateLimit-Limit'][0], |
152
|
|
|
'remaining' => $responseHeaders['X-RateLimit-Remaining'][0], |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.