Completed
Push — master ( 4dba17...7d9984 )
by Bas van
03:38 queued 01:39
created

REST::uploadActivity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 15
ccs 13
cts 13
cp 1
rs 9.4286
cc 1
eloc 13
nc 1
nop 8
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 39
    private function getHeaders() {
38 39
        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) {
183 1
        $path = '/activities/'.$id.'/photos';
184 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
185 1
        return $this->format($result);
186
    }
187
    
188 1
    public function getActivityZones($id) {
189 1
        $path = '/activities/'.$id.'/zones';
190 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
191 1
        return $this->format($result);
192
    }
193
    
194 1
    public function getActivityLaps($id) {
195 1
        $path = '/activities/'.$id.'/laps';
196 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
197 1
        return $this->format($result);
198
    }
199
    
200 1
    public function getActivityUploadStatus($id) {
201 1
        $path = '/uploads/'.$id;
202 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
203 1
        return $this->format($result);
204
    }
205
    
206 1
    public function createActivity($name, $type, $start_date_local, $elapsed_time, $description = null, $distance = null) {
207 1
        $path = '/activities';
208
        $parameters = array(
209 1
            'name' => $name,
210 1
            'type' => $type,
211 1
            'start_date_local' => $start_date_local,
212 1
            'elapsed_time' => $elapsed_time,
213 1
            'description' => $description,
214 1
            'distance' => $distance,
215 1
        );
216 1
        $result = $this->adapter->post($path, $parameters, $this->getHeaders());
217 1
        return $this->format($result);
218
    }
219
    
220 1
    public function uploadActivity($file, $activity_type = null, $name = null, $description = null, $private = null, $trainer = null, $data_type = null, $external_id = null) {
221 1
        $path = '/uploads';
222
        $parameters = array(
223 1
            'activity_type' => $activity_type,
224 1
            'name' => $name,
225 1
            'description' => $description,
226 1
            'private' => $private,
227 1
            'trainer' => $trainer,
228 1
            'data_type' => $data_type,
229 1
            'external_id' => $external_id,
230 1
            'file' => $file,
231 1
        );
232 1
        $result = $this->adapter->post($path, $parameters, $this->getHeaders());
233 1
        return $this->format($result);
234
    }
235
    
236 1
    public function updateActivity($id, $name = null, $type = null, $private = false, $commute = false, $trainer = false, $gear_id = null, $description = null) {
237 1
        $path = '/activities/'.$id;
238
        $parameters = array(
239 1
            'name' => $name,
240 1
            'type' => $type,
241 1
            'private' => $private   ,
242 1
            'commute' => $commute,
243 1
            'trainer' => $trainer,
244 1
            'gear_id' => $gear_id,
245 1
            'description' => $description,
246 1
        );
247 1
        $result = $this->adapter->put($path, $parameters, $this->getHeaders());
248 1
        return $this->format($result);
249
    }
250
    
251 1
    public function deleteActivity($id) {
252 1
        $path = '/activities/'.$id;
253 1
        $result = $this->adapter->delete($path, array());
254 1
        return $this->format($result);
255
    }
256
    
257 1
    public function getGear($id) {
258 1
        $path = '/gear/'.$id;
259 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
260 1
        return $this->format($result);
261
    }
262
    
263 1
    public function getClub($id) {
264 1
        $path = '/clubs/'.$id;
265 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
266 1
        return $this->format($result);
267
    }
268
    
269 1
    public function getClubMembers($id, $page = null, $per_page  = null) {
270 1
        $path = '/clubs/'.$id.'/members';
271
        $parameters = array(
272 1
            'page' => $page,
273 1
            'per_page' => $per_page,
274 1
        );
275 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
276 1
        return $this->format($result);
277
    }
278
    
279 1
    public function getClubActivities($id, $page = null, $per_page  = null) {
280 1
        $path = '/clubs/'.$id.'/activities';
281
        $parameters = array(
282 1
            'page' => $page,
283 1
            'per_page' => $per_page,
284 1
        );
285 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
286 1
        return $this->format($result);
287
    }
288
289 1
    public function getClubAnnouncements($id) {
290 1
        $path = '/clubs/'.$id.'/announcements';
291 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
292 1
        return $this->format($result);
293
    }
294
295 1
    public function getClubGroupEvents($id) {
296 1
        $path = '/clubs/'.$id.'/group_events';
297 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
298 1
        return $this->format($result);
299
    }
300
301 1
    public function joinClub($id) {
302 1
        $path = '/clubs/'.$id.'/join';
303 1
        $result = $this->adapter->post($path, array(), $this->getHeaders());
304 1
        return $this->format($result);
305
    }
306
307 1
    public function leaveClub($id) {
308 1
        $path = '/clubs/'.$id.'/leave';
309 1
        $result = $this->adapter->post($path, array(), $this->getHeaders());
310 1
        return $this->format($result);
311
    }
312
313 1
    public function getSegment($id) {
314 1
        $path = '/segments/'.$id;
315 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
316 1
        return $this->format($result);
317
    }
318
    
319 1
    public function getSegmentLeaderboard($id, $gender = null, $age_group = null, $weight_class = null, $following = null, $club_id = null, $date_range = null, $page = null, $per_page = null) {
320 1
        $path = '/segments/'.$id.'/leaderboard';
321
        $parameters = array(
322 1
            'id' => $gender,
323 1
            'age_group' => $age_group,
324 1
            'weight_class' => $weight_class,
325 1
            'following' => $following,
326 1
            'club_id' => $club_id,
327 1
            'date_range' => $date_range,
328 1
            'page' => $page,
329
            'per_page' => $per_page
330 1
        );
331
        
332 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
333 1
        return $this->format($result);
334
    }
335
    
336 1
    public function getSegmentExplorer($bounds, $activity_type = 'riding', $min_cat = null, $max_cat = null) {
337 1
        $path = '/segments/explore';
338
        $parameters = array(
339 1
            'bounds' => $bounds,
340 1
            'activity_type' => $activity_type,
341 1
            'min_cat' => $min_cat,
342
            'max_cat' => $max_cat
343 1
        );
344
        
345 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
346 1
        return $this->format($result);
347
    }
348
    
349 1
    public function getSegmentEffort($id, $athlete_id = null, $start_date_local = null, $end_date_local = null, $page = null, $per_page = null) {
350 1
        $path = '/segments/'.$id.'/all_efforts';
351
        $parameters = array(
352 1
            'athlete_id' => $athlete_id,
353 1
            'start_date_local' => $start_date_local,
354 1
            'end_date_local' => $end_date_local,
355 1
            'page' => $page,
356
            'per_page' => $per_page
357 1
        );
358
        
359 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
360 1
        return $this->format($result);
361
    }
362
    
363 1
    public function getStreamsActivity($id, $types, $resolution = 'all', $series_type = 'distance') {
364 1
        $path = '/activities/'.$id.'/streams/'.$types;
365
        $parameters = array(
366 1
            'resolution' => $resolution,
367
            'series_type' => $series_type
368 1
        );
369
        
370 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
371 1
        return $this->format($result);
372
    }
373
    
374 1
    public function getStreamsEffort($id, $types, $resolution = 'all', $series_type = 'distance') {
375 1
        $path = '/segment_efforts/'.$id.'/streams/'.$types;
376
        $parameters = array(
377 1
            'resolution' => $resolution,
378
            'series_type' => $series_type
379 1
        );
380
        
381 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
382 1
        return $this->format($result);
383
    }
384
    
385 1
    public function getStreamsSegment($id, $types, $resolution = 'all', $series_type = 'distance') {
386 1
        $path = '/segments/'.$id.'/streams/'.$types;
387
        $parameters = array(
388 1
            'resolution' => $resolution,
389
            'series_type' => $series_type
390 1
        );
391
        
392 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
393 1
        return $this->format($result);
394
    }
395
    
396
    /**
397
     * Convert the JSON output to an array
398
     * @param string $result
399
     */
400 40
    private function format($result) {
401 40
        return json_decode($result,true);
402
    }
403
}
404