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

Added recording of users checking into buildings #1

Open
wants to merge 2 commits into
base: master
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
11 changes: 11 additions & 0 deletions container.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ public function onRoute(ActionEvent $actionEvent) : void
$buildings->store(Building::new($command->name()));
};
},
Command\CheckIn::class => function (ContainerInterface $container) : callable {
$buildings = $container->get(BuildingRepositoryInterface::class);

return function (Command\CheckIn $command) use ($buildings) : void {
$building = $buildings->get($command->building());

$building->checkInUser($command->username());

$buildings->store($building);
};
},
BuildingRepositoryInterface::class => function (ContainerInterface $container) : BuildingRepositoryInterface {
return new BuildingRepository(
new AggregateRepository(
Expand Down
12 changes: 10 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@
return $response;
});

$app->post('/checkin/{buildingId}', function (Request $request, Response $response) use ($sm) : Response {
throw new \BadFunctionCallException('To be implemented: I should dispatch a command and redirect back to the previous page');
$app->post('/checkin/{buildingId}', function (Request $request, Response $response) use ($sm, $getBodyParameter) : Response {
$sm->get(CommandBus::class)
->dispatch(Command\CheckIn::fromBuildingAndUsername(
Uuid::fromString($request->getAttribute('buildingId')),
$getBodyParameter($request, 'username')
));

return $response
->withStatus(302, 'Found')
->withAddedHeader('Location', '/building/' . $request->getAttribute('buildingId'));
});

$app->post('/checkout/{buildingId}', function (Request $request, Response $response) use ($sm) : Response {
Expand Down
12 changes: 11 additions & 1 deletion src/Domain/Aggregate/Building.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Building\Domain\Aggregate;

use Building\Domain\DomainEvent\NewBuildingWasRegistered;
use Building\Domain\DomainEvent\UserCheckedIn;
use Prooph\EventSourcing\AggregateRoot;
use Rhumsaa\Uuid\Uuid;
use Webmozart\Assert\Assert;
Expand All @@ -26,7 +27,11 @@ public static function new(string $name) : self

public function checkInUser(string $username) : void
{
throw new \BadFunctionCallException('To be implemented: I should record a new event on the building');
$id = $this->uuid;

Assert::notNull($id);

$this->recordThat(UserCheckedIn::toBuilding($id, $username));
}

public function checkOutUser(string $username) : void
Expand All @@ -40,6 +45,11 @@ protected function whenNewBuildingWasRegistered(NewBuildingWasRegistered $event)
$this->name = $event->name();
}

protected function whenUserCheckedIn(UserCheckedIn $event) : void
{
// Empty (on purpose)
}

/** {@inheritDoc} */
protected function aggregateId() : string
{
Expand Down
53 changes: 53 additions & 0 deletions src/Domain/Command/CheckIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Building\Domain\Command;

use Prooph\Common\Messaging\Command;
use Rhumsaa\Uuid\Uuid;

final class CheckIn extends Command
{
private Uuid $building;
private string $username;

private function __construct(Uuid $building, string $username)
{
$this->init();

$this->building = $building;
$this->username = $username;
}

public static function fromBuildingAndUsername(Uuid $building, string $username) : self
{
return new self($building, $username);
}

public function building() : Uuid
{
return $this->building;
}

public function username() : string
{
return $this->username;
}

/** {@inheritDoc} */
public function payload() : array
{
return [
'building' => $this->building->toString(),
'username' => $this->username,
];
}

/** {@inheritDoc} */
protected function setPayload(array $payload)
{
$this->building = Uuid::fromString($payload['building']);
$this->username = $payload['username'];
}
}
21 changes: 21 additions & 0 deletions src/Domain/DomainEvent/UserCheckedIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Building\Domain\DomainEvent;

use Prooph\EventSourcing\AggregateChanged;
use Rhumsaa\Uuid\Uuid;

final class UserCheckedIn extends AggregateChanged
{
public static function toBuilding(Uuid $building, string $username) : self
{
return self::occur($building->toString(), ['username' => $username]);
}

public function username() : string
{
return $this->payload['username'];
}
}