1 | <?php |
||
11 | final class Authentication |
||
12 | { |
||
13 | private const URL_AUTHENTICATE = 'https://api.mobile.endomondo.com/mobile/auth'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $token; |
||
19 | |||
20 | /** |
||
21 | * @param string $token The token. |
||
22 | */ |
||
23 | private function __construct(string $token) |
||
27 | |||
28 | /** |
||
29 | * Get the token. |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | public function token(): string |
||
37 | |||
38 | public static function withToken(string $token): Authentication |
||
39 | { |
||
40 | return new static($token); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param string $username |
||
45 | * @param string $password |
||
46 | * @param Client $client |
||
47 | * @return Authentication If there is an error making the request. |
||
48 | * @throws \SportTrackerConnector\Core\Tracker\Exception\InvalidCredentialsException |
||
49 | */ |
||
50 | public static function withUsernameAndPassword(string $username, string $password, Client $client): Authentication |
||
51 | { |
||
52 | $response = $client->get( |
||
53 | self::URL_AUTHENTICATE, |
||
54 | array( |
||
55 | 'query' => array( |
||
56 | 'country' => 'GB', |
||
57 | 'action' => 'pair', |
||
58 | 'deviceId' => Uuid::uuid5(Uuid::NAMESPACE_DNS, gethostname())->toString(), |
||
59 | 'email' => $username, |
||
60 | 'password' => $password, |
||
61 | ) |
||
62 | ) |
||
63 | ); |
||
64 | |||
65 | $responseBody = $response->getBody()->getContents(); |
||
66 | $response = parse_ini_string($responseBody); |
||
67 | |||
68 | if (array_key_exists('authToken', $response)) { |
||
69 | return new static($response['authToken']); |
||
70 | } |
||
71 | |||
72 | throw new InvalidCredentialsException('Authentication on Endomondo failed.'); |
||
73 | } |
||
74 | |||
75 | public function __toString() |
||
79 | } |
||
80 |