| Total Complexity | 52 | 
| Total Lines | 313 | 
| 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  | 
            ||
| 9 | class Client  | 
            ||
| 10 | { | 
            ||
| 11 | private $apiUrl = 'https://api.proposalpage.com';  | 
            ||
| 12 | |||
| 13 | private $token = null;  | 
            ||
| 14 | |||
| 15 | private $httpClient;  | 
            ||
| 16 | |||
| 17 | public function __construct(string $apiUrl = '')  | 
            ||
| 24 | }  | 
            ||
| 25 | |||
| 26 | // Auth  | 
            ||
| 27 | public function authenticate(string $username, string $password, bool $setToken = true)  | 
            ||
| 28 |     { | 
            ||
| 29 |         $response = $this->request('POST', '/accounts/auth/token', [ | 
            ||
| 30 | 'username' => $username,  | 
            ||
| 31 | 'password' => $password  | 
            ||
| 32 | ]);  | 
            ||
| 33 | |||
| 34 |         if ($setToken) { | 
            ||
| 35 | $this->token = $response->token;  | 
            ||
| 36 | }  | 
            ||
| 37 | |||
| 38 | return $response;  | 
            ||
| 39 | }  | 
            ||
| 40 | |||
| 41 | public function authMe()  | 
            ||
| 44 | }  | 
            ||
| 45 | |||
| 46 | // Templates  | 
            ||
| 47 | public function listTemplates()  | 
            ||
| 48 |     { | 
            ||
| 49 |         return $this->request('GET', '/projects/templates'); | 
            ||
| 50 | }  | 
            ||
| 51 | |||
| 52 | // Projects  | 
            ||
| 53 | public function createProject(array $params)  | 
            ||
| 54 |     { | 
            ||
| 55 |         return $this->request('POST', '/projects', $params); | 
            ||
| 56 | }  | 
            ||
| 57 | |||
| 58 | public function createProjectFromTemplate(string $templateId)  | 
            ||
| 61 | }  | 
            ||
| 62 | |||
| 63 | public function listProjects($page = 1, $itemsPerPage = 6)  | 
            ||
| 64 |     { | 
            ||
| 65 |         return $this->request('GET', '/projects', [], [ | 
            ||
| 66 | 'page' => $page,  | 
            ||
| 67 | 'itemsPerPage' => $itemsPerPage  | 
            ||
| 68 | ]);  | 
            ||
| 69 | }  | 
            ||
| 70 | |||
| 71 | public function listProject($projectId)  | 
            ||
| 72 |     { | 
            ||
| 73 |         return $this->request('GET', "/projects/{$projectId}"); | 
            ||
| 74 | }  | 
            ||
| 75 | |||
| 76 | public function updateProject($projectId, array $params)  | 
            ||
| 77 |     { | 
            ||
| 78 |         return $this->request('PUT', "/projects/{$projectId}", $params); | 
            ||
| 79 | }  | 
            ||
| 80 | |||
| 81 | public function deleteProject($projectId)  | 
            ||
| 82 |     { | 
            ||
| 83 |         return $this->request('DELETE', "/projects/{$projectId}"); | 
            ||
| 84 | }  | 
            ||
| 85 | |||
| 86 | public function cloneProject($projectId)  | 
            ||
| 87 |     { | 
            ||
| 88 |         return $this->request('POST', "/projects/{$projectId}/clone"); | 
            ||
| 89 | }  | 
            ||
| 90 | |||
| 91 | public function setProjectPassword($projectId, string $password)  | 
            ||
| 92 |     { | 
            ||
| 93 |         return $this->request('POST', "/projects/{$projectId}/password", ['password' => $password]); | 
            ||
| 94 | }  | 
            ||
| 95 | |||
| 96 | public function publishProject($projectId)  | 
            ||
| 97 |     { | 
            ||
| 98 |         return $this->request('POST', "/projects/{$projectId}/publish"); | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | public function secureProject($projectId)  | 
            ||
| 104 | }  | 
            ||
| 105 | |||
| 106 | public function generateProjectCover($projectId)  | 
            ||
| 107 |     { | 
            ||
| 108 |         return $this->request('GET', "/projects/{$projectId}/screenshot"); | 
            ||
| 109 | }  | 
            ||
| 110 | |||
| 111 | public function viewProjectAndNotify($projectId)  | 
            ||
| 112 |     { | 
            ||
| 113 |         return $this->request('PUT', "/projects/{$projectId}/view-and-notify"); | 
            ||
| 114 | }  | 
            ||
| 115 | |||
| 116 | // Blocks  | 
            ||
| 117 | public function createBlock($projectId, array $params)  | 
            ||
| 118 |     { | 
            ||
| 119 |         return $this->request('POST', "/projects/{$projectId}/blocks", $params); | 
            ||
| 120 | }  | 
            ||
| 121 | |||
| 122 | public function listBlocks($projectId)  | 
            ||
| 123 |     { | 
            ||
| 124 |         return $this->request('GET', "/projects/{$projectId}/blocks"); | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | public function listBlock($projectId, $blockId)  | 
            ||
| 128 |     { | 
            ||
| 129 |         return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}"); | 
            ||
| 130 | }  | 
            ||
| 131 | |||
| 132 | public function updateBlock($projectId, $blockId, array $params)  | 
            ||
| 133 |     { | 
            ||
| 134 |         return $this->request('PUT', "/projects/{$projectId}/blocks/{$blockId}", $params); | 
            ||
| 135 | }  | 
            ||
| 136 | |||
| 137 | public function moveBlockForward($projectId, $blockId)  | 
            ||
| 138 |     { | 
            ||
| 139 |         return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/forward"); | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | public function moveBlockBackward($projectId, $blockId)  | 
            ||
| 143 |     { | 
            ||
| 144 |         return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/backward"); | 
            ||
| 145 | }  | 
            ||
| 146 | |||
| 147 | public function cloneBlock($projectId, $blockId, string $position = '')  | 
            ||
| 148 |     { | 
            ||
| 149 |         return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/clone/{$position}"); | 
            ||
| 150 | }  | 
            ||
| 151 | |||
| 152 | public function deleteBlock($projectId, $blockId)  | 
            ||
| 153 |     { | 
            ||
| 154 |         return $this->request('DELETE', "/projects/{$projectId}/blocks/{$blockId}"); | 
            ||
| 155 | }  | 
            ||
| 156 | |||
| 157 | // Rows  | 
            ||
| 158 | public function createRow($projectId, $blockId, array $params)  | 
            ||
| 159 |     { | 
            ||
| 160 |         return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/rows", $params); | 
            ||
| 161 | }  | 
            ||
| 162 | |||
| 163 | public function listRows($projectId, $blockId)  | 
            ||
| 164 |     { | 
            ||
| 165 |         return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows"); | 
            ||
| 166 | }  | 
            ||
| 167 | |||
| 168 | public function listRow($projectId, $blockId, $rowId)  | 
            ||
| 169 |     { | 
            ||
| 170 |         return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}"); | 
            ||
| 171 | }  | 
            ||
| 172 | |||
| 173 | public function updateRow($projectId, $blockId, $rowId, $params)  | 
            ||
| 174 |     { | 
            ||
| 175 |         return $this->request('PUT', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}", $params); | 
            ||
| 176 | }  | 
            ||
| 177 | |||
| 178 | public function cloneRow($projectId, $blockId, $rowId, string $position = '')  | 
            ||
| 179 |     { | 
            ||
| 180 |         return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/clone/${position}"); | 
            ||
| 181 | }  | 
            ||
| 182 | |||
| 183 | public function deleteRow($projectId, $blockId, $rowId)  | 
            ||
| 184 |     { | 
            ||
| 185 |         return $this->request('DELETE', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}"); | 
            ||
| 186 | }  | 
            ||
| 187 | |||
| 188 | // Columns  | 
            ||
| 189 | public function createColumn($projectId, $blockId, $rowId, array $params)  | 
            ||
| 190 |     { | 
            ||
| 191 |         return $this->request('POST', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns", $params); | 
            ||
| 192 | }  | 
            ||
| 193 | |||
| 194 | public function listColumns($projectId, $blockId, $rowId)  | 
            ||
| 195 |     { | 
            ||
| 196 |         return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns"); | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 | public function listColumn($projectId, $blockId, $rowId, $columnId)  | 
            ||
| 200 |     { | 
            ||
| 201 |         return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}"); | 
            ||
| 202 | }  | 
            ||
| 203 | |||
| 204 | public function updateColumn($projectId, $blockId, $rowId, $columnId, array $params)  | 
            ||
| 205 |     { | 
            ||
| 206 |         return $this->request('PUT', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}", $params); | 
            ||
| 207 | }  | 
            ||
| 208 | |||
| 209 | public function deleteColumn($projectId, $blockId, $rowId, $columnId)  | 
            ||
| 210 |     { | 
            ||
| 211 |         return $this->request('DELETE', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}"); | 
            ||
| 212 | }  | 
            ||
| 213 | |||
| 214 | // Contents  | 
            ||
| 215 | public function createContent($projectId, $blockId, $rowId, $columnId, array $params)  | 
            ||
| 221 | );  | 
            ||
| 222 | }  | 
            ||
| 223 | |||
| 224 | public function listContents($projectId, $blockId, $rowId, $columnId)  | 
            ||
| 225 |     { | 
            ||
| 226 |         return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}/contents"); | 
            ||
| 227 | }  | 
            ||
| 228 | |||
| 229 | public function listContent($projectId, $blockId, $rowId, $columnId, $contentId)  | 
            ||
| 230 |     { | 
            ||
| 231 |         return $this->request('GET', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}/contents/{$contentId}"); | 
            ||
| 232 | }  | 
            ||
| 233 | |||
| 234 | public function updateContent($projectId, $blockId, $rowId, $columnId, $contentId, array $params)  | 
            ||
| 235 |     { | 
            ||
| 236 | return $this->request(  | 
            ||
| 237 | 'PUT',  | 
            ||
| 238 |             "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}/contents/{$contentId}", | 
            ||
| 239 | $params  | 
            ||
| 240 | );  | 
            ||
| 241 | }  | 
            ||
| 242 | |||
| 243 | public function deleteContent($projectId, $blockId, $rowId, $columnId, $contentId)  | 
            ||
| 244 |     { | 
            ||
| 245 |         return $this->request('DELETE', "/projects/{$projectId}/blocks/{$blockId}/rows/{$rowId}/columns/{$columnId}/contents/{$contentId}"); | 
            ||
| 246 | }  | 
            ||
| 247 | |||
| 248 | // Getters / Setters  | 
            ||
| 249 | public function getApiUrl()  | 
            ||
| 250 |     { | 
            ||
| 251 | return $this->apiUrl;  | 
            ||
| 252 | }  | 
            ||
| 253 | |||
| 254 | public function setApiUrl(string $apiUrl)  | 
            ||
| 255 |     { | 
            ||
| 256 | $this->apiUrl = $apiUrl;  | 
            ||
| 257 | |||
| 258 | return $this;  | 
            ||
| 259 | }  | 
            ||
| 260 | |||
| 261 | public function getToken()  | 
            ||
| 264 | }  | 
            ||
| 265 | |||
| 266 | public function setToken(string $token)  | 
            ||
| 267 |     { | 
            ||
| 268 | $this->token = $token;  | 
            ||
| 269 | |||
| 270 | return $this;  | 
            ||
| 271 | }  | 
            ||
| 272 | |||
| 273 | /**  | 
            ||
| 274 | * @param string $method  | 
            ||
| 275 | * @param string $path  | 
            ||
| 276 | * @param array $params  | 
            ||
| 277 | * @param array $queryStringParams  | 
            ||
| 278 | * @return LushResponse  | 
            ||
| 279 | */  | 
            ||
| 280 | private function request(string $method, string $path, array $params = [], array $queryStringParams = [])  | 
            ||
| 281 |     { | 
            ||
| 282 | return $this->httpClient  | 
            ||
| 283 | ->url($this->getRequestUri($path, $queryStringParams), $params)  | 
            ||
| 284 | ->headers($this->getRequestHeaders())  | 
            ||
| 285 | ->$method();  | 
            ||
| 286 | }  | 
            ||
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * @return array  | 
            ||
| 290 | */  | 
            ||
| 291 | private function getRequestHeaders()  | 
            ||
| 292 |     { | 
            ||
| 293 |         if (!$this->token) { | 
            ||
| 294 | return [];  | 
            ||
| 295 | }  | 
            ||
| 296 | |||
| 297 | return [  | 
            ||
| 298 |             'Authorization' => "Bearer {$this->token}" | 
            ||
| 299 | ];  | 
            ||
| 300 | }  | 
            ||
| 301 | |||
| 302 | /**  | 
            ||
| 303 | * @param array $params  | 
            ||
| 304 | * @return string  | 
            ||
| 305 | */  | 
            ||
| 306 | private function getRequestQueryString(array $params = [])  | 
            ||
| 307 |     { | 
            ||
| 308 |         if (count($params) === 0) { | 
            ||
| 309 | return "";  | 
            ||
| 310 | }  | 
            ||
| 311 | |||
| 312 | $queryString = query_string($params);  | 
            ||
| 313 | |||
| 314 | return "?" . urldecode((string) $queryString);  | 
            ||
| 315 | }  | 
            ||
| 316 | |||
| 317 | private function getRequestUri(string $path, array $queryStringParams = [])  | 
            ||
| 322 | }  | 
            ||
| 323 | }  | 
            ||
| 324 |