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 02: project recorded temperatures events to a file containing latest locations/temperatures #18

Open
wants to merge 1 commit into
base: feature/exercise-01-recording-temperature
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
3 changes: 2 additions & 1 deletion data/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
database.sqlite
database.sqlite
last-temperatures.json
41 changes: 36 additions & 5 deletions exercises/exercise-02/project-last-temperature.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@

namespace EventSourcingWorkshop\Exercise\TemperatureTracking;

use BadMethodCallException;
use EventSourcingWorkshop\Glue\Application\Kernel;
use EventSourcingWorkshop\TemperatureTracking\Domain\TemperatureRecorded;

use function file_exists;
use function file_put_contents;
use function json_encode;
use function Psl\File\read;
use function Psl\Json\typed;
use function Psl\Type\dict;
use function Psl\Type\float;
use function Psl\Type\non_empty_string;

(static function (): void {
require_once __DIR__ . '/../../vendor/autoload.php';
Expand All @@ -18,12 +27,34 @@
/**
* Here we want to:
*
* @TODO 1. iterate over recorded temperatures (tip: check the `$kernel`'s {@see Kernel::$traverseEventStream})
* @TODO 2. generate an `array<string, float>` containing the last known temperature at each location
* @TODO 3. save all accumulated temperatures to a file
* 1. iterate over recorded temperatures (tip: check the `$kernel`'s {@see Kernel::$traverseEventStream})
* 2. generate an `array<string, float>` containing the last known temperature at each location
* 3. save all accumulated temperatures to a file
*
* Question: what happens when you run this script multiple times?
* Question: can you record new temperatures, and track them?
*/
throw new BadMethodCallException('Incomplete: remove me once finished with the exercise!');

/** @var array<non-empty-string, float> $temperatures */
$temperatures = [];

if (file_exists(__DIR__ . '/../../data/last-temperatures.json')) {
$temperatures = typed(
read(__DIR__ . '/../../data/last-temperatures.json'),
dict(
non_empty_string(),
float(),
),
);
}

foreach (($kernel->traverseEventStream)('last-recorded-temperatures') as $event) {
if (! $event instanceof TemperatureRecorded) {
continue;
}

$temperatures[$event->location] = $event->celsius;
}

file_put_contents(__DIR__ . '/../../data/last-temperatures.json', json_encode($temperatures));
})();