Completed
Push — master ( 5c7825...ce6374 )
by Bas van
04:42 queued 02:26
created

REST::getSegmentLeaderboard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 13
cts 13
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 getActivity($id, $include_all_efforts = null) {
153 1
        $path = '/activities/'.$id;
154
        $parameters = array(
155 1
            'include_all_efforts' => $include_all_efforts,
156 1
        );
157 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
158 1
        return $this->format($result);
159
    }
160
    
161 1
    public function getActivityComments($id, $markdown = null, $page = null, $per_page = null) {
162 1
        $path = '/activities/'.$id.'/comments';
163
        $parameters = array(
164 1
            'markdown' => $markdown,
165 1
            'page' => $page,
166
            'per_page' => $per_page
167 1
        );
168 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
169 1
        return $this->format($result);
170
    }
171
    
172 1
    public function getActivityKudos($id, $page = null, $per_page = null) {
173 1
        $path = '/activities/'.$id.'/kudos';
174
        $parameters = array(
175 1
            'page' => $page,
176
            'per_page' => $per_page
177 1
        );
178 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
179 1
        return $this->format($result);
180
    }
181
    
182 1
    public function getActivityPhotos($id, $size = 2048, $photo_sources = 'true') {
183 1
        $path = '/activities/'.$id.'/photos';
184
        $parameters = array(
185 1
            'size' => $size,
186 1
            'photo_sources' => $photo_sources,
187 1
        );
188 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
189 1
        return $this->format($result);
190
    }
191
    
192 1
    public function getActivityZones($id) {
193 1
        $path = '/activities/'.$id.'/zones';
194 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
195 1
        return $this->format($result);
196
    }
197
    
198 1
    public function getActivityLaps($id) {
199 1
        $path = '/activities/'.$id.'/laps';
200 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
201 1
        return $this->format($result);
202
    }
203
    
204 1
    public function getActivityUploadStatus($id) {
205 1
        $path = '/uploads/'.$id;
206 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
207 1
        return $this->format($result);
208
    }
209
    
210 1
    public function createActivity($name, $type, $start_date_local, $elapsed_time, $description = null, $distance = null) {
211 1
        $path = '/activities';
212
        $parameters = array(
213 1
            'name' => $name,
214 1
            'type' => $type,
215 1
            'start_date_local' => $start_date_local,
216 1
            'elapsed_time' => $elapsed_time,
217 1
            'description' => $description,
218 1
            'distance' => $distance,
219 1
        );
220 1
        $result = $this->adapter->post($path, $parameters, $this->getHeaders());
221 1
        return $this->format($result);
222
    }
223
    
224 1
    public function uploadActivity($file, $activity_type = null, $name = null, $description = null, $private = null, $trainer = null, $commute = null, $data_type = null, $external_id = null) {
225 1
        $path = '/uploads';
226
        $parameters = array(
227 1
            'activity_type' => $activity_type,
228 1
            'name' => $name,
229 1
            'description' => $description,
230 1
            'private' => $private,
231 1
            'trainer' => $trainer,
232 1
            'commute' => $commute,
233 1
            'data_type' => $data_type,
234 1
            'external_id' => $external_id,
235 1
            'file' => '@'.ltrim($file, '@'),
236 1
        );
237 1
        $result = $this->adapter->post($path, $parameters, $this->getHeaders());
238 1
        return $this->format($result);
239
    }
240
    
241 1
    public function updateActivity($id, $name = null, $type = null, $private = false, $commute = false, $trainer = false, $gear_id = null, $description = null) {
242 1
        $path = '/activities/'.$id;
243
        $parameters = array(
244 1
            'name' => $name,
245 1
            'type' => $type,
246 1
            'private' => $private   ,
247 1
            'commute' => $commute,
248 1
            'trainer' => $trainer,
249 1
            'gear_id' => $gear_id,
250 1
            'description' => $description,
251 1
        );
252 1
        $result = $this->adapter->put($path, $parameters, $this->getHeaders());
253 1
        return $this->format($result);
254
    }
255
    
256 1
    public function deleteActivity($id) {
257 1
        $path = '/activities/'.$id;
258 1
        $result = $this->adapter->delete($path, $this->getHeaders());
259 1
        return $this->format($result);
260
    }
261
    
262 1
    public function getGear($id) {
263 1
        $path = '/gear/'.$id;
264 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
265 1
        return $this->format($result);
266
    }
267
    
268 1
    public function getClub($id) {
269 1
        $path = '/clubs/'.$id;
270 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
271 1
        return $this->format($result);
272
    }
273
    
274 1
    public function getClubMembers($id, $page = null, $per_page  = null) {
275 1
        $path = '/clubs/'.$id.'/members';
276
        $parameters = array(
277 1
            'page' => $page,
278 1
            'per_page' => $per_page,
279 1
        );
280 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
281 1
        return $this->format($result);
282
    }
283
    
284 1
    public function getClubActivities($id, $page = null, $per_page  = null) {
285 1
        $path = '/clubs/'.$id.'/activities';
286
        $parameters = array(
287 1
            'page' => $page,
288 1
            'per_page' => $per_page,
289 1
        );
290 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
291 1
        return $this->format($result);
292
    }
293
294 1
    public function getClubAnnouncements($id) {
295 1
        $path = '/clubs/'.$id.'/announcements';
296 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
297 1
        return $this->format($result);
298
    }
299
300 1
    public function getClubGroupEvents($id) {
301 1
        $path = '/clubs/'.$id.'/group_events';
302 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
303 1
        return $this->format($result);
304
    }
305
306 1
    public function joinClub($id) {
307 1
        $path = '/clubs/'.$id.'/join';
308 1
        $result = $this->adapter->post($path, array(), $this->getHeaders());
309 1
        return $this->format($result);
310
    }
311
312 1
    public function leaveClub($id) {
313 1
        $path = '/clubs/'.$id.'/leave';
314 1
        $result = $this->adapter->post($path, array(), $this->getHeaders());
315 1
        return $this->format($result);
316
    }
317
318 1
    public function getSegment($id) {
319 1
        $path = '/segments/'.$id;
320 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
321 1
        return $this->format($result);
322
    }
323
    
324 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) {
325 1
        $path = '/segments/'.$id.'/leaderboard';
326
        $parameters = array(
327 1
            'id' => $gender,
328 1
            'age_group' => $age_group,
329 1
            'weight_class' => $weight_class,
330 1
            'following' => $following,
331 1
            'club_id' => $club_id,
332 1
            'date_range' => $date_range,
333 1
            'context_entries' => $context_entries,
334 1
            'page' => $page,
335
            'per_page' => $per_page
336 1
        );
337
        
338 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
339 1
        return $this->format($result);
340
    }
341
    
342 1
    public function getSegmentExplorer($bounds, $activity_type = 'riding', $min_cat = null, $max_cat = null) {
343 1
        $path = '/segments/explore';
344
        $parameters = array(
345 1
            'bounds' => $bounds,
346 1
            'activity_type' => $activity_type,
347 1
            'min_cat' => $min_cat,
348
            'max_cat' => $max_cat
349 1
        );
350
        
351 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
352 1
        return $this->format($result);
353
    }
354
    
355 1
    public function getSegmentEffort($id, $athlete_id = null, $start_date_local = null, $end_date_local = null, $page = null, $per_page = null) {
356 1
        $path = '/segments/'.$id.'/all_efforts';
357
        $parameters = array(
358 1
            'athlete_id' => $athlete_id,
359 1
            'start_date_local' => $start_date_local,
360 1
            'end_date_local' => $end_date_local,
361 1
            'page' => $page,
362
            'per_page' => $per_page
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 getStreamsActivity($id, $types, $resolution = null, $series_type = 'distance') {
370 1
        $path = '/activities/'.$id.'/streams/'.$types;
371
        $parameters = array(
372 1
            'resolution' => $resolution,
373
            'series_type' => $series_type
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 getStreamsEffort($id, $types, $resolution = null, $series_type = 'distance') {
381 1
        $path = '/segment_efforts/'.$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 getStreamsSegment($id, $types, $resolution = null, $series_type = 'distance') {
392 1
        $path = '/segments/'.$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
    /**
403
     * Convert the JSON output to an array
404
     * @param string $result
405
     */
406 40
    private function format($result) {
407 40
        return json_decode($result,true);
408
    }
409
}
410