Skip to content

Commit

Permalink
Change interface for FilterableProvider and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkan committed Nov 23, 2021
1 parent c56bbfe commit 7124909
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ $provider = new ContainerListenerProvider($container);
$provider->addService(Event::class, EventListener::class);
```

### `PriorityAggregateProviderTest`
### `PriorityAggregateProvider`

```php
use Circli\EventDispatcher\ListenerProvider\PriorityAggregateProvider;
Expand All @@ -94,6 +94,18 @@ $aggregateProvider->addProviderWithPriority(new DefaultProvider(), 500);

```

### `FilterableProvider`

```php
use Circli\EventDispatcher\ListenerProvider\FilterableProvider;

$provider = new FilterableProvider();

$provider->listen(RandomEvent::class, $listener, function ($event) {
return ifRandomExternalThingIsTrue();
});
```


## `LazyListenerFactory`

Expand Down
5 changes: 3 additions & 2 deletions src/ListenerProvider/FilterableProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use Psr\EventDispatcher\ListenerProviderInterface;

final class FilterableProvider implements ListenerProviderInterface
final class FilterableProvider implements ListenerInterface
{
/** @var array<string, array<array-key, array{callable, callable}>> */
private $listeners = [];

public function listen(string $eventType, callable $listener, callable $filter): void
public function listen(string $eventType, callable $listener, callable $filter = null): void
{
$filter = $filter ?? static function () {return true;};
if (!isset($this->listeners[$eventType])) {
$this->listeners[$eventType] = [];
}
Expand Down
13 changes: 13 additions & 0 deletions tests/ListenerProvider/FilterableProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function testAllFilterReturnFalse(): void
});
$this->assertCount(0, $listener->getListenersForEvent(new TestFilterEvent(2)));
}

public function testNoListenerFound(): void
{
$listener = new FilterableProvider();
Expand All @@ -38,6 +39,7 @@ public function testNoListenerFound(): void
});
$this->assertCount(0, $listener->getListenersForEvent(new TestFilterEvent(2)));
}

public function testAddDuplicateCallback(): void
{
$listener = new FilterableProvider();
Expand All @@ -48,4 +50,15 @@ public function testAddDuplicateCallback(): void

$this->assertCount(1, $listener->getListenersForEvent(new TestEvent()));
}

public function testDefaultFilter(): void
{
$listener = new FilterableProvider();
$listener->listen(TestFilterEvent::class, function () {}, function (TestFilterEvent $e) {
return $e->getFilterValue() === 2;
});
$listener->listen(TestFilterEvent::class, function () {});

$this->assertCount(2, $listener->getListenersForEvent(new TestFilterEvent(2)));
}
}

0 comments on commit 7124909

Please sign in to comment.