Total Complexity | 58 |
Total Lines | 356 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Client |
||
13 | { |
||
14 | private $apiUrl = 'https://api.proposalpage.com'; |
||
15 | |||
16 | private $token = null; |
||
17 | |||
18 | private $httpClient; |
||
19 | |||
20 | public function __construct(string $apiUrl = '') |
||
30 | ]); |
||
31 | } |
||
32 | |||
33 | // Auth |
||
34 | public function authenticate(string $username, string $password, bool $setToken = true) |
||
35 | { |
||
36 | $response = $this->request('POST', '/accounts/auth/token', [ |
||
37 | 'username' => $username, |
||
38 | 'password' => $password |
||
39 | ]); |
||
40 | |||
41 | if ($setToken) { |
||
42 | $this->token = $response->json['token']; |
||
43 | } |
||
44 | |||
45 | return $response; |
||
46 | } |
||
47 | |||
48 | public function authMe() |
||
51 | } |
||
52 | |||
53 | // Templates |
||
54 | public function listTemplates($page = 1, $itemsPerPage = 6) |
||
55 | { |
||
56 | return $this->request('GET', '/projects/templates', [], [ |
||
57 | 'page' => $page, |
||
58 | 'itemsPerPage' => $itemsPerPage |
||
59 | ]); |
||
60 | } |
||
61 | |||
62 | // Projects |
||
63 | public function createProject(array $params) |
||
64 | { |
||
65 | return $this->request('POST', '/projects', $params); |
||
66 | } |
||
67 | |||
68 | public function createProjectFromTemplate(string $templateId) |
||
71 | } |
||
72 | |||
73 | public function listProjects($page = 1, $itemsPerPage = 6, $title = null) |
||
74 | { |
||
75 | $query = [ |
||
76 | 'page' => $page, |
||
77 | 'itemsPerPage' => $itemsPerPage |
||
78 | ]; |
||
79 | |||
80 | if ($title) { |
||
81 | $query['title'] = $title; |
||
82 | } |
||
83 | |||
84 | return $this->request('GET', '/projects', [], $query); |
||
85 | } |
||
86 | |||
87 | public function listProject($projectId) |
||
88 | { |
||
89 | return $this->request('GET', "/projects/{$projectId}"); |
||
90 | } |
||
91 | |||
92 | public function updateProject($projectId, array $params) |
||
93 | { |
||
94 | return $this->request('PUT', "/projects/{$projectId}", $params); |
||
95 | } |
||
96 | |||
97 | public function deleteProject($projectId) |
||
98 | { |
||
99 | return $this->request('DELETE', "/projects/{$projectId}"); |
||
100 | } |
||
101 | |||
102 | public function cloneProject($projectId) |
||
103 | { |
||
104 | return $this->request('POST', "/projects/{$projectId}/clone"); |
||
105 | } |
||
106 | |||
107 | public function setProjectPassword($projectId, string $password) |
||
108 | { |
||
109 | return $this->request('POST', "/projects/{$projectId}/password", ['password' => $password]); |
||
110 | } |
||
111 | |||
112 | public function publishProject($projectId) |
||
113 | { |
||
114 | return $this->request('POST', "/projects/{$projectId}/publish"); |
||
115 | } |
||
116 | |||
117 | public function secureProject($projectId) |
||
120 | } |
||
121 | |||
122 | public function generateProjectCover($projectId) |
||
123 | { |
||
124 | return $this->request('GET', "/projects/{$projectId}/screenshot"); |
||
125 | } |
||
126 | |||
127 | public function viewProjectAndNotify($projectId) |
||
128 | { |
||
129 | return $this->request('PUT', "/projects/{$projectId}/view-and-notify"); |
||
130 | } |
||
131 | |||
132 | // Blocks |
||
133 | public function createBlock($projectId, array $params) |
||
134 | { |
||
135 | return $this->request('POST', "/projects/{$projectId}/blocks", $params); |
||
136 | } |
||
137 | |||
138 | public function listBlocks($projectId) |
||
139 | { |
||
140 | return $this->request('GET', "/projects/{$projectId}/blocks"); |
||
141 | } |
||
142 | |||
143 | public function listBlock($projectId, $blockId) |
||
144 | { |
||
145 | return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}"); |
||
146 | } |
||
147 | |||
148 | public function updateBlock($projectId, $blockId, array $params) |
||
149 | { |
||
150 | return $this->request('PUT', "/projects/{$projectId}/blocks/{$blockId}", $params); |
||
151 | } |
||
152 | |||
153 | public function moveBlockForward($projectId, $blockId) |
||
154 | { |
||
155 | return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/forward"); |
||
156 | } |
||
157 | |||
158 | public function moveBlockBackward($projectId, $blockId) |
||
159 | { |
||
160 | return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/backward"); |
||
161 | } |
||
162 | |||
163 | public function cloneBlock($projectId, $blockId, string $position = '') |
||
164 | { |
||
165 | return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/clone/{$position}"); |
||
166 | } |
||
167 | |||
168 | public function deleteBlock($projectId, $blockId) |
||
169 | { |
||
170 | return $this->request('DELETE', "/projects/{$projectId}/blocks/{$blockId}"); |
||
171 | } |
||
172 | |||
173 | // Rows |
||
174 | public function createRow($projectId, $blockId, array $params) |
||
175 | { |
||
176 | return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/rows", $params); |
||
177 | } |
||
178 | |||
179 | public function listRows($projectId, $blockId) |
||
180 | { |
||
181 | return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows"); |
||
182 | } |
||
183 | |||
184 | public function listRow($projectId, $blockId, $rowId) |
||
185 | { |
||
186 | return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}"); |
||
187 | } |
||
188 | |||
189 | public function updateRow($projectId, $blockId, $rowId, $params) |
||
190 | { |
||
191 | return $this->request('PUT', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}", $params); |
||
192 | } |
||
193 | |||
194 | public function cloneRow($projectId, $blockId, $rowId, string $position = '') |
||
195 | { |
||
196 | return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/clone/${position}"); |
||
197 | } |
||
198 | |||
199 | public function deleteRow($projectId, $blockId, $rowId) |
||
200 | { |
||
201 | return $this->request('DELETE', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}"); |
||
202 | } |
||
203 | |||
204 | // Columns |
||
205 | public function createColumn($projectId, $blockId, $rowId, array $params) |
||
206 | { |
||
207 | return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns", $params); |
||
208 | } |
||
209 | |||
210 | public function listColumns($projectId, $blockId, $rowId) |
||
211 | { |
||
212 | return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns"); |
||
213 | } |
||
214 | |||
215 | public function listColumn($projectId, $blockId, $rowId, $columnId) |
||
216 | { |
||
217 | return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}"); |
||
218 | } |
||
219 | |||
220 | public function updateColumn($projectId, $blockId, $rowId, $columnId, array $params) |
||
221 | { |
||
222 | return $this->request('PUT', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}", $params); |
||
223 | } |
||
224 | |||
225 | public function deleteColumn($projectId, $blockId, $rowId, $columnId) |
||
226 | { |
||
227 | return $this->request('DELETE', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}"); |
||
228 | } |
||
229 | |||
230 | // Contents |
||
231 | public function createContent($projectId, $blockId, $rowId, $columnId, array $params) |
||
237 | ); |
||
238 | } |
||
239 | |||
240 | public function listContents($projectId, $blockId, $rowId, $columnId) |
||
241 | { |
||
242 | return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}/contents"); |
||
243 | } |
||
244 | |||
245 | public function listContent($projectId, $blockId, $rowId, $columnId, $contentId) |
||
246 | { |
||
247 | return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}/contents/{$contentId}"); |
||
248 | } |
||
249 | |||
250 | public function updateContent($projectId, $blockId, $rowId, $columnId, $contentId, array $params) |
||
251 | { |
||
252 | return $this->request( |
||
253 | 'PUT', |
||
254 | "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}/contents/{$contentId}", |
||
255 | $params |
||
256 | ); |
||
257 | } |
||
258 | |||
259 | public function deleteContent($projectId, $blockId, $rowId, $columnId, $contentId) |
||
260 | { |
||
261 | return $this->request('DELETE', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}/contents/{$contentId}"); |
||
262 | } |
||
263 | |||
264 | // Getters / Setters |
||
265 | public function getApiUrl() |
||
266 | { |
||
267 | return $this->apiUrl; |
||
268 | } |
||
269 | |||
270 | public function setApiUrl(string $apiUrl) |
||
271 | { |
||
272 | $this->apiUrl = $apiUrl; |
||
273 | |||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | public function getToken() |
||
280 | } |
||
281 | |||
282 | public function setToken(string $token) |
||
283 | { |
||
284 | $this->token = $token; |
||
285 | |||
286 | return $this; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @param string $method |
||
291 | * @param string $path |
||
292 | * @param array $params |
||
293 | * @param array $queryStringParams |
||
294 | * @return mixed|ResponseInterface |
||
295 | * @throws FailedActionException |
||
296 | * @throws NotFoundException |
||
297 | * @throws ValidationException |
||
298 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
299 | */ |
||
300 | private function request(string $method, string $path, array $params = [], array $queryStringParams = []) |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @return array |
||
320 | */ |
||
321 | private function getRequestHeaders() |
||
322 | { |
||
323 | if (!$this->token) { |
||
324 | return [ |
||
325 | 'Accept' => 'application/json', |
||
326 | 'Content-Type' => 'application/json', |
||
327 | ]; |
||
328 | } |
||
329 | |||
330 | return [ |
||
331 | 'Authorization' => "Bearer {$this->token}", |
||
332 | 'Accept' => 'application/json', |
||
334 | ]; |
||
335 | } |
||
336 | |||
337 | private function isSuccessfulResponse($response) |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @param ResponseInterface $response |
||
348 | * @throws FailedActionException |
||
349 | * @throws NotFoundException |
||
350 | * @throws ValidationException |
||
351 | * @throws Exception |
||
352 | */ |
||
353 | private function handleRequestError(ResponseInterface $response) |
||
368 | } |
||
369 | } |
||
370 |