-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
albert
committed
Apr 11, 2019
1 parent
db97ed0
commit 9ceb33f
Showing
5 changed files
with
140 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/AlbertDonCelis/DDD/Infrastructure/Projection/ProjectionInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |