Skip to content

Commit

Permalink
add eventstore, globalfunction,
Browse files Browse the repository at this point in the history
  • Loading branch information
albert committed Apr 15, 2019
1 parent 93f9355 commit 57c4c06
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public function it_should_return_entity_type()
{
$this->entityType()->shouldReturn($this->domainEntityType());
}

public function it_should_return_data()
{
$this->data()->shouldReturn($this->arrayData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ protected function domainEntityType(): string
{
return "Basket";
}

protected function arrayData(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ protected function domainEntityType(): string
{
return "Basket";
}

protected function arrayData(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: albert
* Date: 2019-04-15
* Time: 10:27
*/

namespace AlbertDonCelis\DDD\Infrastructure\EventStore {

function date($format) {
return $format;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace spec\AlbertDonCelis\DDD\Infrastructure\EventStore;

require_once 'GlobalFunction.php';

use AlbertDonCelis\DDD\Domain\DomainEventInterface;
use AlbertDonCelis\DDD\Example\Domain\BasketEventSourced;
use AlbertDonCelis\DDD\Example\Domain\ValueObject\BasketId;
use AlbertDonCelis\DDD\Infrastructure\EventStore\EventStoreInterface;
use AlbertDonCelis\DDD\Infrastructure\EventStore\PDOEventStore;
use Faker\Factory;
use Faker\Generator;
use PhpSpec\ObjectBehavior;
/**
* Class PDOEventStoreSpec
* @package spec\AlbertDonCelis\DDD\Infrastructure\EventStore
*
* @mixin PDOEventStore
*/
class PDOEventStoreSpec extends ObjectBehavior
{
/** @var \PDO $pdo */
private $pdo;

/** @var Generator $faker */
private $faker;

function it_is_initializable()
{
$this->shouldHaveType(PDOEventStore::class);
$this->shouldHaveType(EventStoreInterface::class);
}

public function let(\PDO $pdo)
{
$this->pdo = $pdo;
$this->faker = Factory::create();
$this->beConstructedWith($pdo);
}

private function insertDataString(): string
{
return <<<EOF
INSERT INTO events (aggregate_id, `event_name`, `entity_type`, created_at, `data`) VALUES (:aggregate_id, :event_name, :entity_type, :created_at, :data)
EOF;

}

public function it_should_insert_data_of_a_domain_events(\PDOStatement $statement)
{
$this->pdo->prepare($this->insertDataString())->shouldBeCalledTimes(1)->willReturn($statement);

$basketId = new BasketId($this->faker->uuid);

$basketEventSourced = BasketEventSourced::pickUp($basketId);

$recordEvents = $basketEventSourced->getRecordedEvents();

/** @var DomainEventInterface $event */
foreach($recordEvents as $event) {

$statement->execute([
':aggregate_id' => (string) $basketId,
':event_name' => $event->eventName(),
':entity_type' => $event->entityType(),
':created_at' => date('Y-m-d H:i:s'),
':data' => json_encode($event->data())

])->shouldBeCalledTimes(count($recordEvents));

}

$this->commit($recordEvents);

}
}

function date($format) {
return $format;
}
2 changes: 2 additions & 0 deletions src/AlbertDonCelis/DDD/Domain/DomainEventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ interface DomainEventInterface extends DomainEvent
public function eventName(): string;

public function entityType(): string;

public function data(): array;
}
5 changes: 5 additions & 0 deletions src/AlbertDonCelis/DDD/Example/Domain/BasketWasPickedUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public function entityType(): string
{
return "Basket";
}

public function data(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public function productId(): IdentifiesAggregate
{
return $this->productId;
}

public function data(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Created by PhpStorm.
* User: albert
* Date: 2019-04-15
* Time: 09:33
*/

namespace AlbertDonCelis\DDD\Infrastructure\EventStore;

use AlbertDonCelis\DDD\Domain\History;
use Buttercup\Protects\DomainEvents;
use Buttercup\Protects\IdentifiesAggregate;

interface EventStoreInterface
{
public function commit(DomainEvents $domainEvents): void;
}
41 changes: 41 additions & 0 deletions src/AlbertDonCelis/DDD/Infrastructure/EventStore/PDOEventStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace AlbertDonCelis\DDD\Infrastructure\EventStore;

use AlbertDonCelis\DDD\Domain\DomainEventInterface;
use AlbertDonCelis\DDD\Domain\History;
use Buttercup\Protects\DomainEvents;
use Buttercup\Protects\IdentifiesAggregate;

class PDOEventStore implements EventStoreInterface
{
/** @var \PDO $pdo */
private $pdo;

public function __construct(\PDO $pdo)
{
$this->pdo = $pdo;
}

private function eventStoreInsertString(): string
{
return <<<EOF
INSERT INTO events (aggregate_id, `event_name`, `entity_type`, created_at, `data`) VALUES (:aggregate_id, :event_name, :entity_type, :created_at, :data)
EOF;
}
public function commit(DomainEvents $domainEvents): void
{
$stmt = $this->pdo->prepare($this->eventStoreInsertString());

/** @var DomainEventInterface $event */
foreach ($domainEvents as $event) {
$stmt->execute([
':aggregate_id' => (string) $event->getAggregateId(),
':event_name' => $event->eventName(),
':entity_type' => $event->entityType(),
':created_at' => date('Y-m-d H:i:s'),
':data' => json_encode($event->data())
]);
}
}
}

0 comments on commit 57c4c06

Please sign in to comment.