Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
nixilla committed Feb 11, 2020
2 parents 147d039 + 490d9d8 commit 06c6139
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changeset

<!-- 0.2.0 -->

[![Version](https://img.shields.io/packagist/v/chgst/chgst.svg?style=flat-square)](https://packagist.org/packages/chgst/chgst)
[![Build Status](https://travis-ci.org/chgst/chgst.svg?branch=develop)](https://travis-ci.org/chgst/chgst)
[![Coverage Status](https://coveralls.io/repos/github/chgst/chgst/badge.svg?branch=develop)](https://coveralls.io/github/chgst/chgst?branch=develop)
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"license": "MIT",
"require": {
"chgst/common":"^0.1"
"chgst/common":"^0.2"
},
"autoload": {
"psr-4": {
Expand All @@ -17,9 +17,9 @@
}
},
"require-dev": {
"phpspec/phpspec": "^4.0",
"leanphp/phpspec-code-coverage": "^4.2",
"php-coveralls/php-coveralls": "^2.1"
"phpspec/phpspec": "^6",
"friends-of-phpspec/phpspec-code-coverage": "^4.3",
"php-coveralls/php-coveralls": "^2.2"
},
"config": {
"bin-dir": "bin",
Expand Down
15 changes: 14 additions & 1 deletion spec/Changeset/Communication/InMemoryEventBusSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,26 @@ function it_you_can_toggle_execution_of_listeners()
$this->listenersEnabled()->shouldReturn(false);
}

function it_can_dispatch_event_to_projectors_and_listeners(EventInterface $event, ProjectorInterface $projector, ListenerInterface $listener)
function it_can_dispatch_event_to_projectors_and_listeners(
EventInterface $event,
ProjectorInterface $projector,
ProjectorInterface $projector2,
ProjectorInterface $projector3,
ListenerInterface $listener
)
{
$this->addProjector($projector2, -32);
$this->addProjector($projector);
$this->addProjector($projector3, 45);
$this->addListener($listener);

$projector->supports(Argument::any())->willReturn(true);
$projector2->supports(Argument::any())->willReturn(true);
$projector3->supports(Argument::any())->willReturn(true);

$projector->process($event)->shouldBeCalled();
$projector2->process($event)->shouldBeCalled();
$projector3->process($event)->shouldBeCalled();

$this->dispatch($event);

Expand Down
48 changes: 34 additions & 14 deletions src/Changeset/Communication/InMemoryEventBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

class InMemoryEventBus implements EventBusInterface
{
/** @var ProjectorInterface[] */
/** @var array */
private $projectors = [];

/** @var ListenerInterface[] */
/** @var array */
private $listeners = [];

/** @var bool */
Expand All @@ -32,39 +32,59 @@ public function listenersEnabled() : bool
return $this->listenersEnabled;
}

public function addListener(ListenerInterface $listener)
public function addListener(ListenerInterface $listener, $priority = 0)
{
if ( ! in_array($listener, $this->listeners))
if ( ! isset($this->listeners[$priority]) || ! is_array($this->listeners[$priority]))
{
$this->listeners[] = $listener;
$this->listeners[$priority] = [];
}

if ( ! in_array($listener, $this->listeners[$priority]))
{
$this->listeners[$priority][] = $listener;
}
}

public function addProjector(ProjectorInterface $projector)
public function addProjector(ProjectorInterface $projector, $priority = 0)
{
if ( ! in_array($projector, $this->projectors))
if ( ! isset($this->projectors[$priority]) || ! is_array($this->projectors[$priority]))
{
$this->projectors[$priority] = [];
}

if ( ! in_array($projector, $this->projectors[$priority]))
{
$this->projectors[] = $projector;
$this->projectors[$priority][] = $projector;
}
}

public function dispatch(EventInterface $event)
{
foreach ($this->projectors as $projector)
krsort($this->projectors);

foreach ($this->projectors as $priority => $projectors)
{
if($projector->supports($event))
foreach ($projectors as $projector)
{
$projector->process($event);
if ($projector->supports($event))
{
$projector->process($event);
}
}
}

if($this->listenersEnabled)
{
foreach ($this->listeners as $listener)
krsort($this->listeners);

foreach ($this->listeners as $priority => $listeners)
{
if($listener->supports($event))
foreach ($listeners as $listener)
{
$listener->process($event);
if($listener->supports($event))
{
$listener->process($event);
}
}
}
}
Expand Down

0 comments on commit 06c6139

Please sign in to comment.