Skip to content

Commit

Permalink
Merge pull request #9 from barryosull/feature/extract_snapshot_genera…
Browse files Browse the repository at this point in the history
…tion_into_its_own_class

Feature/extract snapshot generation into its own class
  • Loading branch information
Barry O Sullivan committed Jul 14, 2017
2 parents e72b762 + 8f530a8 commit dd2815d
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 46 deletions.
30 changes: 15 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Contracts/Event/Event.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace BoundedContext\Contracts\Event;

use BoundedContext\Contracts\Core\Identifiable;
use EventSourced\ValueObject\Contracts\ValueObject\Identifier;
use EventSourced\ValueObject\Contracts\ValueObject;

/**
Expand Down
9 changes: 0 additions & 9 deletions src/Contracts/Event/Snapshot/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,4 @@ interface Factory
*/

public function event(Event $event);

/**
* Returns a new Snapshot from a Schema.
*
* @param Schema $schema
* @return Snapshot $snapshot
*/

public function schema(Schema $schema);
}
12 changes: 12 additions & 0 deletions src/Contracts/Event/Snapshot/Transformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php namespace BoundedContext\Contracts\Event\Snapshot;

use BoundedContext\Contracts\Event\Event;

interface Transformer
{
public function fromEvent(Event $event);

public function toPopo($snapshot);

public function fromPopo($popo);
}
82 changes: 82 additions & 0 deletions src/Event/Snapshot/Transformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php namespace BoundedContext\Event\Snapshot;

use BoundedContext\Contracts\Event\Event;
use BoundedContext\Contracts\Generator\DateTime as DateTimeGenerator;
use BoundedContext\Contracts\Generator\Identifier;
use BoundedContext\Contracts\Version\Factory as EventVersionFactory;
use BoundedContext\Event\Type as EventType;
use BoundedContext\Event\AggregateType;
use BoundedContext\Schema\Schema as ConcreteSchema;
use EventSourced\ValueObject\Serializer\Serializer;
use EventSourced\ValueObject\ValueObject\Integer as Integer_;
use EventSourced\ValueObject\ValueObject\Uuid;
use EventSourced\ValueObject\ValueObject\DateTime;

class Transformer implements \BoundedContext\Contracts\Event\Snapshot\Transformer
{
protected $identifier_generator;
protected $datetime_generator;
protected $event_version_factory;
protected $map;
protected $serializer;

public function __construct(
Identifier $identifier_generator,
DateTimeGenerator $datetime_generator,
EventVersionFactory $event_version_factory,
Serializer $serializer
)
{
$this->identifier_generator = $identifier_generator;
$this->datetime_generator = $datetime_generator;
$this->event_version_factory = $event_version_factory;
$this->serializer = $serializer;
}

public function fromEvent(Event $event)
{
$serialized = $this->serializer->serialize($event->values());
$domain_event = $event->values();
$event_type = EventType::from_event($event->values());
$aggregate_type = $event->aggregate_type();

return new Snapshot(
$event->id(),
$this->event_version_factory->event($domain_event),
$this->datetime_generator->now(),
$event_type,
$event->command_id(),
$event->aggregate_id(),
$aggregate_type,
new ConcreteSchema($serialized)
);
}

public function toPopo($snapshot)
{
return (object)[
'id' => $snapshot->id()->value(),
'type' => $snapshot->type()->value(),
'aggregate_id' => $snapshot->aggregate_id()->value(),
'aggregate_type' => $snapshot->aggregate_type()->value(),
'command_id' => $snapshot->command_id()->value(),
'version' => $snapshot->version()->value(),
'occurred_at' => $snapshot->occurred_at()->value(),
'event' => $snapshot->schema()->data_tree()
];
}

public function fromPopo($popo)
{
return new Snapshot(
new Uuid($popo->id),
new Integer_($popo->version),
new DateTime($popo->occurred_at),
new EventType($popo->type),
new Uuid($popo->command_id),
new Uuid($popo->aggregate_id),
new AggregateType($popo->aggregate_type),
new ConcreteSchema((array)$popo->event)
);
}
}
2 changes: 1 addition & 1 deletion src/Player/AbstractPlayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function play($limit = 1000)
->after($this->snapshot()->last_id())
->limit(new Integer($limit))
->stream();

foreach($snapshot_stream as $snapshot) {
$this->apply($snapshot);
}
Expand Down
23 changes: 5 additions & 18 deletions src/Sourced/Stream/SnapshotStream.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
<?php namespace BoundedContext\Sourced\Stream;

use BoundedContext\Contracts\Sourced\Stream\Stream;
use BoundedContext\Schema\Schema;
use BoundedContext\Event\AggregateType;
use BoundedContext\Event\Snapshot\Snapshot;
use EventSourced\ValueObject\ValueObject\Uuid;
use EventSourced\ValueObject\ValueObject\Integer as Integer_;
use EventSourced\ValueObject\ValueObject\DateTime;
use BoundedContext\Event;
use BoundedContext\Contracts\Event\Snapshot;

class SnapshotStream implements Stream
{
private $snapshot_transformer;
private $stream;

public function __construct(Stream $stream)
public function __construct(Snapshot\Transformer $snapshot_transformer, Stream $stream)
{
$this->snapshot_transformer = $snapshot_transformer;
$this->stream = $stream;
}

Expand All @@ -31,16 +27,7 @@ public function current()

private function popo_to_event_snapshot($popo)
{
return new Snapshot(
new Uuid($popo->id),
new Integer_($popo->version),
new DateTime($popo->occurred_at),
new Event\Type($popo->type),
new Uuid($popo->command_id),
new Uuid($popo->aggregate_id),
new AggregateType($popo->aggregate_type),
new Schema((array)$popo->event)
);
return $this->snapshot_transformer->fromPopo($popo);
}

public function next()
Expand Down
41 changes: 38 additions & 3 deletions tests/SnapshotStreamTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

use BoundedContext\Contracts\Event\Snapshot\Transformer;
use BoundedContext\Contracts\Sourced\Stream\Stream;
use BoundedContext\Event;
use BoundedContext\Event\AggregateType;
use BoundedContext\Event\Snapshot\Snapshot;
use BoundedContext\Schema\Schema;
use BoundedContext\Sourced\Stream\SnapshotStream;
use EventSourced\ValueObject\ValueObject\Uuid;
use EventSourced\ValueObject\ValueObject\Integer as Integer_;
use EventSourced\ValueObject\ValueObject\DateTime;
use BoundedContext\Event;

class SnapshotStreamTest extends PHPUnit_Framework_TestCase
{
Expand All @@ -17,7 +18,7 @@ class SnapshotStreamTest extends PHPUnit_Framework_TestCase
*/
public function test_conversion($popo, $snapshot)
{
$stream = new SnapshotStream( $this->fakeStream($popo) );
$stream = new SnapshotStream($this->fakeTransformer(), $this->fakeStream($popo) );

$this->assertEquals($snapshot, $stream->current());
}
Expand All @@ -26,7 +27,7 @@ public function test_can_foreach()
{
$provider = $this->eventProvider();
$popo = reset($provider)[0];
$stream = new SnapshotStream( $this->fakeStream($popo) );
$stream = new SnapshotStream($this->fakeTransformer(), $this->fakeStream($popo) );

$count = 0;
foreach ($stream as $snapshot) {
Expand Down Expand Up @@ -65,6 +66,40 @@ public function eventProvider()
];
}

private function fakeTransformer()
{
$provider = $this->eventProvider();

$popo = reset($provider)[0];
$snapshot = reset($provider)[1];

return new Class($popo, $snapshot) implements Transformer
{
private $popo;
private $snapshot;

public function __construct($popo, $snapshot)
{
$this->popo = $popo;
$this->snapshot = $snapshot;
}

public function fromEvent(\BoundedContext\Contracts\Event\Event $event){}

public function fromSchema(\BoundedContext\Contracts\Schema\Schema $schema){}

public function toPopo($snapshot){}

public function fromPopo($popo)
{
if ($popo == $this->popo) {
return $this->snapshot;
}
return null;
}
};
}

private function fakeStream($popo)
{
return new Class($popo) implements Stream
Expand Down

0 comments on commit dd2815d

Please sign in to comment.