Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 01: recording a temperature at a location in the event store #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions exercises/exercise-01/record-temperature.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@
use BadMethodCallException;
use EventSourcingWorkshop\EventSourcing\Domain\DomainEvent;
use EventSourcingWorkshop\Glue\Application\Kernel;
use EventSourcingWorkshop\TemperatureTracking\Domain\TemperatureRecorded;
use Psl\Env;
use Psl\Type;
use Throwable;
use UnexpectedValueException;

/**
* Usage: ./record-temperature.php <string $location> <float $celsius>
*
* @psalm-suppress UnusedVariable until the exercise is complete, some unused symbols may be sitting around
*/
(static function (): void {
require_once __DIR__ . '/../../vendor/autoload.php';

try {
[, $location, $temperature] = Type\shape([
1 => Type\string(),
1 => Type\non_empty_string(),
2 => Type\float(),
])->coerce(Env\args());
} catch (Throwable $e) {
Expand All @@ -38,12 +37,14 @@
/**
* Here we want to:
*
* @TODO 1. create a new `TemperatureRecorded` {@see DomainEvent} implementation under ../src/TemperatureTracking/Domain
* @TODO 2. raise the event
* @TODO 2. save that event to the event store (tip: check the `$kernel`'s {@see Kernel::$traverseEventStream})
* @TODO 3. observe the event store
* 1. create a new `TemperatureRecorded` {@see DomainEvent} implementation under ../src/TemperatureTracking/Domain
* 2. raise the event
* 2. save that event to the event store (tip: check the `$kernel`'s {@see Kernel::$eventStore})
* 3. observe the event store
*
* Question: what was saved in the DB?
*/
throw new BadMethodCallException('Incomplete: remove me once finished with the exercise!');
$kernel->eventStore->save(
new TemperatureRecorded($location, $temperature, $kernel->clock->now()),
);
})();
35 changes: 35 additions & 0 deletions src/TemperatureTracking/Domain/TemperatureRecorded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace EventSourcingWorkshop\TemperatureTracking\Domain;

use DateTimeImmutable;
use EventSourcingWorkshop\EventSourcing\Domain\DomainEvent;

/** @psalm-immutable */
final class TemperatureRecorded implements DomainEvent
{
/** @param non-empty-string $location */
public function __construct(
public readonly string $location,
public readonly float $celsius,
public readonly DateTimeImmutable $raisedAt,
) {
}

public function raisedAt(): DateTimeImmutable
{
return $this->raisedAt;
}

/** {@inheritDoc} */
public function toArray(): array
{
return [
'location' => $this->location,
'celsius' => $this->celsius,
'raisedAt' => $this->raisedAt->format(DateTimeImmutable::RFC3339_EXTENDED),
];
}
}