1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace SportTrackerConnector\Endomondo; |
6
|
|
|
|
7
|
|
|
use SportTrackerConnector\Core\Tracker\TrackerInterface; |
8
|
|
|
use SportTrackerConnector\Core\Workout\Extension\HR; |
9
|
|
|
use SportTrackerConnector\Core\Workout\SportMapperInterface; |
10
|
|
|
use SportTrackerConnector\Core\Workout\Track; |
11
|
|
|
use SportTrackerConnector\Core\Workout\TrackPoint; |
12
|
|
|
use SportTrackerConnector\Core\Workout\Workout; |
13
|
|
|
use SportTrackerConnector\Core\Workout\WorkoutIdInterface; |
14
|
|
|
use SportTrackerConnector\Core\Workout\WorkoutSummary; |
15
|
|
|
use SportTrackerConnector\Endomondo\API\Workouts; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Endomondo tracker. |
19
|
|
|
*/ |
20
|
|
|
final class EndomondoTracker implements TrackerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The Endomondo Workouts API. |
24
|
|
|
* |
25
|
|
|
* @var Workouts |
26
|
|
|
*/ |
27
|
|
|
protected $endomondoWorkouts; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Workouts $endomondoWorkouts |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Workouts $endomondoWorkouts) |
33
|
|
|
{ |
34
|
|
|
$this->endomondoWorkouts = $endomondoWorkouts; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function list(\DateTimeImmutable $startDate, \DateTimeImmutable $endDate): array |
41
|
|
|
{ |
42
|
|
|
$list = array(); |
43
|
|
|
$data = $this->endomondoWorkouts->listWorkouts($startDate, $endDate); |
44
|
|
|
foreach ($data as $workout) { |
45
|
|
|
$startDateTime = \DateTimeImmutable::createFromFormat( |
46
|
|
|
'Y-m-d H:i:s \U\T\C', |
47
|
|
|
$workout['start_time'], |
48
|
|
|
new \DateTimeZone('UTC') |
49
|
|
|
); |
50
|
|
|
$list[] = new WorkoutSummary( |
51
|
|
|
new WorkoutId((string)$workout['id']), |
52
|
|
|
$this->sportMapper()->sportFromCode((string)$workout['sport']), |
53
|
|
|
$startDateTime |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $list; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function workout(WorkoutIdInterface $idWorkout): Workout |
64
|
|
|
{ |
65
|
|
|
$json = $this->endomondoWorkouts->getWorkout($idWorkout->toString()); |
66
|
|
|
|
67
|
|
|
$trackPoints = []; |
68
|
|
|
if (array_key_exists('points', $json)) { |
69
|
|
|
foreach ($json['points'] as $point) { |
70
|
|
|
$elevation = null; |
71
|
|
|
if (array_key_exists('alt', $point)) { |
72
|
|
|
$elevation = $point['alt']; |
73
|
|
|
} |
74
|
|
|
$extensions = []; |
75
|
|
|
if (array_key_exists('hr', $point)) { |
76
|
|
|
$extensions[] = HR::fromValue($point['hr']); |
77
|
|
|
} |
78
|
|
|
$trackPoint = TrackPoint::with( |
79
|
|
|
$point['lat'], |
80
|
|
|
$point['lng'], |
81
|
|
|
new \DateTimeImmutable($point['time']), |
82
|
|
|
$elevation, |
83
|
|
|
$extensions |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$trackPoints[] = $trackPoint; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$sport = SportMapperInterface::OTHER; |
91
|
|
|
if (isset($json['sport'])) { |
92
|
|
|
$sport = $this->sportMapper()->sportFromCode((string)$json['sport']); |
93
|
|
|
} |
94
|
|
|
$track = new Track($trackPoints, $sport); |
95
|
|
|
|
96
|
|
|
return new Workout([$track]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function workouts(\DateTimeImmutable $startDate, \DateTimeImmutable $endDate): array |
103
|
|
|
{ |
104
|
|
|
$list = $this->list($startDate, $endDate); |
105
|
|
|
$workouts = array(); |
106
|
|
|
foreach ($list as $workoutSummary) { |
107
|
|
|
$workouts[] = $this->workout($workoutSummary->workoutId()); |
108
|
|
|
} |
109
|
|
|
return $workouts; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function save(Workout $workout): bool |
116
|
|
|
{ |
117
|
|
|
$workoutIds = array(); |
118
|
|
|
foreach ($workout->tracks() as $track) { |
119
|
|
|
$sport = $this->sportMapper()->codeFromSport($track->sport()); |
120
|
|
|
|
121
|
|
|
$workoutIds[] = $this->endomondoWorkouts->postTrack($track, $sport); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return count(array_filter($workoutIds)) > 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function sportMapper(): SportMapperInterface |
131
|
|
|
{ |
132
|
|
|
return new SportMapper(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public static function ID(): string |
139
|
|
|
{ |
140
|
|
|
return 'endomondo'; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|