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

Client::updateActivity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 8
crap 2

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;
3
4
use Strava\API\Service\ServiceInterface;
5
use Strava\API\Exception as ClientException;
6
use Strava\API\Service\Exception as ServiceException;
7
8
/**
9
 * Strava API Client
10
 * 
11
 * @author Bas van Dorst
12
 * @package StravaPHP
13
 */
14
class Client {
15
    
16
    /**
17
     * @var ServiceInterface $service
18
     */
19
    protected $service;
20
    
21
    /**
22
     * Initiate this class with a subclass of ServiceInterface. There are two 
23
     * service subclasses available:
24
     * - Service\REST: Service which makes calls to the live Strava API 
25
     * - Service\Stub: Service stub for test purposes (unit tests)
26
     * 
27
     * @param ServiceInterface $service
28
     */
29 72
    public function __construct(ServiceInterface $service) {
30 72
        $this->service = $service;
31 72
    }
32
33
    /**
34
     * Retrieve current athlete
35
     * 
36
     * @link    http://strava.github.io/api/v3/athlete/#get-details,
37
     *          http://strava.github.io/api/v3/athlete/#get-another-details
38
     * @param   int $id
39
     * @return  array
40
     * @throws  Exception
41
     */
42 2
    public function getAthlete($id = null) {
43
        try {
44 2
            return $this->service->getAthlete($id);
45 1
        } catch (ServiceException $e) {
46 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
47
        }
48
    }
49
50
    /**
51
     * Retrieve athlete stats
52
     *
53
     * Only available for the authenticated athlete.
54
     *
55
     * @link    http://strava.github.io/api/v3/athlete/#stats
56
     * @param   int $id
57
     * @return  array
58
     * @throws  ClientException
59
     */
60 1
    public function getAthleteStats($id) {
61
        try {
62 1
            return $this->service->getAthleteStats($id);
63
        } catch (ServiceException $e) {
64
            throw new ClientException('[SERVICE] '.$e->getMessage());
65
        }
66
    }
67
    
68
    /**
69
     * List athlete clubs
70
     * 
71
     * @link    http://strava.github.io/api/v3/clubs/#get-athletes
72
     * @return  array
73
     * @throws  Exception
74
     */
75 2
    public function getAthleteClubs() {
76
        try {
77 2
            return $this->service->getAthleteClubs();
78 1
        } catch (ServiceException $e) {
79 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
80
        }
81
    }
82
    
83
    /**
84
     * List athlete activities
85
     * 
86
     * @link    http://strava.github.io/api/v3/activities/#get-activities
87
     * @param   string $before
88
     * @param   string $after
89
     * @param   int $page
90
     * @param   int $per_page
91
     * @return  array
92
     * @throws  Exception
93
     */
94 2
    public function getAthleteActivities($before = null, $after = null, $page = null, $per_page = null) {
95
        try {
96 2
            return $this->service->getAthleteActivities($before, $after, $page, $per_page);
97 1
        } catch (ServiceException $e) {
98 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
99
        }
100
    }
101
    
102
    /**
103
     * List athlete friends
104
     * 
105
     * @link    http://strava.github.io/api/v3/follow/#friends
106
     * @param   int $id
107
     * @param   int $page
108
     * @param   int $per_page
109
     * @return  array
110
     * @throws  Exception
111
     */
112 2
    public function getAthleteFriends($id = null, $page = null, $per_page = null) {
113
        try {
114 2
            return $this->service->getAthleteFriends($id, $page, $per_page);
115 1
        } catch (ServiceException $e) {
116 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
117
        }
118
    }
119
    
120
    /**
121
     * List athlete followers
122
     * 
123
     * @link    http://strava.github.io/api/v3/follow/#followers
124
     * @param   int $id
125
     * @param   int $page
126
     * @param   int $per_page
127
     * @return  array
128
     * @throws  Exception
129
     */
130 2
    public function getAthleteFollowers($id = null, $page = null, $per_page = null) {
131
        try {
132 2
            return $this->service->getAthleteFollowers($id, $page, $per_page);
133 1
        } catch (ServiceException $e) {
134 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
135
        }
136
    }
137
    
138
    /**
139
     * List both following
140
     * 
141
     * @link    http://strava.github.io/api/v3/follow/#both
142
     * @param   int $id
143
     * @param   int $page
144
     * @param   int $per_page
145
     * @return  array
146
     * @throws  Exception
147
     */
148 2
    public function getAthleteBothFollowing($id, $page = null, $per_page = null) {
149
        try {
150 2
            return $this->service->getAthleteBothFollowing($id, $page, $per_page);
151 1
        } catch (ServiceException $e) {
152 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
153
        }
154
    }
155
    
156
    /**
157
     * List athlete K/QOMs/CRs
158
     * 
159
     * @link    http://strava.github.io/api/v3/athlete/#koms
160
     * @param   int $id
161
     * @param   int $page
162
     * @param   int $per_page
163
     * @return  array
164
     * @throws  Exception
165
     */
166 2
    public function getAthleteKom($id, $page = null, $per_page = null) {
167
        try {
168 2
            return $this->service->getAthleteKom($id, $page, $per_page);
169 1
        } catch (ServiceException $e) {
170 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
171
        }
172
    }
173
    
174
    /**
175
     * List starred segment
176
     * 
177
     * @link    http://strava.github.io/api/v3/segments/#starred
178
     * @param   int $id
179
     * @param   int $page
180
     * @param   int $per_page
181
     * @return  array
182
     * @throws  Exception
183
     */
184 2
    public function getAthleteStarredSegments($id = null, $page = null, $per_page = null) {
185
        try {
186 2
            return $this->service->getAthleteStarredSegments($id, $page, $per_page);
187 1
        } catch (ServiceException $e) {
188 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
189
        }
190
    }
191
    
192
    /**
193
     * Update current athlete
194
     * 
195
     * @link    http://strava.github.io/api/v3/athlete/#update
196
     * @param   string $city
197
     * @param   string $state
198
     * @param   string $country
199
     * @param   string $sex
200
     * @param   float $weight
201
     * @return  array
202
     * @throws  Exception
203
     */
204 2
    public function updateAthlete($city, $state, $country, $sex, $weight){
205
        try {
206 2
            return $this->service->updateAthlete($city, $state, $country, $sex, $weight);
207 1
        } catch (ServiceException $e) {
208 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
209
        }
210
    }
211
    
212
    /**
213
     * Retrieve an activity
214
     * 
215
     * @link    http://strava.github.io/api/v3/athlete/#get-details,
216
     *          http://strava.github.io/api/v3/athlete/#get-another-details
217
     * @param   int $id
218
     * @param   boolean $include_all_efforts
219
     * @return  array
220
     * @throws  Exception
221
     */
222 2
    public function getActivity($id, $include_all_efforts = null) {
223
        try {
224 2
            return $this->service->getActivity($id, $include_all_efforts);
225 1
        } catch (ServiceException $e) {
226 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
227
        }
228
    }
229
    
230
    /**
231
     * List activity comments
232
     * 
233
     * @link    http://strava.github.io/api/v3/comments/#list
234
     * @param   int $id
235
     * @param   boolean $markdown
236
     * @param   int $page
237
     * @param   int $per_page
238
     * @return  array
239
     * @throws  Exception
240
     */
241 2
    public function getActivityComments($id, $markdown = null, $page = null, $per_page = null) {
242
        try {
243 2
            return $this->service->getActivityComments($id, $markdown, $page, $per_page);
244 1
        } catch (ServiceException $e) {
245 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
246
        }
247
    }
248
    
249
    /**
250
     * List activity kudoers
251
     * 
252
     * @link    http://strava.github.io/api/v3/kudos/#list
253
     * @param   int $id
254
     * @param   int $page
255
     * @param   int $per_page
256
     * @return  array
257
     * @throws  Exception
258
     */
259 2
    public function getActivityKudos($id, $page = null, $per_page = null) {
260
        try {
261 2
            return $this->service->getActivityKudos($id, $page, $per_page);
262 1
        } catch (ServiceException $e) {
263 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
264
        }
265
    }
266
    
267
    /**
268
     * List activity photos
269
     * 
270
     * @link    http://strava.github.io/api/v3/photos/#list
271
     * @param   int $id
272
     * @return  array
273
     * @throws  Exception
274
     */
275 2
    public function getActivityPhotos($id) {
276
        try {
277 2
            return $this->service->getActivityPhotos($id);
278 1
        } catch (ServiceException $e) {
279 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
280
        }
281
    }
282
    
283
    /**
284
     * List activity zones
285
     * 
286
     * @link    http://strava.github.io/api/v3/activities/#zones
287
     * @param   int $id
288
     * @return  array
289
     * @throws  Exception
290
     */
291 2
    public function getActivityZones($id) {
292
        try {
293 2
            return $this->service->getActivityZones($id);
294 1
        } catch (ServiceException $e) {
295 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
296
        }
297
    }
298
    
299
    /**
300
     * List activity laps
301
     * 
302
     * @link    http://strava.github.io/api/v3/activities/#laps
303
     * @param   int $id
304
     * @return  array
305
     * @throws  Exception
306
     */
307 2
    public function getActivityLaps($id) {
308
        try {
309 2
            return $this->service->getActivityLaps($id);
310 1
        } catch (ServiceException $e) {
311 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
312
        }
313
    }
314
    
315
    /**
316
     * Check upload status
317
     * 
318
     * @link    http://strava.github.io/api/v3/uploads/#get-status
319
     * @param   int $id
320
     * @return  array
321
     * @throws  Exception
322
     */
323 2
    public function getActivityUploadStatus($id) {
324
        try {
325 2
            return $this->service->getActivityUploadStatus($id);
326 1
        } catch (ServiceException $e) {
327 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
328
        }
329
    }
330
    
331
    /**
332
     * Create an activity
333
     * 
334
     * @link    http://strava.github.io/api/v3/activities/#create
335
     * @param   string $name
336
     * @param   string $type
337
     * @param   string $start_date_local
338
     * @param   int $elapsed_time
339
     * @param   string $description
340
     * @param   float $distance
341
     * @return  array
342
     * @throws  Exception
343
     */
344 2
    public function createActivity($name, $type, $start_date_local, $elapsed_time, $description = null, $distance = null) {
345
        try {
346 2
            return $this->service->createActivity($name, $type, $start_date_local, $elapsed_time, $description, $distance);
347 1
        } catch (ServiceException $e) {
348 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
349
        }
350
    }
351
    
352
    /**
353
     * Upload an activity
354
     * 
355
     * @link    http://strava.github.io/api/v3/uploads/#post-file
356
     * @param   mixed $file
357
     * @param   string $activity_type
358
     * @param   string $name
359
     * @param   string $description
360
     * @param   int $private
361
     * @param   int $trainer
362
     * @param   string $data_type
363
     * @param   string $external_id
364
     * @return  array
365
     * @throws  Exception
366
     */
367 2
    public function uploadActivity($file, $activity_type = null, $name = null, $description = null, $private = null, $trainer = null, $data_type = null, $external_id = null) { 
368
        try {
369 2
            return $this->service->uploadActivity($file, $activity_type, $name, $description, $private, $trainer, $data_type, $external_id);
370 1
        } catch (ServiceException $e) {
371 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
372
        }
373
    }
374
    
375
    /**
376
     * Update an activity
377
     * 
378
     * @link    http://strava.github.io/api/v3/activities/#put-updates
379
     * @param   int $id
380
     * @param   string $name
381
     * @param   string $type
382
     * @param   boolean $private
383
     * @param   boolean $commute
384
     * @param   boolean $trainer
385
     * @param   string $gear_id
386
     * @param   string $description
387
     * @return  array
388
     * @throws  Exception
389
     */
390 2
    public function updateActivity($id, $name = null, $type = null, $private = false, $commute = false, $trainer = false, $gear_id = null, $description = null) {
391
        try {
392 2
            return $this->service->updateActivity($id, $name, $type, $private, $commute, $trainer, $gear_id, $description);
393 1
        } catch (ServiceException $e) {
394 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
395
        }
396
    }
397
    
398
    /**
399
     * Delete an activity
400
     * 
401
     * @link    http://strava.github.io/api/v3/activities/#delete
402
     * @param   int $id
403
     * @return  array
404
     * @throws  Exception
405
     */
406 2
    public function deleteActivity($id) {
407
        try {
408 2
            return $this->service->deleteActivity($id);
409 1
        } catch (ServiceException $e) {
410 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
411
        }
412
    }
413
    
414
    
415
    /**
416
     * Retrieve gear
417
     * 
418
     * @link    http://strava.github.io/api/v3/gear/
419
     * @param   int $id
420
     * @return  array
421
     * @throws  Exception
422
     */
423 2
    public function getGear($id) {
424
        try {
425 2
            return $this->service->getGear($id);
426 1
        } catch (ServiceException $e) {
427 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
428
        }
429
    }
430
431
    /**
432
     * Retrieve a club
433
     * 
434
     * @link    http://strava.github.io/api/v3/clubs/#get-details
435
     * @param   int $id
436
     * @return  array
437
     * @throws  Exception
438
     */
439 2
    public function getClub($id) {
440
        try {
441 2
            return $this->service->getClub($id);
442 1
        } catch (ServiceException $e) {
443 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
444
        }
445
    }
446
    
447
    /**
448
     * List club members
449
     * 
450
     * @link    http://strava.github.io/api/v3/clubs/#get-members
451
     * @param   int $id
452
     * @param   int $page
453
     * @param   int $per_page
454
     * @return  array
455
     * @throws  Exception
456
     */
457 2
    public function getClubMembers($id, $page = null, $per_page  = null) {
458
        try {
459 2
            return $this->service->getClubMembers($id, $page, $per_page);
460 1
        } catch (ServiceException $e) {
461 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
462
        }
463
    }
464
    
465
    /**
466
     * List club activities
467
     * 
468
     * @link    http://strava.github.io/api/v3/clubs/#get-activities
469
     * @param   int $id
470
     * @param   int $page
471
     * @param   int $per_page
472
     * @return  array
473
     * @throws  Exception
474
     */
475 2
    public function getClubActivities($id, $page = null, $per_page  = null) {
476
        try {
477 2
            return $this->service->getClubActivities($id, $page, $per_page);
478 1
        } catch (ServiceException $e) {
479 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
480
        }
481
    }
482
483
    /**
484
     * List club announcements
485
     *
486
     * @link    https://strava.github.io/api/v3/clubs/#get-announcements
487
     * @param   int $id
488
     * @return  array
489
     * @throws  Exception
490
     */
491 2
    public function getClubAnnouncements($id) {
492
        try {
493 2
            return $this->service->getClubAnnouncements($id);
494 1
        } catch (ServiceException $e) {
495 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
496
        }
497
    }
498
499
    /**
500
     * List club group events
501
     *
502
     * @link    https://strava.github.io/api/v3/clubs/#get-group-events
503
     * @param   int $id
504
     * @return  array
505
     * @throws  Exception
506
     */
507 2
    public function getClubGroupEvents($id) {
508
        try {
509 2
            return $this->service->getClubGroupEvents($id);
510 1
        } catch (ServiceException $e) {
511 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
512
        }
513
    }
514
515
    /**
516
     * Join a club
517
     *
518
     * @link    https://strava.github.io/api/v3/clubs/#join
519
     * @param   int $id
520
     * @return  array
521
     * @throws  Exception
522
     */
523 2
    public function joinClub($id) {
524
        try {
525 2
            return $this->service->joinClub($id);
526 1
        } catch (ServiceException $e) {
527 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
528
        }
529
    }
530
531
    /**
532
     * Leave a club
533
     *
534
     * @link    https://strava.github.io/api/v3/clubs/#leave
535
     * @param   int $id
536
     * @return  array
537
     * @throws  Exception
538
     */
539 2
    public function leaveClub($id) {
540
        try {
541 2
            return $this->service->leaveClub($id);
542 1
        } catch (ServiceException $e) {
543 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
544
        }
545
    }
546
547
    /**
548
     * Retrieve a segment
549
     * 
550
     * @link    http://strava.github.io/api/v3/segments/#retrieve
551
     * @param   int $id
552
     * @return  array
553
     * @throws  Exception
554
     */
555 2
    public function getSegment($id) {
556
        try {
557 2
            return $this->service->getSegment($id);
558 1
        } catch (ServiceException $e) {
559 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
560
        }
561
    }
562
    
563
    /**
564
     * Segment leaderboards
565
     * 
566
     * @link    http://strava.github.io/api/v3/segments/#leaderboard
567
     * @param   int $id
568
     * @param   string $gender
569
     * @param   string $age_group
570
     * @param   string $weight_class
571
     * @param   boolean $following
572
     * @param   int $club_id
573
     * @param   string $date_range
574
     * @param   int $page
575
     * @param   int $per_page
576
     * @return  array
577
     * @throws  Exception
578
     */
579 2
    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) {
580
        try {
581 2
            return $this->service->getSegmentLeaderboard($id, $gender, $age_group, $weight_class, $following, $club_id, $date_range, $page, $per_page);
582 1
        } catch (ServiceException $e) {
583 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
584
        }
585
    }
586
    
587
    /**
588
     * Segment explorer
589
     * 
590
     * @link    http://strava.github.io/api/v3/segments/#explore
591
     * @param   string $bounds
592
     * @param   string $activity_type
593
     * @param   int $min_cat
594
     * @param   int $max_cat
595
     * @return  array
596
     * @throws  Exception
597
     */
598 2
    public function getSegmentExplorer($bounds, $activity_type = 'riding', $min_cat = null, $max_cat = null) {
599
        try {
600 2
            return $this->service->getSegmentExplorer($bounds, $activity_type, $min_cat, $max_cat);
601 1
        } catch (ServiceException $e) {
602 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
603
        }     
604
    }
605
    
606
    /**
607
     * List efforts filtered by athlete and/or a date range
608
     * 
609
     * @link    http://strava.github.io/api/v3/segments/#efforts
610
     * @param   int $id
611
     * @param   int $athlete_id
612
     * @param   string $start_date_local
613
     * @param   string $end_date_local
614
     * @param   int $page
615
     * @param   int $per_page
616
     * @return  array
617
     * @throws  Exception
618
     */
619 2
    public function getSegmentEffort($id, $athlete_id = null, $start_date_local = null, $end_date_local = null, $page = null, $per_page = null) {
620
        try {
621 2
            return $this->service->getSegmentEffort($id, $athlete_id, $start_date_local, $end_date_local, $page, $per_page);
622 1
        } catch (ServiceException $e) {
623 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
624
        }            
625
    }
626
    
627
    /**
628
     * Retrieve activity streams
629
     * 
630
     * @link    http://strava.github.io/api/v3/streams/#activity
631
     * @param   int $id
632
     * @param   string $types
633
     * @param   string $resolution
634
     * @param   string $series_type
635
     * @return  array
636
     * @throws  Exception
637
     */
638 2
    public function getStreamsActivity($id, $types, $resolution = 'all', $series_type = 'distance') {
639
        try {
640 2
            return $this->service->getStreamsActivity($id, $types, $resolution, $series_type);
641 1
        } catch (ServiceException $e) {
642 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
643
        }
644
    }
645
    
646
    /**
647
     * Retrieve effort streams
648
     * 
649
     * @link    http://strava.github.io/api/v3/streams/#effort
650
     * @param   int $id
651
     * @param   string $types
652
     * @param   string $resolution
653
     * @param   string $series_type
654
     * @return  array
655
     * @throws  Exception
656
     */
657 2
    public function getStreamsEffort($id, $types, $resolution = 'all', $series_type = 'distance') {
658
        try {
659 2
            return $this->service->getStreamsEffort($id, $types, $resolution, $series_type);
660 1
        } catch (ServiceException $e) {
661 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
662
        }
663
    }
664
    
665
    /**
666
     * Retrieve segment streams
667
     * @link    http://strava.github.io/api/v3/streams/#segment
668
     * @param   int $id
669
     * @param   string $types
670
     * @param   string $resolution
671
     * @param   string $series_type
672
     * @return  array
673
     * @throws  Exception
674
     */
675 2
    public function getStreamsSegment($id, $types, $resolution = 'all', $series_type = 'distance') {
676
        try {
677 2
            return $this->service->getStreamsSegment($id, $types, $resolution, $series_type);
678 1
        } catch (ServiceException $e) {
679 1
            throw new ClientException('[SERVICE] '.$e->getMessage());
680
        }
681
    }
682
}
683