Completed
Pull Request — master (#31)
by
unknown
08:09
created

REST::getSegmentLeaderboard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 14
nc 1
nop 10
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 1
    public function getActivityFollowing($before = null, $page = null, $per_page = null) {
153 1
        $path = '/activities/following';
154
        $parameters = array(
155 1
            'before' => $before,
156 1
            'page' => $page,
157 1
            'per_page' => $per_page
158 1
        );
159
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
160
        return $this->format($result);        
161 1
    }
162 1
    
163
    public function getActivity($id, $include_all_efforts = null) {
164 1
        $path = '/activities/'.$id;
165 1
        $parameters = array(
166
            '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
            'page' => $page,
177 1
            'per_page' => $per_page
178 1
        );
179 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
180
        return $this->format($result);
181
    }
182 1
    
183 1
    public function getActivityKudos($id, $page = null, $per_page = null) {
184
        $path = '/activities/'.$id.'/kudos';
185 1
        $parameters = array(
186 1
            'page' => $page,
187 1
            'per_page' => $per_page
188 1
        );
189 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
190
        return $this->format($result);
191
    }
192 1
    
193 1
    public function getActivityPhotos($id, $size = 2048, $photo_sources = 'true') {
194 1
        $path = '/activities/'.$id.'/photos';
195 1
        $parameters = array(
196
            'size' => $size,
197
            'photo_sources' => $photo_sources,
198 1
        );
199 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
200 1
        return $this->format($result);
201 1
    }
202
    
203
    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 1
    }
208
    
209
    public function getActivityLaps($id) {
210 1
        $path = '/activities/'.$id.'/laps';
211 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
212
        return $this->format($result);
213 1
    }
214 1
    
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 1
    }
220 1
    
221 1
    public function createActivity($name, $type, $start_date_local, $elapsed_time, $description = null, $distance = null) {
222
        $path = '/activities';
223
        $parameters = array(
224 1
            'name' => $name,
225 1
            'type' => $type,
226
            'start_date_local' => $start_date_local,
227 1
            'elapsed_time' => $elapsed_time,
228 1
            'description' => $description,
229 1
            'distance' => $distance,
230 1
        );
231 1
        $result = $this->adapter->post($path, $parameters, $this->getHeaders());
232 1
        return $this->format($result);
233 1
    }
234 1
    
235 1
    public function uploadActivity($file, $activity_type = null, $name = null, $description = null, $private = null, $trainer = null, $commute = null, $data_type = null, $external_id = null) {
236 1
        $path = '/uploads';
237 1
        $parameters = array(
238 1
            'activity_type' => $activity_type,
239
            'name' => $name,
240
            'description' => $description,
241 1
            'private' => $private,
242 1
            'trainer' => $trainer,
243
            'commute' => $commute,
244 1
            'data_type' => $data_type,
245 1
            'external_id' => $external_id,
246 1
            'file' => '@'.ltrim($file, '@'),
247 1
        );
248 1
        $result = $this->adapter->post($path, $parameters, $this->getHeaders());
249 1
        return $this->format($result);
250 1
    }
251 1
    
252 1
    public function updateActivity($id, $name = null, $type = null, $private = false, $commute = false, $trainer = false, $gear_id = null, $description = null) {
253 1
        $path = '/activities/'.$id;
254
        $parameters = array(
255
            'name' => $name,
256 1
            'type' => $type,
257 1
            'private' => $private   ,
258 1
            'commute' => $commute,
259 1
            'trainer' => $trainer,
260
            'gear_id' => $gear_id,
261
            'description' => $description,
262 1
        );
263 1
        $result = $this->adapter->put($path, $parameters, $this->getHeaders());
264 1
        return $this->format($result);
265 1
    }
266
    
267
    public function deleteActivity($id) {
268 1
        $path = '/activities/'.$id;
269 1
        $result = $this->adapter->delete($path, $this->getHeaders());
270 1
        return $this->format($result);
271 1
    }
272
    
273
    public function getGear($id) {
274 1
        $path = '/gear/'.$id;
275 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
276
        return $this->format($result);
277 1
    }
278 1
    
279 1
    public function getClub($id) {
280 1
        $path = '/clubs/'.$id;
281 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
282
        return $this->format($result);
283
    }
284 1
    
285 1
    public function getClubMembers($id, $page = null, $per_page  = null) {
286
        $path = '/clubs/'.$id.'/members';
287 1
        $parameters = array(
288 1
            'page' => $page,
289 1
            'per_page' => $per_page,
290 1
        );
291 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
292
        return $this->format($result);
293
    }
294 1
    
295 1
    public function getClubActivities($id, $page = null, $per_page  = null) {
296 1
        $path = '/clubs/'.$id.'/activities';
297 1
        $parameters = array(
298
            'page' => $page,
299
            'per_page' => $per_page,
300 1
        );
301 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
302 1
        return $this->format($result);
303 1
    }
304
305
    public function getClubAnnouncements($id) {
306 1
        $path = '/clubs/'.$id.'/announcements';
307 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
308 1
        return $this->format($result);
309 1
    }
310
311
    public function getClubGroupEvents($id) {
312 1
        $path = '/clubs/'.$id.'/group_events';
313 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
314 1
        return $this->format($result);
315 1
    }
316
317
    public function joinClub($id) {
318 1
        $path = '/clubs/'.$id.'/join';
319 1
        $result = $this->adapter->post($path, array(), $this->getHeaders());
320 1
        return $this->format($result);
321 1
    }
322
323
    public function leaveClub($id) {
324 1
        $path = '/clubs/'.$id.'/leave';
325 1
        $result = $this->adapter->post($path, array(), $this->getHeaders());
326
        return $this->format($result);
327 1
    }
328 1
329 1
    public function getSegment($id) {
330 1
        $path = '/segments/'.$id;
331 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
332 1
        return $this->format($result);
333 1
    }
334 1
    
335
    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) {
336 1
        $path = '/segments/'.$id.'/leaderboard';
337
        $parameters = array(
338 1
            'id' => $gender,
339 1
            'age_group' => $age_group,
340
            'weight_class' => $weight_class,
341
            'following' => $following,
342 1
            'club_id' => $club_id,
343 1
            'date_range' => $date_range,
344
            'context_entries' => $context_entries,
345 1
            'page' => $page,
346 1
            'per_page' => $per_page
347 1
        );
348
        
349 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
350
        return $this->format($result);
351 1
    }
352 1
    
353
    public function getSegmentExplorer($bounds, $activity_type = 'riding', $min_cat = null, $max_cat = null) {
354
        $path = '/segments/explore';
355 1
        $parameters = array(
356 1
            'bounds' => $bounds,
357
            'activity_type' => $activity_type,
358 1
            'min_cat' => $min_cat,
359 1
            'max_cat' => $max_cat
360 1
        );
361 1
        
362
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
363 1
        return $this->format($result);
364
    }
365 1
    
366 1
    public function getSegmentEffort($id, $athlete_id = null, $start_date_local = null, $end_date_local = null, $page = null, $per_page = null) {
367
        $path = '/segments/'.$id.'/all_efforts';
368
        $parameters = array(
369 1
            'athlete_id' => $athlete_id,
370 1
            'start_date_local' => $start_date_local,
371
            'end_date_local' => $end_date_local,
372 1
            'page' => $page,
373
            'per_page' => $per_page
374 1
        );
375
        
376 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
377 1
        return $this->format($result);
378
    }
379
    
380 1
    public function getStreamsActivity($id, $types, $resolution = null, $series_type = 'distance') {
381 1
        $path = '/activities/'.$id.'/streams/'.$types;
382
        $parameters = array(
383 1
            'resolution' => $resolution,
384
            'series_type' => $series_type
385 1
        );
386
        
387 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
388 1
        return $this->format($result);
389
    }
390
    
391 1
    public function getStreamsEffort($id, $types, $resolution = null, $series_type = 'distance') {
392 1
        $path = '/segment_efforts/'.$id.'/streams/'.$types;
393
        $parameters = array(
394 1
            'resolution' => $resolution,
395
            'series_type' => $series_type
396 1
        );
397
        
398 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
399 1
        return $this->format($result);
400
    }
401
    
402
    public function getStreamsSegment($id, $types, $resolution = null, $series_type = 'distance') {
403
        $path = '/segments/'.$id.'/streams/'.$types;
404
        $parameters = array(
405
            'resolution' => $resolution,
406 40
            'series_type' => $series_type
407 40
        );
408
        
409
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
410
        return $this->format($result);
411
    }
412
    
413
    /**
414
     * Convert the JSON output to an array
415
     * @param string $result
416
     */
417
    private function format($result) {
418
        return json_decode($result,true);
419
    }
420
}
421