Skip to content

Commit bcda858

Browse files
committed
Add Value Object in Domain
1 parent 1aeb65d commit bcda858

File tree

9 files changed

+90
-8
lines changed

9 files changed

+90
-8
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ install:
4040

4141
script:
4242
- make test
43+
44+
after_script:
45+
- make prune

docs/openapi.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ components:
6262
allOf:
6363
# Inherit PutRunningSession
6464
- $ref: '#/components/schemas/PutRunningSession'
65-
required: [ temperatureCelcius ]
65+
required: [ temperatureCelcius, is_freezing ]
6666
properties:
6767
temperatureCelcius:
6868
type: number
6969
format: float
7070
example: 37.2
71+
is_freezing:
72+
type: boolean
7173

7274
RunningSessionId:
7375
type: integer

src/Application/Command/RegisterRunningSessionHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Domain\RunningSession;
88
use App\Domain\RunningSessionRepository;
9+
use App\Domain\Temperature;
910
use App\Domain\WeatherProvider;
1011

1112
class RegisterRunningSessionHandler
@@ -27,7 +28,7 @@ public function handle(RegisterRunningSession $command): RunningSession
2728
$command->getId(),
2829
$command->getDistance(),
2930
$command->getShoes(),
30-
$currentTemperature
31+
new Temperature($currentTemperature)
3132
);
3233

3334
$this->repository->add($session);

src/Domain/RunningSession.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class RunningSession
99
private int $id;
1010
private float $distance;
1111
private string $shoes;
12-
private float $metricTemperature;
12+
private Temperature $temperature;
1313

14-
public function __construct(int $id, float $distance, string $shoes, float $metricTemperature)
14+
public function __construct(int $id, float $distance, string $shoes, Temperature $temperature)
1515
{
1616
$this->id = $id;
1717
$this->distance = $distance;
1818
$this->shoes = $shoes;
19-
$this->metricTemperature = $metricTemperature;
19+
$this->temperature = $temperature;
2020
}
2121

2222
public function getId(): int
@@ -36,6 +36,11 @@ public function getShoes(): string
3636

3737
public function getMetricTemperature(): float
3838
{
39-
return $this->metricTemperature;
39+
return $this->temperature->getMetricTemperature();
40+
}
41+
42+
public function isFreezing(): bool
43+
{
44+
return $this->temperature->isFreezing();
4045
}
4146
}

src/Domain/Temperature.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain;
6+
7+
use Webmozart\Assert\Assert;
8+
9+
class Temperature
10+
{
11+
private const ABSOLUTE_ZERO = -273.15;
12+
13+
private float $metricTemperature;
14+
15+
public function __construct(float $metricTemperature)
16+
{
17+
Assert::greaterThanEq(
18+
$metricTemperature,
19+
self::ABSOLUTE_ZERO,
20+
"Cannot create temperature below absolute zero: $metricTemperature"
21+
);
22+
23+
$this->metricTemperature = $metricTemperature;
24+
}
25+
26+
public function getMetricTemperature(): float
27+
{
28+
return $this->metricTemperature;
29+
}
30+
31+
public function isFreezing(): bool
32+
{
33+
return $this->metricTemperature < 0;
34+
}
35+
}

src/Infrastructure/Symfony/Serializer/RunningSessionSerializer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function serialize(RunningSession $session): string
1818
'distance' => $session->getDistance(),
1919
'shoes' => $session->getShoes(),
2020
'temperatureCelcius' => $session->getMetricTemperature(),
21+
'is_freezing' => $session->isFreezing(),
2122
];
2223

2324
return json_encode($data, JSON_THROW_ON_ERROR);

tests/Domain/RunningSessionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public static function create(
1212
string $shoes = 'shoes_value_not_used',
1313
float $metricTemperature = 98.76
1414
): RunningSession {
15-
return new RunningSession($id, $distance, $shoes, $metricTemperature);
15+
return new RunningSession($id, $distance, $shoes, new Temperature($metricTemperature));
1616
}
1717
}

tests/Domain/TemperatureTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class TemperatureTest extends TestCase
10+
{
11+
public function testIsNotFreezingAboveZero()
12+
{
13+
//Given
14+
$temperature = new Temperature(25.7);
15+
16+
//When
17+
$result = $temperature->isFreezing();
18+
19+
//Then
20+
self::assertFalse($result, 'temperature should not be freezing');
21+
}
22+
23+
public function testIsFreezingBelowZero()
24+
{
25+
//Given
26+
$temperature = new Temperature(-12);
27+
28+
//When
29+
$result = $temperature->isFreezing();
30+
31+
//Then
32+
self::assertTrue($result, 'temperature should be freezing');
33+
}
34+
}

tests/Infrastructure/Symfony/Serializer/RunningSessionSerializerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public function testRunningSessionIsConvertedToJson()
2929
"id": 42,
3030
"distance": 5.5,
3131
"shoes": "Adadis Turbo2",
32-
"temperatureCelcius": 37.2
32+
"temperatureCelcius": 37.2,
33+
"is_freezing": false
3334
}
3435
EOD;
3536
self::assertJsonStringEqualsJsonString($expectedJson, $result);

0 commit comments

Comments
 (0)