Completed
Push — master ( 6a30a3...8c0277 )
by Bas van
02:47
created

REST::uploadActivity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
ccs 15
cts 15
cp 1
rs 9.4285
cc 1
eloc 15
nc 1
nop 9
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
    /**
15
     * REST adapter
16
     * @var Pest
17
     */
18
    protected $adapter;
19
20
    /**
21
     * Application token
22
     * @var string
23
     */
24
    private $token = null;
25
26
    /**
27
     * Inititate this REST servcie with the application token and a instance
28
     * of the REST adapter (Pest)
29
     *
30
     * @param string $token
31
     * @param Pest $adapter
32
     */
33 42
    public function __construct($token, Pest $adapter)
34
    {
35 42
        $this->token = $token;
36 42
        $this->adapter = $adapter;
37 42
    }
38
39 41
    private function getHeaders()
40
    {
41 41
        return array('Authorization: Bearer ' . $this->token);
42
    }
43
44 2
    public function getAthlete($id = null)
45
    {
46 2
        $path = '/athlete';
47 2
        if (isset($id) && $id !== null) {
48 1
            $path = '/athletes/' . $id;
49 1
        }
50 2
        $result = $this->adapter->get($path, array(), $this->getHeaders());
51 2
        return $this->format($result);
52
    }
53
54 1
    public function getAthleteStats($id)
55
    {
56 1
        $path = '/athletes/' . $id . '/stats';
57 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
58 1
        return $this->format($result);
59
    }
60
61 1
    public function getAthleteClubs()
62
    {
63 1
        $path = '/athlete/clubs';
64 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
65 1
        return $this->format($result);
66
    }
67
68 1
    public function getAthleteActivities($before = null, $after = null, $page = null, $per_page = null)
69
    {
70 1
        $path = '/athlete/activities';
71
        $parameters = array(
72 1
            'before' => $before,
73 1
            'after' => $after,
74 1
            'page' => $page,
75 1
            'per_page' => $per_page,
76 1
        );
77 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
78 1
        return $this->format($result);
79
    }
80
81 2
    public function getAthleteFriends($id = null, $page = null, $per_page = null)
82
    {
83 2
        $path = '/athlete/friends';
84 2
        if (isset($id) && $id !== null) {
85 1
            $path = '/athletes/' . $id . '/friends';
86 1
        }
87
88
        $parameters = array(
89 2
            'page' => $page,
90 2
            'per_page' => $per_page,
91 2
        );
92 2
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
93 2
        return $this->format($result);
94
    }
95
96 2
    public function getAthleteFollowers($id = null, $page = null, $per_page = null)
97
    {
98 2
        $path = '/athlete/followers';
99 2
        if (isset($id) && $id !== null) {
100 1
            $path = '/athletes/' . $id . '/followers';
101 1
        }
102
103
        $parameters = array(
104 2
            'page' => $page,
105 2
            'per_page' => $per_page,
106 2
        );
107 2
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
108 2
        return $this->format($result);
109
    }
110
111 1
    public function getAthleteBothFollowing($id, $page = null, $per_page = null)
112
    {
113 1
        $path = '/athletes/' . $id . '/both-following';
114
115
        $parameters = array(
116 1
            'page' => $page,
117 1
            'per_page' => $per_page,
118 1
        );
119 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
120 1
        return $this->format($result);
121
    }
122
123 1
    public function getAthleteKom($id, $page = null, $per_page = null)
124
    {
125 1
        $path = '/athletes/' . $id . '/koms';
126
127
        $parameters = array(
128 1
            'page' => $page,
129 1
            'per_page' => $per_page,
130 1
        );
131 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
132 1
        return $this->format($result);
133
    }
134
135 1
    public function getAthleteZones()
136
    {
137 1
        $path = '/athlete/zones';
138 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
139 1
        return $this->format($result);
140
    }
141
142 2
    public function getAthleteStarredSegments($id = null, $page = null, $per_page = null)
143
    {
144 2
        $path = '/segments/starred';
145 2
        if (isset($id) && $id !== null) {
146 1
            $path = '/athletes/' . $id . '/segments/starred';
147
            // ...wrong in Strava documentation
148 1
        }
149
150
        $parameters = array(
151 2
            'page' => $page,
152 2
            'per_page' => $per_page,
153 2
        );
154 2
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
155 2
        return $this->format($result);
156
    }
157
158 1
    public function updateAthlete($city, $state, $country, $sex, $weight)
159
    {
160 1
        $path = '/athlete';
161
        $parameters = array(
162 1
            'city' => $city,
163 1
            'state' => $state,
164 1
            'country' => $country,
165 1
            'sex' => $sex,
166 1
            'weight' => $weight,
167 1
        );
168 1
        $result = $this->adapter->put($path, $parameters, $this->getHeaders());
169 1
        return $this->format($result);
170
    }
171
172
    public function getActivityFollowing($before = null, $page = null, $per_page = null)
173
    {
174
        $path = '/activities/following';
175
        $parameters = array(
176
            'before' => $before,
177
            'page' => $page,
178
            'per_page' => $per_page
179
        );
180
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
181
        return $this->format($result);
182
    }
183
   
184 1
    public function getActivity($id, $include_all_efforts = null)
185
    {
186 1
        $path = '/activities/' . $id;
187
        $parameters = array(
188 1
            'include_all_efforts' => $include_all_efforts,
189 1
        );
190 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
191 1
        return $this->format($result);
192
    }
193
194 1
    public function getActivityComments($id, $markdown = null, $page = null, $per_page = null)
195
    {
196 1
        $path = '/activities/' . $id . '/comments';
197
        $parameters = array(
198 1
            'markdown' => $markdown,
199 1
            'page' => $page,
200
            'per_page' => $per_page
201 1
        );
202 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
203 1
        return $this->format($result);
204
    }
205
206 1
    public function getActivityKudos($id, $page = null, $per_page = null)
207
    {
208 1
        $path = '/activities/' . $id . '/kudos';
209
        $parameters = array(
210 1
            'page' => $page,
211
            'per_page' => $per_page
212 1
        );
213 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
214 1
        return $this->format($result);
215
    }
216
217 1
    public function getActivityPhotos($id, $size = 2048, $photo_sources = 'true')
218
    {
219 1
        $path = '/activities/' . $id . '/photos';
220
        $parameters = array(
221 1
            'size' => $size,
222 1
            'photo_sources' => $photo_sources,
223 1
        );
224 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
225 1
        return $this->format($result);
226
    }
227
228 1
    public function getActivityZones($id)
229
    {
230 1
        $path = '/activities/' . $id . '/zones';
231 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
232 1
        return $this->format($result);
233
    }
234
235 1
    public function getActivityLaps($id)
236
    {
237 1
        $path = '/activities/' . $id . '/laps';
238 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
239 1
        return $this->format($result);
240
    }
241
242 1
    public function getActivityUploadStatus($id)
243
    {
244 1
        $path = '/uploads/' . $id;
245 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
246 1
        return $this->format($result);
247
    }
248
249 1
    public function createActivity($name, $type, $start_date_local, $elapsed_time, $description = null, $distance = null, $private = null, $trainer = null)
250
    {
251 1
        $path = '/activities';
252
        $parameters = array(
253 1
            'name' => $name,
254 1
            'type' => $type,
255 1
            'start_date_local' => $start_date_local,
256 1
            'elapsed_time' => $elapsed_time,
257 1
            'description' => $description,
258 1
            'distance' => $distance,
259 1
            'private' => $private,
260 1
            'trainer' => $trainer,
261 1
        );
262 1
        $result = $this->adapter->post($path, $parameters, $this->getHeaders());
263 1
        return $this->format($result);
264
    }
265
266 1
    public function uploadActivity($file, $activity_type = null, $name = null, $description = null, $private = null, $trainer = null, $commute = null, $data_type = null, $external_id = null)
267
    {
268 1
        $path = '/uploads';
269
        $parameters = array(
270 1
            'activity_type' => $activity_type,
271 1
            'name' => $name,
272 1
            'description' => $description,
273 1
            'private' => $private,
274 1
            'trainer' => $trainer,
275 1
            'commute' => $commute,
276 1
            'data_type' => $data_type,
277 1
            'external_id' => $external_id,
278 1
            'file' => curl_file_create($file),
279 1
            'file_hack' => '@' . ltrim($file, '@'),
280 1
        );
281 1
        $result = $this->adapter->post($path, $parameters, $this->getHeaders());
282 1
        return $this->format($result);
283
    }
284
285 1
    public function updateActivity($id, $name = null, $type = null, $private = false, $commute = false, $trainer = false, $gear_id = null, $description = null)
286
    {
287 1
        $path = '/activities/' . $id;
288
        $parameters = array(
289 1
            'name' => $name,
290 1
            'type' => $type,
291 1
            'private' => $private,
292 1
            'commute' => $commute,
293 1
            'trainer' => $trainer,
294 1
            'gear_id' => $gear_id,
295 1
            'description' => $description,
296 1
        );
297 1
        $result = $this->adapter->put($path, $parameters, $this->getHeaders());
298 1
        return $this->format($result);
299
    }
300
301 1
    public function deleteActivity($id)
302
    {
303 1
        $path = '/activities/' . $id;
304 1
        $result = $this->adapter->delete($path, $this->getHeaders());
305 1
        return $this->format($result);
306
    }
307
308 1
    public function getGear($id)
309
    {
310 1
        $path = '/gear/' . $id;
311 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
312 1
        return $this->format($result);
313
    }
314
315 1
    public function getClub($id)
316
    {
317 1
        $path = '/clubs/' . $id;
318 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
319 1
        return $this->format($result);
320
    }
321
322 1
    public function getClubMembers($id, $page = null, $per_page = null)
323
    {
324 1
        $path = '/clubs/' . $id . '/members';
325
        $parameters = array(
326 1
            'page' => $page,
327 1
            'per_page' => $per_page,
328 1
        );
329 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
330 1
        return $this->format($result);
331
    }
332
333 1
    public function getClubActivities($id, $page = null, $per_page = null)
334
    {
335 1
        $path = '/clubs/' . $id . '/activities';
336
        $parameters = array(
337 1
            'page' => $page,
338 1
            'per_page' => $per_page,
339 1
        );
340 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
341 1
        return $this->format($result);
342
    }
343
344 1
    public function getClubAnnouncements($id)
345
    {
346 1
        $path = '/clubs/' . $id . '/announcements';
347 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
348 1
        return $this->format($result);
349
    }
350
351 1
    public function getClubGroupEvents($id)
352
    {
353 1
        $path = '/clubs/' . $id . '/group_events';
354 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
355 1
        return $this->format($result);
356
    }
357
358 1
    public function joinClub($id)
359
    {
360 1
        $path = '/clubs/' . $id . '/join';
361 1
        $result = $this->adapter->post($path, array(), $this->getHeaders());
362 1
        return $this->format($result);
363
    }
364
365 1
    public function leaveClub($id)
366
    {
367 1
        $path = '/clubs/' . $id . '/leave';
368 1
        $result = $this->adapter->post($path, array(), $this->getHeaders());
369 1
        return $this->format($result);
370
    }
371
372 1
    public function getSegment($id)
373
    {
374 1
        $path = '/segments/' . $id;
375 1
        $result = $this->adapter->get($path, array(), $this->getHeaders());
376 1
        return $this->format($result);
377
    }
378
379 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)
380
    {
381 1
        $path = '/segments/' . $id . '/leaderboard';
382
        $parameters = array(
383 1
            'id' => $gender,
384 1
            'age_group' => $age_group,
385 1
            'weight_class' => $weight_class,
386 1
            'following' => $following,
387 1
            'club_id' => $club_id,
388 1
            'date_range' => $date_range,
389 1
            'context_entries' => $context_entries,
390 1
            'page' => $page,
391
            'per_page' => $per_page
392 1
        );
393
394 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
395 1
        return $this->format($result);
396
    }
397
398 1
    public function getSegmentExplorer($bounds, $activity_type = 'riding', $min_cat = null, $max_cat = null)
399
    {
400 1
        $path = '/segments/explore';
401
        $parameters = array(
402 1
            'bounds' => $bounds,
403 1
            'activity_type' => $activity_type,
404 1
            'min_cat' => $min_cat,
405
            'max_cat' => $max_cat
406 1
        );
407
408 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
409 1
        return $this->format($result);
410
    }
411
412 1
    public function getSegmentEffort($id, $athlete_id = null, $start_date_local = null, $end_date_local = null, $page = null, $per_page = null)
413
    {
414 1
        $path = '/segments/' . $id . '/all_efforts';
415
        $parameters = array(
416 1
            'athlete_id' => $athlete_id,
417 1
            'start_date_local' => $start_date_local,
418 1
            'end_date_local' => $end_date_local,
419 1
            'page' => $page,
420
            'per_page' => $per_page
421 1
        );
422
423 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
424 1
        return $this->format($result);
425
    }
426
427 1
    public function getStreamsActivity($id, $types, $resolution = null, $series_type = 'distance')
428
    {
429 1
        $path = '/activities/' . $id . '/streams/' . $types;
430
        $parameters = array(
431 1
            'resolution' => $resolution,
432
            'series_type' => $series_type
433 1
        );
434
435 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
436 1
        return $this->format($result);
437
    }
438
439 1
    public function getStreamsEffort($id, $types, $resolution = null, $series_type = 'distance')
440
    {
441 1
        $path = '/segment_efforts/' . $id . '/streams/' . $types;
442
        $parameters = array(
443 1
            'resolution' => $resolution,
444
            'series_type' => $series_type
445 1
        );
446
447 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
448 1
        return $this->format($result);
449
    }
450
451 1
    public function getStreamsSegment($id, $types, $resolution = null, $series_type = 'distance')
452
    {
453 1
        $path = '/segments/' . $id . '/streams/' . $types;
454
        $parameters = array(
455 1
            'resolution' => $resolution,
456
            'series_type' => $series_type
457 1
        );
458
459 1
        $result = $this->adapter->get($path, $parameters, $this->getHeaders());
460 1
        return $this->format($result);
461
    }
462
463
    /**
464
     * Convert the JSON output to an array
465
     * @param string $result
466
     */
467 41
    private function format($result)
468
    {
469 41
        return json_decode($result, true);
470
    }
471
}
472