Complex classes like REST 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 REST, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class REST implements ServiceInterface { |
||
| 13 | /** |
||
| 14 | * REST adapter |
||
| 15 | * @var Pest |
||
| 16 | */ |
||
| 17 | protected $adapter; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Application token |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $token = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Inititate this REST servcie with the application token and a instance |
||
| 27 | * of the REST adapter (Pest) |
||
| 28 | * |
||
| 29 | * @param string $token |
||
| 30 | * @param Pest $adapter |
||
| 31 | */ |
||
| 32 | 41 | public function __construct($token, Pest $adapter) { |
|
| 36 | |||
| 37 | 40 | private function getHeaders() { |
|
| 40 | |||
| 41 | 2 | public function getAthlete($id = null) { |
|
| 49 | |||
| 50 | 1 | public function getAthleteStats($id) { |
|
| 55 | |||
| 56 | 1 | public function getAthleteClubs() { |
|
| 61 | |||
| 62 | 1 | public function getAthleteActivities($before = null, $after = null, $page = null, $per_page = null) { |
|
| 73 | |||
| 74 | 2 | public function getAthleteFriends($id = null, $page = null, $per_page = null) { |
|
| 87 | |||
| 88 | 2 | public function getAthleteFollowers($id = null, $page = null, $per_page = null) { |
|
| 101 | |||
| 102 | 1 | public function getAthleteBothFollowing($id, $page = null, $per_page = null) { |
|
| 112 | |||
| 113 | 1 | public function getAthleteKom($id, $page = null, $per_page = null) { |
|
| 123 | |||
| 124 | 2 | public function getAthleteStarredSegments($id = null, $page = null, $per_page = null) { |
|
| 138 | |||
| 139 | 1 | public function updateAthlete($city, $state, $country, $sex, $weight) { |
|
| 151 | |||
| 152 | public function getActivityFollowing($before = null, $page = null, $per_page = null) { |
||
| 153 | $path = '/activities/following'; |
||
| 154 | $parameters = array( |
||
| 155 | 'before' => $before, |
||
| 156 | 'page' => $page, |
||
| 157 | 'per_page' => $per_page |
||
| 158 | ); |
||
| 159 | $result = $this->adapter->get($path, $parameters, $this->getHeaders()); |
||
| 160 | return $this->format($result); |
||
| 161 | } |
||
| 162 | |||
| 163 | 1 | public function getActivity($id, $include_all_efforts = null) { |
|
| 164 | 1 | $path = '/activities/'.$id; |
|
| 165 | $parameters = array( |
||
| 166 | 1 | 'include_all_efforts' => $include_all_efforts, |
|
| 167 | 1 | ); |
|
| 168 | 1 | $result = $this->adapter->get($path, $parameters, $this->getHeaders()); |
|
| 169 | 1 | return $this->format($result); |
|
| 170 | } |
||
| 171 | |||
| 172 | 1 | public function getActivityComments($id, $markdown = null, $page = null, $per_page = null) { |
|
| 173 | 1 | $path = '/activities/'.$id.'/comments'; |
|
| 174 | $parameters = array( |
||
| 175 | 1 | 'markdown' => $markdown, |
|
| 176 | 1 | 'page' => $page, |
|
| 177 | 'per_page' => $per_page |
||
| 178 | 1 | ); |
|
| 179 | 1 | $result = $this->adapter->get($path, $parameters, $this->getHeaders()); |
|
| 180 | 1 | return $this->format($result); |
|
| 181 | } |
||
| 182 | |||
| 183 | 1 | public function getActivityKudos($id, $page = null, $per_page = null) { |
|
| 192 | |||
| 193 | 1 | public function getActivityPhotos($id, $size = 2048, $photo_sources = 'true') { |
|
| 202 | |||
| 203 | 1 | public function getActivityZones($id) { |
|
| 208 | |||
| 209 | 1 | public function getActivityLaps($id) { |
|
| 210 | 1 | $path = '/activities/'.$id.'/laps'; |
|
| 211 | 1 | $result = $this->adapter->get($path, array(), $this->getHeaders()); |
|
| 212 | 1 | return $this->format($result); |
|
| 213 | } |
||
| 214 | |||
| 215 | 1 | public function getActivityUploadStatus($id) { |
|
| 216 | 1 | $path = '/uploads/'.$id; |
|
| 217 | 1 | $result = $this->adapter->get($path, array(), $this->getHeaders()); |
|
| 218 | 1 | return $this->format($result); |
|
| 219 | } |
||
| 220 | |||
| 221 | 1 | public function createActivity($name, $type, $start_date_local, $elapsed_time, $description = null, $distance = null, $private = null, $trainer = null) { |
|
| 222 | 1 | $path = '/activities'; |
|
| 223 | $parameters = array( |
||
| 224 | 1 | 'name' => $name, |
|
| 225 | 1 | 'type' => $type, |
|
| 226 | 1 | 'start_date_local' => $start_date_local, |
|
| 227 | 1 | 'elapsed_time' => $elapsed_time, |
|
| 228 | 1 | 'description' => $description, |
|
| 229 | 1 | 'distance' => $distance, |
|
| 230 | 1 | 'private' => $private, |
|
| 231 | 1 | 'trainer' => $trainer, |
|
| 232 | 1 | ); |
|
| 233 | 1 | $result = $this->adapter->post($path, $parameters, $this->getHeaders()); |
|
| 234 | 1 | return $this->format($result); |
|
| 235 | } |
||
| 236 | |||
| 237 | 1 | public function uploadActivity($file, $activity_type = null, $name = null, $description = null, $private = null, $trainer = null, $commute = null, $data_type = null, $external_id = null) { |
|
| 238 | 1 | $path = '/uploads'; |
|
| 239 | $parameters = array( |
||
| 240 | 1 | 'activity_type' => $activity_type, |
|
| 241 | 1 | 'name' => $name, |
|
| 242 | 1 | 'description' => $description, |
|
| 243 | 1 | 'private' => $private, |
|
| 244 | 1 | 'trainer' => $trainer, |
|
| 245 | 1 | 'commute' => $commute, |
|
| 246 | 1 | 'data_type' => $data_type, |
|
| 247 | 1 | 'external_id' => $external_id, |
|
| 248 | 1 | 'file' => curl_file_create($file), |
|
| 249 | 1 | 'file_hack' => '@'.ltrim($file, '@'), |
|
| 250 | 1 | ); |
|
| 251 | 1 | $result = $this->adapter->post($path, $parameters, $this->getHeaders()); |
|
| 252 | 1 | return $this->format($result); |
|
| 253 | } |
||
| 254 | |||
| 255 | 1 | public function updateActivity($id, $name = null, $type = null, $private = false, $commute = false, $trainer = false, $gear_id = null, $description = null) { |
|
| 256 | 1 | $path = '/activities/'.$id; |
|
| 257 | $parameters = array( |
||
| 258 | 1 | 'name' => $name, |
|
| 259 | 1 | 'type' => $type, |
|
| 260 | 1 | 'private' => $private, |
|
| 261 | 1 | 'commute' => $commute, |
|
| 262 | 1 | 'trainer' => $trainer, |
|
| 263 | 1 | 'gear_id' => $gear_id, |
|
| 264 | 1 | 'description' => $description, |
|
| 265 | 1 | ); |
|
| 266 | 1 | $result = $this->adapter->put($path, $parameters, $this->getHeaders()); |
|
| 267 | 1 | return $this->format($result); |
|
| 268 | } |
||
| 269 | |||
| 270 | 1 | public function deleteActivity($id) { |
|
| 275 | |||
| 276 | 1 | public function getGear($id) { |
|
| 277 | 1 | $path = '/gear/'.$id; |
|
| 278 | 1 | $result = $this->adapter->get($path, array(), $this->getHeaders()); |
|
| 279 | 1 | return $this->format($result); |
|
| 280 | } |
||
| 281 | |||
| 282 | 1 | public function getClub($id) { |
|
| 283 | 1 | $path = '/clubs/'.$id; |
|
| 284 | 1 | $result = $this->adapter->get($path, array(), $this->getHeaders()); |
|
| 285 | 1 | return $this->format($result); |
|
| 286 | } |
||
| 287 | |||
| 288 | 1 | public function getClubMembers($id, $page = null, $per_page = null) { |
|
| 289 | 1 | $path = '/clubs/'.$id.'/members'; |
|
| 290 | $parameters = array( |
||
| 291 | 1 | 'page' => $page, |
|
| 292 | 1 | 'per_page' => $per_page, |
|
| 293 | 1 | ); |
|
| 294 | 1 | $result = $this->adapter->get($path, $parameters, $this->getHeaders()); |
|
| 295 | 1 | return $this->format($result); |
|
| 296 | } |
||
| 297 | |||
| 298 | 1 | public function getClubActivities($id, $page = null, $per_page = null) { |
|
| 307 | |||
| 308 | 1 | public function getClubAnnouncements($id) { |
|
| 313 | |||
| 314 | 1 | public function getClubGroupEvents($id) { |
|
| 319 | |||
| 320 | 1 | public function joinClub($id) { |
|
| 325 | |||
| 326 | 1 | public function leaveClub($id) { |
|
| 327 | 1 | $path = '/clubs/'.$id.'/leave'; |
|
| 328 | 1 | $result = $this->adapter->post($path, array(), $this->getHeaders()); |
|
| 329 | 1 | return $this->format($result); |
|
| 330 | } |
||
| 331 | |||
| 332 | 1 | public function getSegment($id) { |
|
| 333 | 1 | $path = '/segments/'.$id; |
|
| 334 | 1 | $result = $this->adapter->get($path, array(), $this->getHeaders()); |
|
| 335 | 1 | return $this->format($result); |
|
| 336 | } |
||
| 337 | |||
| 338 | 1 | public function getSegmentLeaderboard($id, $gender = null, $age_group = null, $weight_class = null, $following = null, $club_id = null, $date_range = null, $context_entries = null, $page = null, $per_page = null) { |
|
| 339 | 1 | $path = '/segments/'.$id.'/leaderboard'; |
|
| 340 | $parameters = array( |
||
| 341 | 1 | 'id' => $gender, |
|
| 342 | 1 | 'age_group' => $age_group, |
|
| 343 | 1 | 'weight_class' => $weight_class, |
|
| 344 | 1 | 'following' => $following, |
|
| 345 | 1 | 'club_id' => $club_id, |
|
| 346 | 1 | 'date_range' => $date_range, |
|
| 347 | 1 | 'context_entries' => $context_entries, |
|
| 348 | 1 | 'page' => $page, |
|
| 349 | 'per_page' => $per_page |
||
| 350 | 1 | ); |
|
| 351 | |||
| 352 | 1 | $result = $this->adapter->get($path, $parameters, $this->getHeaders()); |
|
| 353 | 1 | return $this->format($result); |
|
| 354 | } |
||
| 355 | |||
| 356 | 1 | public function getSegmentExplorer($bounds, $activity_type = 'riding', $min_cat = null, $max_cat = null) { |
|
| 357 | 1 | $path = '/segments/explore'; |
|
| 358 | $parameters = array( |
||
| 359 | 1 | 'bounds' => $bounds, |
|
| 360 | 1 | 'activity_type' => $activity_type, |
|
| 361 | 1 | 'min_cat' => $min_cat, |
|
| 362 | 'max_cat' => $max_cat |
||
| 363 | 1 | ); |
|
| 364 | |||
| 365 | 1 | $result = $this->adapter->get($path, $parameters, $this->getHeaders()); |
|
| 366 | 1 | return $this->format($result); |
|
| 367 | } |
||
| 368 | |||
| 369 | 1 | public function getSegmentEffort($id, $athlete_id = null, $start_date_local = null, $end_date_local = null, $page = null, $per_page = null) { |
|
| 370 | 1 | $path = '/segments/'.$id.'/all_efforts'; |
|
| 371 | $parameters = array( |
||
| 372 | 1 | 'athlete_id' => $athlete_id, |
|
| 373 | 1 | 'start_date_local' => $start_date_local, |
|
| 374 | 1 | 'end_date_local' => $end_date_local, |
|
| 375 | 1 | 'page' => $page, |
|
| 376 | 'per_page' => $per_page |
||
| 377 | 1 | ); |
|
| 378 | |||
| 379 | 1 | $result = $this->adapter->get($path, $parameters, $this->getHeaders()); |
|
| 380 | 1 | return $this->format($result); |
|
| 381 | } |
||
| 382 | |||
| 383 | 1 | public function getStreamsActivity($id, $types, $resolution = null, $series_type = 'distance') { |
|
| 393 | |||
| 394 | 1 | public function getStreamsEffort($id, $types, $resolution = null, $series_type = 'distance') { |
|
| 404 | |||
| 405 | 1 | public function getStreamsSegment($id, $types, $resolution = null, $series_type = 'distance') { |
|
| 406 | 1 | $path = '/segments/'.$id.'/streams/'.$types; |
|
| 407 | $parameters = array( |
||
| 408 | 1 | 'resolution' => $resolution, |
|
| 409 | 'series_type' => $series_type |
||
| 410 | 1 | ); |
|
| 411 | |||
| 412 | 1 | $result = $this->adapter->get($path, $parameters, $this->getHeaders()); |
|
| 413 | 1 | return $this->format($result); |
|
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Convert the JSON output to an array |
||
| 418 | * @param string $result |
||
| 419 | */ |
||
| 420 | 40 | private function format($result) { |
|
| 421 | 40 | return json_decode($result, true); |
|
| 423 | } |
||
| 424 |