Skip to content

Commit

Permalink
add projection
Browse files Browse the repository at this point in the history
  • Loading branch information
albert committed Apr 11, 2019
1 parent db97ed0 commit 9ceb33f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "library",
"description": "",
"require": {
"php": ">= 7.2",
"php": ">= 7.1",
"ramsey/uuid": "^3.8"
},
"require-dev": {
Expand Down
28 changes: 13 additions & 15 deletions composer.lock

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

66 changes: 66 additions & 0 deletions spec/AlbertDonCelis/DDD/Infrastructure/ProjectorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace spec\AlbertDonCelis\DDD\Infrastructure;

use AlbertDonCelis\DDD\Domain\EventInterface;
use AlbertDonCelis\DDD\Infrastructure\Projection\ProjectionInterface;
use AlbertDonCelis\DDD\Infrastructure\Projector;
use Faker\Factory;
use PhpSpec\ObjectBehavior;
use PHPUnit\Framework\Assert;
use Prophecy\Argument;

/**
* Class ProjectorSpec
* @package spec\AlbertDonCelis\DDD\Infrastructure
*
* @mixin Projector
*/
class ProjectorSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(Projector::class);
}

/**
* @param ProjectionInterface $projection
*/
public function it_should_register_projection(ProjectionInterface $projection)
{
$projections = [];
$randomCount = rand(1, 10);

for ($count = 0; $randomCount > $count ; $count++) {

$listenTo = Factory::create()->userName();
$projection->listenToEventNameOf()->shouldBeCalledTimes($randomCount)->willReturn($listenTo);
$projections[$listenTo][] = $projection;
$this->register($projection)->shouldReturn($this);
}

$registerProjections = $this->registeredProjections();
$registerProjections->shouldReturn($projections);

}

public function it_should_subscribe(EventInterface $event, ProjectionInterface $projection) {

$projections = [];
$randomCount = rand(1, 10);
$listenToEvents = [];

for ($count = 0; $randomCount > $count ; $count++) {
$listenTo = Factory::create()->userName();
array_push($listenToEvents, $listenTo);
$projection->listenToEventNameOf()->shouldBeCalledTimes($randomCount)->willReturn($listenTo);
$projections[$listenTo][] = $projection;
$this->register($projection);
}
$event->eventName()->shouldBeCalledTimes(1)->willReturn($listenToEvents[$randomCount - 1]);
$projection->project($event)->shouldBeCalledTimes(1);
$this->subscribeTo($event);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Created by PhpStorm.
* User: albert
* Date: 2019-04-11
* Time: 08:04
*/

namespace AlbertDonCelis\DDD\Infrastructure\Projection;

use AlbertDonCelis\DDD\Domain\EventInterface;

interface ProjectionInterface
{

public function listenToEventNameOf(): string;

public function project(EventInterface $event): void;
}
41 changes: 41 additions & 0 deletions src/AlbertDonCelis/DDD/Infrastructure/Projector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace AlbertDonCelis\DDD\Infrastructure;

use AlbertDonCelis\DDD\Domain\EventInterface;
use AlbertDonCelis\DDD\Infrastructure\Projection\ProjectionInterface;

class Projector
{
private $projections = [];

public function register(ProjectionInterface $projection): self
{
$this->projections[$projection->listenToEventNameOf()][] = $projection;

return $this;
}

public function registeredProjections(): array
{
return $this->projections;
}

private function hasSubscriber(string $listenToEventNameOf): bool
{
return array_key_exists($listenToEventNameOf, $this->projections);
}

public function subscribeTo(EventInterface $event)
{
$eventListenTo = $event->eventName();

if ($this->hasSubscriber($eventListenTo)) {

/** @var ProjectionInterface $projection */
foreach ($this->projections[$eventListenTo] as $projection) {
$projection->project($event);
}
}
}
}

0 comments on commit 9ceb33f

Please sign in to comment.