Skip to content

Commit

Permalink
Merge pull request #7 from barryosull/feature/player_versioning
Browse files Browse the repository at this point in the history
Added player versioning.
  • Loading branch information
Barry O Sullivan committed Jun 20, 2017
2 parents 31fdd1c + 869599e commit 224d042
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/Contracts/Player/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ interface Factory
*/
public function snapshot(Snapshot $snapshot);

/**
* Returns a Player by a Classname.
*
* @return Player $player
*/
public function make($class_name);
}
5 changes: 5 additions & 0 deletions src/Contracts/Player/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ interface Player extends Resetable, Playable
*/

public function snapshot();

/**
* @return int
*/
public function version();
}
2 changes: 2 additions & 0 deletions src/Contracts/Player/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public function get(ClassName $class_name);
* @return void
*/
public function save(Player $player);

public function hasVersionChanged(ClassName $class_name);
}
10 changes: 8 additions & 2 deletions src/Contracts/Player/Snapshot/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use EventSourced\ValueObject\Contracts\ValueObject\Identifier;
use BoundedContext\Contracts\Generator\DateTime as DateTimeGenerator;
use BoundedContext\Contracts\Generator\Identifier as IdentifierGenerator;
use EventSourced\ValueObject\ValueObject\Integer as Integer_;

interface Snapshot extends \BoundedContext\Contracts\Snapshot\Snapshot
{
Expand All @@ -20,6 +21,11 @@ public function class_name();

public function last_id();

/**
* @return Integer_
*/
public function updateCount();

/**
* Returns a new Snapshot after resetting it back to its default state.
*
Expand All @@ -28,10 +34,10 @@ public function last_id();
*
* @return Snapshot
*/

public function reset(
IdentifierGenerator $identifier_generator,
DateTimeGenerator $datetime_generator
DateTimeGenerator $datetime_generator,
Integer_ $version
);

/**
Expand Down
27 changes: 26 additions & 1 deletion src/Player/AbstractPlayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ public function reset()
{
$this->snapshot = $this->snapshot->reset(
$this->identifier_generator,
$this->datetime_generator
$this->datetime_generator,
new Integer($this->version())
);
}

public function play($limit = 1000)
{
if ($this->snapshot()->version()->value() != $this->version()) {
throw new \Exception("The snapshot and projector have different versions, playing would cause potential data corruption. This exception only occurs if the players are used incorrectly.");
}

$snapshot_stream = $this->log
->builder()
->after($this->snapshot()->last_id())
Expand Down Expand Up @@ -74,4 +79,24 @@ public function snapshot()
{
return $this->snapshot;
}

/**
* @return int
*/
public function version()
{
$player_class = get_called_class();

$path = explode("\\", $player_class);

$path[count($path)-1] = "Version";

$version_class = implode("\\", $path);

if (!class_exists($version_class)) {
return 1;
}

return $version_class::VALUE;
}
}
10 changes: 10 additions & 0 deletions src/Player/Collection/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ public function snapshot()
{
throw new \Exception("Collection Player Snapshots are not supported.");
}

public function version()
{
throw new \Exception("Collection Player versions are not supported.");
}

public function count()
{
return $this->player_classes->count()->value();
}
}
15 changes: 15 additions & 0 deletions src/Player/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use BoundedContext\Contracts\Player\Snapshot\Repository as SnapshotRepository;
use BoundedContext\Contracts\Player\Factory as PlayerFactory;
use BoundedContext\Player\Snapshot\ClassName;
use EventSourced\ValueObject\ValueObject\Integer as Integer_;

class Repository implements \BoundedContext\Contracts\Player\Repository
{
Expand Down Expand Up @@ -35,4 +36,18 @@ public function save(Player $player)
{
$this->snapshot_repository->save($player->snapshot());
}

public function hasVersionChanged(ClassName $class_name)
{
$snapshot = $this->snapshot_repository->get($class_name);

$active_version = new Integer_(1);
if ($snapshot) {
$active_version = $snapshot->version();
}

$player = $this->player_factory->make($class_name);

return $active_version->value() != $player->version();
}
}
1 change: 1 addition & 0 deletions src/Player/Snapshot/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function make(array $snapshot)
return new Snapshot(
new ClassName($snapshot['class_name']),
new Integer($snapshot['version']),
new Integer($snapshot['update_count']),
$this->datetime_generator->string($snapshot['occurred_at']),
$this->identifier_generator->string($snapshot['last_id'])
);
Expand Down
27 changes: 20 additions & 7 deletions src/Player/Snapshot/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@
use BoundedContext\Contracts\Generator\DateTime as DateTimeGenerator;
use BoundedContext\Contracts\Generator\Identifier as IdentifierGenerator;
use BoundedContext\Snapshot\AbstractSnapshot;
use EventSourced\ValueObject\ValueObject\Integer as Version;
use EventSourced\ValueObject\ValueObject\Integer as Integer_;

class Snapshot extends AbstractSnapshot implements \BoundedContext\Contracts\Player\Snapshot\Snapshot
{
public $last_id;
private $class_name;
protected $class_name;
protected $update_count;

public function __construct(
ClassName $class_name,
Version $version,
Integer_ $version,
Integer_ $update_count,
DateTime $occurred_at,
Identifier $last_id
)
{
parent::__construct($version, $occurred_at);
$this->class_name = $class_name;
$this->update_count = $update_count;
$this->last_id = $last_id;
}

Expand All @@ -31,12 +34,14 @@ public function last_id()

public function reset(
IdentifierGenerator $identifier_generator,
DateTimeGenerator $datetime_generator
DateTimeGenerator $datetime_generator,
Integer_ $version
)
{
return new Snapshot(
$this->class_name,
$this->version->reset(),
$version,
$this->update_count->reset(),
$datetime_generator->now(),
$identifier_generator->null()
);
Expand All @@ -50,6 +55,7 @@ public function skip(
return new Snapshot(
$this->class_name,
$this->version,
$this->update_count,
$datetime_generator->now(),
$next_id
);
Expand All @@ -62,7 +68,8 @@ public function take(
{
return new Snapshot(
$this->class_name,
$this->version->increment(),
$this->version,
$this->update_count->increment(),
$datetime_generator->now(),
$next_id
);
Expand All @@ -81,9 +88,15 @@ public static function make(
{
return new Snapshot(
$class_name,
new Version(1),
new Integer_(1),
new Integer_(1),
$datetime_generator->now(),
$identifier_generator->null()
);
}

public function updateCount()
{
return $this->update_count;
}
}
4 changes: 2 additions & 2 deletions src/Snapshot/AbstractSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

use EventSourced\ValueObject\Contracts\ValueObject\DateTime;
use EventSourced\ValueObject\ValueObject\Type\AbstractComposite;
use EventSourced\ValueObject\ValueObject\Integer as Version;
use EventSourced\ValueObject\ValueObject\Integer as Integer_;

abstract class AbstractSnapshot extends AbstractComposite
{
protected $version;
protected $occurred_at;

public function __construct(
Version $version,
Integer_ $version,
DateTime $occurred_at
)
{
Expand Down

0 comments on commit 224d042

Please sign in to comment.