Completed
Push — master ( f0f8ea...68f02b )
by Bas van
02:50
created

REST   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 412
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.36%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 48
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 412
ccs 258
cts 265
cp 0.9736
rs 8.4864

40 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHeaders() 0 3 1
A getAthlete() 0 8 3
A getAthleteStats() 0 5 1
A getAthleteClubs() 0 5 1
A getAthleteActivities() 0 11 1
A getAthleteFriends() 0 13 3
A getAthleteFollowers() 0 13 3
A getAthleteBothFollowing() 0 10 1
A getAthleteKom() 0 10 1
A getAthleteStarredSegments() 0 14 3
A updateAthlete() 0 12 1
A getActivityKudos() 0 9 1
A getActivityPhotos() 0 9 1
A getActivityZones() 0 5 1
A deleteActivity() 0 5 1
A getClubActivities() 0 9 1
A getClubAnnouncements() 0 5 1
A getClubGroupEvents() 0 5 1
A joinClub() 0 5 1
A getStreamsActivity() 0 10 1
A getStreamsEffort() 0 10 1
A getActivityFollowing() 0 10 1
A getActivity() 0 8 1
A getActivityComments() 0 10 1
A getActivityLaps() 0 5 1
A getActivityUploadStatus() 0 5 1
A createActivity() 0 15 1
A uploadActivity() 0 17 1
A updateActivity() 0 14 1
A getGear() 0 5 1
A getClub() 0 5 1
A getClubMembers() 0 9 1
A leaveClub() 0 5 1
A getSegment() 0 5 1
A getSegmentLeaderboard() 0 17 1
A getSegmentExplorer() 0 12 1
A getSegmentEffort() 0 13 1
A getStreamsSegment() 0 10 1
A format() 0 3 1

How to fix   Complexity   

Complex Class

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
2
namespace Strava\API\Service;
3
4
use Pest;
5
6
/**
7
 * Strava REST Service
8
 *
9
 * @author Bas van Dorst
10
 * @package StravaPHP
11
 */
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) {
33 41
        $this->token = $token;
34 41
        $this->adapter = $adapter;
35 41
    }
36
37 40
    private function getHeaders() {
38 40
        return array('Authorization: Bearer '.$this->token);
39
    }
40
41 2
    public function getAthlete($id = null) {
42 2
        $path = '/athlete';
43 2
        if (isset($id) && $id !== null) {
44 1
            $path = '/athletes/'.$id;
45 1
        }
46 2
        $result = $this->adapter->get($path, array(), $this->getHeaders());
47 2
        return $this->format($result);
48
    }
49
50 1
    public function getAthleteStats($id) {
51 1
        $path = '/athletes/'.$id.'/stats';
52 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
53 1
        return $this->format($result);
54
    }
55
56 1
    public function getAthleteClubs() {
57 1
        $path = '/athlete/clubs';
58 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
59 1
        return $this->format($result);
60
    }
61
62 1
    public function getAthleteActivities($before = null, $after = null, $page = null, $per_page = null) {
63 1
        $path = '/athlete/activities';
64
        $parameters = array(
65 1
            'before' => $before,
66 1
            'after' => $after,
67 1
            'page' => $page,
68 1
            'per_page' => $per_page,
69 1
        );
70 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
71 1
        return $this->format($result);
72
    }
73
74 2
    public function getAthleteFriends($id = null, $page = null, $per_page = null) {
75 2
        $path = '/athlete/friends';
76 2
        if (isset($id) && $id !== null) {
77 1
            $path = '/athletes/'.$id.'/friends';
78 1
        }
79
80
        $parameters = array(
81 2
            'page' => $page,
82 2
            'per_page' => $per_page,
83 2
        );
84 2
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
85 2
        return $this->format($result);
86
    }
87
88 2
    public function getAthleteFollowers($id = null, $page = null, $per_page = null) {
89 2
        $path = '/athlete/followers';
90 2
        if (isset($id) && $id !== null) {
91 1
            $path = '/athletes/'.$id.'/followers';
92 1
        }
93
94
        $parameters = array(
95 2
            'page' => $page,
96 2
            'per_page' => $per_page,
97 2
        );
98 2
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
99 2
        return $this->format($result);
100
    }
101
102 1
    public function getAthleteBothFollowing($id, $page = null, $per_page = null) {
103 1
        $path = '/athletes/'.$id.'/both-following';
104
105
        $parameters = array(
106 1
            'page' => $page,
107 1
            'per_page' => $per_page,
108 1
        );
109 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
110 1
        return $this->format($result);
111
    }
112
113 1
    public function getAthleteKom($id, $page = null, $per_page = null) {
114 1
        $path = '/athletes/'.$id.'/koms';
115
116
        $parameters = array(
117 1
            'page' => $page,
118 1
            'per_page' => $per_page,
119 1
        );
120 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
121 1
        return $this->format($result);
122
    }
123
124 2
    public function getAthleteStarredSegments($id = null, $page = null, $per_page = null) {
125 2
        $path = '/segments/starred';
126 2
        if (isset($id) && $id !== null) {
127 1
            $path = '/athletes/'.$id.'/segments/starred';
128
            // ...wrong in Strava documentation
129 1
        }
130
131
        $parameters = array(
132 2
            'page' => $page,
133 2
            'per_page' => $per_page,
134 2
        );
135 2
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
136 2
        return $this->format($result);
137
    }
138
139 1
    public function updateAthlete($city, $state, $country, $sex, $weight) {
140 1
        $path = '/athlete';
141
        $parameters = array(
142 1
            'city' => $city,
143 1
            'state' => $state,
144 1
            'country' => $country,
145 1
            'sex' => $sex,
146 1
            'weight' => $weight,
147 1
        );
148 1
        $result = $this->adapter->put($path, $parameters, $this->getHeaders());
149 1
        return $this->format($result);
150
    }
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) {
184 1
        $path = '/activities/'.$id.'/kudos';
185
        $parameters = array(
186 1
            'page' => $page,
187
            'per_page' => $per_page
188 1
        );
189 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
190 1
        return $this->format($result);
191
    }
192
193 1
    public function getActivityPhotos($id, $size = 2048, $photo_sources = 'true') {
194 1
        $path = '/activities/'.$id.'/photos';
195
        $parameters = array(
196 1
            'size' => $size,
197 1
            'photo_sources' => $photo_sources,
198 1
        );
199 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
200 1
        return $this->format($result);
201
    }
202
203 1
    public function getActivityZones($id) {
204 1
        $path = '/activities/'.$id.'/zones';
205 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
206 1
        return $this->format($result);
207
    }
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) {
271 1
        $path = '/activities/'.$id;
272 1
        $result = $this->adapter->delete($path, $this->getHeaders());
273 1
        return $this->format($result);
274
    }
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) {
299 1
        $path = '/clubs/'.$id.'/activities';
300
        $parameters = array(
301 1
            'page' => $page,
302 1
            'per_page' => $per_page,
303 1
        );
304 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
305 1
        return $this->format($result);
306
    }
307
308 1
    public function getClubAnnouncements($id) {
309 1
        $path = '/clubs/'.$id.'/announcements';
310 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
311 1
        return $this->format($result);
312
    }
313
314 1
    public function getClubGroupEvents($id) {
315 1
        $path = '/clubs/'.$id.'/group_events';
316 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
317 1
        return $this->format($result);
318
    }
319
320 1
    public function joinClub($id) {
321 1
        $path = '/clubs/'.$id.'/join';
322 1
        $result = $this->adapter->post($path, array(), $this->getHeaders());
323 1
        return $this->format($result);
324
    }
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') {
384 1
        $path = '/activities/'.$id.'/streams/'.$types;
385
        $parameters = array(
386 1
            'resolution' => $resolution,
387
            'series_type' => $series_type
388 1
        );
389
390 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
391 1
        return $this->format($result);
392
    }
393
394 1
    public function getStreamsEffort($id, $types, $resolution = null, $series_type = 'distance') {
395 1
        $path = '/segment_efforts/'.$id.'/streams/'.$types;
396
        $parameters = array(
397 1
            'resolution' => $resolution,
398
            'series_type' => $series_type
399 1
        );
400
401 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
402 1
        return $this->format($result);
403
    }
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);
422
    }
423
}
424