Skip to content

Instantly share code, notes, and snippets.

@nickdtodd
Created November 16, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickdtodd/822b4f4509eb87ef89c5b7e271afef23 to your computer and use it in GitHub Desktop.
Save nickdtodd/822b4f4509eb87ef89c5b7e271afef23 to your computer and use it in GitHub Desktop.
<?php
class ChangeStationScheduleSettings implements Command // Implement 'isVersionableProperty' or something???
{
private string $stationId;
private bool $isEnabled;
public function __construct(string $stationId, bool $isEnabled)
{
$this->stationId = $stationId;
$this->isEnabled = $isEnabled;
}
// Getters
}
class PublishDraft implements Command
{
}
class App
{
// All the other attributes
// ...
private string $revisionState;
private array $commands = [];
private function startADraft()
{
$this->revisionState = 'draft';
}
public function handle(Command $command)
{
if($this->getRevisionState() === 'draft') {
$this->persistCommand($command);
return;
}
if($this->state !== 'design') { // We're not at the design stage. Better start a draft
$this->startADraft();
$this->handle($command);
return;
}
$this->handleCommand($command); // State is something else. Just apply the changes
}
private function handleCommand(Command $command)
{
if($command instanceof ChangeStationScheduleSettings) {
$this->handleStationScheduleChangeCommand($command);
}
if($command instanceof PublishDraft) {
$this->revisionState = 'published'; // publish??? An intermediate state??
}
// Other handlers here
}
public function handleStationScheduleChangeCommand(ChangeStationScheduleCommand $command)
{
$appStation = $this->stations->get($command->getStationId());
$appStation->setScheduleSettings(new AppStationScheduleSettings($command->isEnabled()));
}
private function persistCommand(Command $command)
{
$this->commands[] = $command;
}
public function applyCommands()
{
foreach ($this->commands as $command) {
$this->handleCommand($command);
}
}
public function purgeCommands()
{
$this->commands = [];
}
}
class AppRepository
{
public function getPublishedVersion(string $appId): ?App // The currently published version with no changes
{
$app = $this->em->findById($appId);
return $app;
}
public function getLatestVersion(string $appId): ?App // The draft version. Could be the same as above???
{
$app = $this->em->findById($appId);
$app->applyCommands();
return $app;
}
public function save(App $app)
{
$this->em->persist($app);
$this->em->flush();
}
public function saveAndPublish(App $app) // A good idea doing this here??
{
$app->handle(new PublishDraft());
$app->applyCommands();
$app->purgeCommands();
$this->save($app);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment