Completed
Push — master ( fec96a...082b8a )
by Bas van
02:24
created

REST::getRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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