Completed
Pull Request — master (#42)
by
unknown
03:32
created

REST::getStreamsEffort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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