Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Compatibility with latest changes from stc-core
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosprotung committed Dec 16, 2016
1 parent bb815c0 commit 2765612
Show file tree
Hide file tree
Showing 19 changed files with 746 additions and 462 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
}
],
"require": {
"php": "^7.1",
"dragosprotung/stc-core": "~2.0@dev",
"beberlei/assert": "^2.6",
"guzzlehttp/guzzle": "^6.2",
"ramsey/uuid": "^3.5"
},
"require-dev": {
"phpunit/phpunit": "^5.5"
"phpunit/phpunit": "^5.7"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 6 additions & 6 deletions src/API/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Ramsey\Uuid\Uuid;
use SportTrackerConnector\Core\Tracker\Exception\InvalidCredentialsException;

class Authentication
final class Authentication
{
const URL_AUTHENTICATE = 'https://api.mobile.endomondo.com/mobile/auth';
private const URL_AUTHENTICATE = 'https://api.mobile.endomondo.com/mobile/auth';

/**
* @var string
Expand All @@ -30,12 +30,12 @@ private function __construct(string $token)
*
* @return string
*/
public function token() : string
public function token(): string
{
return $this->token;
}

public static function fromToken(string $token) : Authentication
public static function withToken(string $token): Authentication
{
return new static($token);
}
Expand All @@ -47,15 +47,15 @@ public static function fromToken(string $token) : Authentication
* @return Authentication If there is an error making the request.
* @throws \SportTrackerConnector\Core\Tracker\Exception\InvalidCredentialsException
*/
public static function fromUsernameAndPassword(string $username, string $password, Client $client) : Authentication
public static function withUsernameAndPassword(string $username, string $password, Client $client): Authentication
{
$response = $client->get(
self::URL_AUTHENTICATE,
array(
'query' => array(
'country' => 'GB',
'action' => 'pair',
'deviceId' => (string)Uuid::uuid5(Uuid::NAMESPACE_DNS, gethostname()),
'deviceId' => Uuid::uuid5(Uuid::NAMESPACE_DNS, gethostname())->toString(),
'email' => $username,
'password' => $password,
)
Expand Down
53 changes: 24 additions & 29 deletions src/API/Workouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
*/
class Workouts
{
const URL_BASE = 'https://api.mobile.endomondo.com/mobile';
const URL_WORKOUTS = 'https://api.mobile.endomondo.com/mobile/api/workouts';
const URL_WORKOUT_GET = 'https://api.mobile.endomondo.com/mobile/api/workout/get';
const URL_WORKOUT_POST = 'https://api.mobile.endomondo.com/mobile/api/workout/post';
const URL_TRACK = 'https://api.mobile.endomondo.com/mobile/track';
const URL_FRIENDS = 'https://api.mobile.endomondo.com/mobile/friends';

const INSTRUCTION_PAUSE = 0;
const INSTRUCTION_RESUME = 1;
const INSTRUCTION_START = 2;
const INSTRUCTION_STOP = 3;
const INSTRUCTION_NONE = 4;
const INSTRUCTION_GPS_OFF = 5;
const INSTRUCTION_LAP = 6;
private const URL_BASE = 'https://api.mobile.endomondo.com/mobile';
private const URL_WORKOUTS = 'https://api.mobile.endomondo.com/mobile/api/workouts';
private const URL_WORKOUT_GET = 'https://api.mobile.endomondo.com/mobile/api/workout/get';
private const URL_WORKOUT_POST = 'https://api.mobile.endomondo.com/mobile/api/workout/post';
private const URL_TRACK = 'https://api.mobile.endomondo.com/mobile/track';
private const URL_FRIENDS = 'https://api.mobile.endomondo.com/mobile/friends';

private const INSTRUCTION_PAUSE = 0;
private const INSTRUCTION_RESUME = 1;
private const INSTRUCTION_START = 2;
private const INSTRUCTION_STOP = 3;
private const INSTRUCTION_NONE = 4;
private const INSTRUCTION_GPS_OFF = 5;
private const INSTRUCTION_LAP = 6;

/**
* Endomondo authentication token.
Expand Down Expand Up @@ -62,7 +62,7 @@ public function __construct(Authentication $authentication, Client $client)
* @return array
* @throws \RuntimeException
*/
public function getWorkout(string $idWorkout) : array
public function getWorkout($idWorkout): array
{
$response = $this
->client
Expand All @@ -89,11 +89,11 @@ public function getWorkout(string $idWorkout) : array
/**
* Get a list of workouts in a date interval.
*
* @param \DateTime $startDate The start date for the workouts.
* @param \DateTime $endDate The end date for the workouts.
* @param \DateTimeImmutable $startDate The start date for the workouts.
* @param \DateTimeImmutable $endDate The end date for the workouts.
* @return array
*/
public function listWorkouts(\DateTime $startDate, \DateTime $endDate) : array
public function listWorkouts(\DateTimeImmutable $startDate, \DateTimeImmutable $endDate): array
{
$response = $this
->client
Expand Down Expand Up @@ -136,12 +136,8 @@ public function postTrack(Track $track, string $sport)
$data = array();
/** @var TrackPoint[] $trackPoints */
foreach ($trackPoints as $trackPoint) {
if ($trackPoint->hasDistance() === true) {
$distance = $trackPoint->distance();
} elseif ($previousPoint !== null) {
$distance += $trackPoint->distanceFromPoint($previousPoint);
}
if ($previousPoint !== null) {
$distance += $trackPoint->distanceFromPoint($previousPoint);
$speed = $trackPoint->speed($previousPoint);
}

Expand Down Expand Up @@ -198,7 +194,7 @@ private function flattenEndWorkoutTrackPoint(Track $track, $speed)
* @return string The workout ID.
* @throws \RuntimeException
*/
private function postWorkoutData($deviceWorkoutId, $sport, $duration, array $data) : string
private function postWorkoutData($deviceWorkoutId, $sport, $duration, array $data): string
{
$body = \GuzzleHttp\Psr7\stream_for(gzencode(implode("\n", $data)));

Expand Down Expand Up @@ -242,7 +238,7 @@ private function postWorkoutData($deviceWorkoutId, $sport, $duration, array $dat
* @param float $speed The speed the point in km/h from the previous point.
* @return string
*/
private function flattenTrackPoint(TrackPoint $trackPoint, $distance, $speed) : string
private function flattenTrackPoint(TrackPoint $trackPoint, $distance, $speed): string
{
$dateTime = clone $trackPoint->dateTime();
$dateTime->setTimezone(new \DateTimeZone('UTC'));
Expand All @@ -268,7 +264,7 @@ private function flattenTrackPoint(TrackPoint $trackPoint, $distance, $speed) :
* 2 - running
* 3 - stop
*
* @param \DateTime $dateTime
* @param \DateTimeImmutable $dateTime
* @param integer $type The post type (0-6). Don't know what they mean.
* @param string $lat The latitude of the point.
* @param string $lon The longitude of the point.
Expand All @@ -280,7 +276,7 @@ private function flattenTrackPoint(TrackPoint $trackPoint, $distance, $speed) :
* @return string
*/
private function formatEndomondoTrackPoint(
\DateTime $dateTime,
\DateTimeImmutable $dateTime,
$type,
$lat = null,
$lon = null,
Expand All @@ -289,8 +285,7 @@ private function formatEndomondoTrackPoint(
$elevation = null,
$heartRate = null,
$cadence = null
) : string
{
): string {
$dateTime = clone $dateTime;
$dateTime->setTimezone(new \DateTimeZone('UTC'));

Expand Down
95 changes: 56 additions & 39 deletions src/EndomondoTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@

namespace SportTrackerConnector\Endomondo;

use DateTime;
use DateTimeZone;
use SportTrackerConnector\Core\Tracker\AbstractTracker;
use SportTrackerConnector\Core\Tracker\TrackerListWorkoutsResult;
use SportTrackerConnector\Core\Tracker\TrackerInterface;
use SportTrackerConnector\Core\Workout\Extension\HR;
use SportTrackerConnector\Core\Workout\SportMapperInterface;
use SportTrackerConnector\Core\Workout\Track;
use SportTrackerConnector\Core\Workout\TrackPoint;
use SportTrackerConnector\Core\Workout\Workout;
use SportTrackerConnector\Core\Workout\WorkoutIdInterface;
use SportTrackerConnector\Core\Workout\WorkoutSummary;
use SportTrackerConnector\Endomondo\API\Workouts;

/**
* Endomondo tracker.
*/
class EndomondoTracker extends AbstractTracker
final class EndomondoTracker implements TrackerInterface
{
/**
* The Endomondo Workouts API.
Expand All @@ -38,64 +37,82 @@ public function __construct(Workouts $endomondoWorkouts)
/**
* {@inheritdoc}
*/
public function workout($idWorkout) : Workout
public function list(\DateTimeImmutable $startDate, \DateTimeImmutable $endDate): array
{
$json = $this->endomondoWorkouts->getWorkout($idWorkout);
$list = array();
$data = $this->endomondoWorkouts->listWorkouts($startDate, $endDate);
foreach ($data as $workout) {
$startDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d H:i:s \U\T\C',
$workout['start_time'],
new \DateTimeZone('UTC')
);
$list[] = new WorkoutSummary(
new WorkoutId((string)$workout['id']),
$this->sportMapper()->sportFromCode((string)$workout['sport']),
$startDateTime
);
}

$workout = new Workout();
$sport = $this->sportMapper()->sportFromCode((string)$json['sport']);
$track = new Track(array(), $sport);
return $list;
}

/**
* {@inheritdoc}
*/
public function workout(WorkoutIdInterface $idWorkout): Workout
{
$json = $this->endomondoWorkouts->getWorkout($idWorkout->toString());

$trackPoints = [];
if (array_key_exists('points', $json)) {
foreach ($json['points'] as $point) {
$trackPoint = new TrackPoint(
$point['lat'],
$point['lng'],
new DateTime($point['time'])
);
$elevation = null;
if (array_key_exists('alt', $point)) {
$trackPoint->setElevation($point['alt']);
$elevation = $point['alt'];
}
$extensions = [];
if (array_key_exists('hr', $point)) {
$trackPoint->addExtension(new HR($point['hr']));
$extensions[] = HR::fromValue($point['hr']);
}
$trackPoint = TrackPoint::with(
$point['lat'],
$point['lng'],
new \DateTimeImmutable($point['time']),
$elevation,
$extensions
);

$track->addTrackPoint($trackPoint);
$trackPoints[] = $trackPoint;
}
}

$workout->addTrack($track);
$sport = SportMapperInterface::OTHER;
if (isset($json['sport'])) {
$sport = $this->sportMapper()->sportFromCode((string)$json['sport']);
}
$track = new Track($trackPoints, $sport);

return $workout;
return new Workout([$track]);
}

/**
* {@inheritdoc}
*/
public function workouts(DateTime $startDate, DateTime $endDate) : array
public function workouts(\DateTimeImmutable $startDate, \DateTimeImmutable $endDate): array
{
$list = array();
$data = $this->endomondoWorkouts->listWorkouts($startDate, $endDate);
foreach ($data as $workout) {
$startDateTime = DateTime::createFromFormat(
'Y-m-d H:i:s \U\T\C',
$workout['start_time'],
new DateTimeZone('UTC')
);
$list[] = new TrackerListWorkoutsResult(
(string)$workout['id'],
$this->sportMapper()->sportFromCode((string)$workout['sport']),
$startDateTime
);
$list = $this->list($startDate, $endDate);
$workouts = array();
foreach ($list as $workoutSummary) {
$workouts[] = $this->workout($workoutSummary->workoutId());
}

return $list;
return $workouts;
}

/**
* {@inheritdoc}
*/
public function post(Workout $workout) : bool
public function save(Workout $workout): bool
{
$workoutIds = array();
foreach ($workout->tracks() as $track) {
Expand All @@ -110,15 +127,15 @@ public function post(Workout $workout) : bool
/**
* {@inheritdoc}
*/
protected function constructSportMapper() : SportMapperInterface
public function sportMapper(): SportMapperInterface
{
return new SportMapper();
}

/**
* {@inheritdoc}
*/
public static function ID() : string
public static function ID(): string
{
return 'endomondo';
}
Expand Down
2 changes: 1 addition & 1 deletion src/SportMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class SportMapper extends AbstractSportMapper
/**
* {@inheritdoc}
*/
public function getMap() : array
public function getMap(): array
{
return array(
self::RUNNING => self::SPORT_RUNNING,
Expand Down
40 changes: 40 additions & 0 deletions src/WorkoutId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types = 1);

namespace SportTrackerConnector\Endomondo;

use SportTrackerConnector\Core\Workout\WorkoutIdInterface;

class WorkoutId implements WorkoutIdInterface
{
/**
* @var string
*/
private $id;

/**
* @param string $id
*/
public function __construct(string $id)
{
$this->id = $id;
}

/**
* @return string
*/
public function toString(): string
{
return $this->id;
}

/**
* @param WorkoutIdInterface $other
* @return bool
*/
public function equals(WorkoutIdInterface $other): bool
{
return $this->toString() === $other->toString();
}
}

0 comments on commit 2765612

Please sign in to comment.