Skip to content

Commit b7660be

Browse files
Import athlete weights
1 parent 39366ef commit b7660be

File tree

9 files changed

+165
-0
lines changed

9 files changed

+165
-0
lines changed

.env.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ FTP_VALUES='{
1414
"2023-08-01": 238,
1515
"2023-09-24": 250,
1616
"2024-03-26": 258
17+
}'
18+
ATHLETE_WEIGHTS='{
19+
"2023-04-01": 74.6,
20+
"2023-05-25": 70.3
1721
}'

config/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ services:
7474
factory: [ App\Domain\Strava\Athlete\AthleteBirthday, 'fromString' ]
7575
arguments: ['%env(resolve:ATHLETE_BIRTHDAY)%']
7676

77+
App\Domain\Strava\Athlete\Weight\ImportAthleteWeight\AthleteWeightsFromEnvFile:
78+
factory: [ 'App\Domain\Strava\Athlete\Weight\ImportAthleteWeight\AthleteWeightsFromEnvFile', fromString ]
79+
arguments: [ '%env(resolve:ATHLETE_WEIGHTS)%' ]
80+
7781
App\Domain\Strava\Ftp\ImportFtp\FtpValuesFromEnvFile:
7882
factory: [ 'App\Domain\Strava\Ftp\ImportFtp\FtpValuesFromEnvFile', fromString ]
7983
arguments: [ '%env(resolve:FTP_VALUES)%' ]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain\Strava\Athlete\Weight;
6+
7+
use App\Infrastructure\ValueObject\Collection;
8+
9+
final class AthleteWeights extends Collection
10+
{
11+
public function getItemClassName(): string
12+
{
13+
return AthleteWeight::class;
14+
}
15+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain\Strava\Athlete\Weight\ImportAthleteWeight;
6+
7+
use App\Domain\Strava\Athlete\Weight\AthleteWeight;
8+
use App\Domain\Strava\Athlete\Weight\AthleteWeights;
9+
use App\Infrastructure\Serialization\Json;
10+
use App\Infrastructure\ValueObject\Time\SerializableDateTime;
11+
12+
final readonly class AthleteWeightsFromEnvFile
13+
{
14+
private AthleteWeights $weights;
15+
16+
/**
17+
* @param array<string, float> $weightsInKg
18+
*/
19+
private function __construct(
20+
array $weightsInKg,
21+
) {
22+
$this->weights = AthleteWeights::empty();
23+
24+
foreach ($weightsInKg as $on => $weightInKg) {
25+
$this->weights->add(AthleteWeight::fromState(
26+
on: SerializableDateTime::fromString($on),
27+
weightInGrams: (int) ($weightInKg * 1000),
28+
));
29+
}
30+
}
31+
32+
public function getAll(): AthleteWeights
33+
{
34+
return $this->weights;
35+
}
36+
37+
public static function fromString(string $values): self
38+
{
39+
return new self(
40+
Json::decode($values)
41+
);
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain\Strava\Athlete\Weight\ImportAthleteWeight;
6+
7+
use App\Infrastructure\CQRS\Bus\DomainCommand;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
final class ImportAthleteWeight extends DomainCommand
11+
{
12+
public function __construct(
13+
private readonly OutputInterface $output,
14+
) {
15+
}
16+
17+
public function getOutput(): OutputInterface
18+
{
19+
return $this->output;
20+
}
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain\Strava\Athlete\Weight\ImportAthleteWeight;
6+
7+
use App\Domain\Strava\Athlete\Weight\AthleteWeightRepository;
8+
use App\Infrastructure\CQRS\Bus\Command;
9+
use App\Infrastructure\CQRS\Bus\CommandHandler;
10+
11+
final readonly class ImportAthleteWeightCommandHandler implements CommandHandler
12+
{
13+
public function __construct(
14+
private AthleteWeightsFromEnvFile $athleteWeightsFromEnvFile,
15+
private AthleteWeightRepository $athleteWeightRepository,
16+
) {
17+
}
18+
19+
public function handle(Command $command): void
20+
{
21+
assert($command instanceof ImportAthleteWeight);
22+
$command->getOutput()->writeln('Importing weights...');
23+
24+
$this->athleteWeightRepository->removeAll();
25+
26+
if (!$athleteWeights = $this->athleteWeightsFromEnvFile->getAll()) {
27+
$command->getOutput()->writeln('No athlete weights found. Will not be able to calculate relative power outputs');
28+
}
29+
30+
/** @var \App\Domain\Strava\Athlete\Weight\AthleteWeight $weight */
31+
foreach ($athleteWeights as $weight) {
32+
$this->athleteWeightRepository->save($weight);
33+
$command->getOutput()->writeln(sprintf(
34+
' => Imported weight set on %s (%s kg)...',
35+
$weight->getOn()->format('d-m-Y'),
36+
$weight->getWeightInKg()
37+
));
38+
}
39+
}
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Tests\Domain\Strava\Athlete\Weight\ImportAthleteWeight;
4+
5+
use App\Domain\Strava\Athlete\Weight\ImportAthleteWeight\ImportAthleteWeight;
6+
use App\Domain\Strava\Strava;
7+
use App\Infrastructure\CQRS\Bus\CommandBus;
8+
use App\Tests\ContainerTestCase;
9+
use App\Tests\Domain\Strava\SpyStrava;
10+
use App\Tests\SpyOutput;
11+
use Spatie\Snapshots\MatchesSnapshots;
12+
13+
class ImportAthleteWeightCommandHandlerTest extends ContainerTestCase
14+
{
15+
use MatchesSnapshots;
16+
17+
private CommandBus $commandBus;
18+
private SpyStrava $strava;
19+
20+
public function testHandle(): void
21+
{
22+
$output = new SpyOutput();
23+
$this->commandBus->dispatch(new ImportAthleteWeight($output));
24+
25+
$this->assertMatchesTextSnapshot((string) $output);
26+
}
27+
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
$this->commandBus = $this->getContainer()->get(CommandBus::class);
33+
$this->strava = $this->getContainer()->get(Strava::class);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Importing weights...
2+
=> Imported weight set on 01-04-2023 (74.6 kg)...
3+
=> Imported weight set on 25-05-2023 (70.3 kg)...

tests/strava.db

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)