Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
StravaUtils | |
0.00% |
0 / 49 |
|
0.00% |
0 / 8 |
90 | |
0.00% |
0 / 1 |
fromEnv | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
setClientId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setClientSecret | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setRedirectUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setStravaFetcher | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAuthUrl | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
getTokenDataForCode | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
6 | |||
getUserData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Olz\Utils; |
4 | |
5 | use Olz\Fetchers\StravaFetcher; |
6 | |
7 | class StravaUtils { |
8 | use WithUtilsTrait; |
9 | |
10 | protected ?string $client_id = null; |
11 | protected ?string $client_secret = null; |
12 | protected ?string $redirect_url = null; |
13 | protected ?StravaFetcher $strava_fetcher = null; |
14 | |
15 | public static function fromEnv(): self { |
16 | $env_utils = new EnvUtils(); |
17 | $base_href = $env_utils->getBaseHref(); |
18 | $code_href = $env_utils->getCodeHref(); |
19 | $redirect_url = "{$base_href}{$code_href}konto_strava"; |
20 | $strava_fetcher = new StravaFetcher(); |
21 | |
22 | $instance = new self(); |
23 | $instance->setClientId($env_utils->getStravaClientId()); |
24 | $instance->setClientSecret($env_utils->getStravaClientSecret()); |
25 | $instance->setRedirectUrl($redirect_url); |
26 | $instance->setStravaFetcher($strava_fetcher); |
27 | return $instance; |
28 | } |
29 | |
30 | public function setClientId(?string $client_id): void { |
31 | $this->client_id = $client_id; |
32 | } |
33 | |
34 | public function setClientSecret(?string $client_secret): void { |
35 | $this->client_secret = $client_secret; |
36 | } |
37 | |
38 | public function setRedirectUrl(?string $redirect_url): void { |
39 | $this->redirect_url = $redirect_url; |
40 | } |
41 | |
42 | public function setStravaFetcher(?StravaFetcher $strava_fetcher): void { |
43 | $this->strava_fetcher = $strava_fetcher; |
44 | } |
45 | |
46 | public function getAuthUrl(): string { |
47 | $strava_auth_url = 'https://www.strava.com/oauth/authorize'; |
48 | $data = [ |
49 | 'client_id' => $this->client_id, |
50 | 'redirect_uri' => $this->redirect_url, |
51 | 'response_type' => 'code', |
52 | 'approval_prompt' => 'auto', |
53 | 'scope' => 'profile:read_all', |
54 | ]; |
55 | return "{$strava_auth_url}?".http_build_query($data); |
56 | } |
57 | |
58 | /** @return ?array<string, mixed> */ |
59 | public function getTokenDataForCode(string $code): ?array { |
60 | $token_request_data = [ |
61 | 'client_id' => $this->client_id, |
62 | 'client_secret' => $this->client_secret, |
63 | 'code' => $code, |
64 | 'grant_type' => 'authorization_code', |
65 | ]; |
66 | $this->generalUtils()->checkNotNull($this->strava_fetcher, "No strava fetcher"); |
67 | $token_response = $this->strava_fetcher->fetchTokenDataForCode($token_request_data); |
68 | |
69 | if (!isset($token_response['token_type'])) { |
70 | return null; |
71 | } |
72 | |
73 | return [ |
74 | 'token_type' => $token_response['token_type'], |
75 | 'expires_at' => $token_response['expires_at'], |
76 | 'refresh_token' => $token_response['refresh_token'], |
77 | 'access_token' => $token_response['access_token'], |
78 | 'user_identifier' => $token_response['athlete']['id'], |
79 | 'first_name' => $token_response['athlete']['firstname'], |
80 | 'last_name' => $token_response['athlete']['lastname'], |
81 | 'gender' => $token_response['athlete']['sex'], |
82 | 'city' => $token_response['athlete']['city'], |
83 | 'region' => $token_response['athlete']['state'], |
84 | 'country' => $token_response['athlete']['country'], |
85 | 'profile_picture_url' => $token_response['athlete']['profile'], |
86 | ]; |
87 | } |
88 | |
89 | /** |
90 | * @param array<string, mixed> $token_data |
91 | * |
92 | * @return array<string, mixed> |
93 | */ |
94 | public function getUserData(array $token_data): array { |
95 | return $token_data; |
96 | } |
97 | } |