Skip to content

Commit

Permalink
Merge pull request #18 from digitalrisks/feature/AddsUserMetaData
Browse files Browse the repository at this point in the history
fixes #13 and fixes #16
  • Loading branch information
Kani Robinson authored Nov 4, 2019
2 parents 725b153 + 2d27de5 commit 5c70f4c
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 42 deletions.
90 changes: 72 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,74 @@ You can install the package via composer:
composer require digitalrisks/laravel-eventstore
```

Add the base service provider for the package.

``` php
<?php

namespace App\Providers;

use Illuminate\Support\Str;
use DigitalRisks\LaravelEventStore\EventStore;
use DigitalRisks\LaravelEventStore\ServiceProvider as EventStoreApplicationServiceProvider;

class EventStoreServiceProvider extends EventStoreApplicationServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
parent::boot();
}

/**
* Set the eventToClass method.
*
* @return void
*/
public function eventClasses()
{
// This will set your events to be the following '\\App\Events\\' . $event->getType();.
EventStore::eventToClass();

// You can customise this by doing the following.
EventStore::eventToClass(function ($event) {
return 'App\Events\\' . Str::studly($event->getType());
});
}

/**
* Register the application services.
*/
public function register()
{
parent::register();
}
}

```

In your `config/app.php` file, add the following to the `providers` array.

``` php
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/

'providers' => [
...
App\Providers\EventStoreServiceProvider::class,
],
```

## Example Event

``` php
Expand Down Expand Up @@ -92,6 +160,7 @@ Metadata can help trace events around your system. You can include any of the fo

* `AddsHerokuMetadata`
* `AddsLaravelMetadata`
* `AddsUserMetaData`

Or you can define your own methods to collect metadata. Any method with the `@metadata` annotation will be called:

Expand Down Expand Up @@ -225,23 +294,6 @@ return [
'group' => 'account-email-subscription',
'volatile_streams' => ['quotes', 'accounts'],
'subscription_streams' => ['quotes', 'accounts'],
'event_to_class' => function ($event) {
return 'App\Events\\' . $event->getType();
}
];
```

### Mapping events to Laravel Event Classes

By default the event type will be used to create and fire a Laravel Event class. If your event types are named differently to your Laravel Event
Class names, you may specify a `event_to_class` callback in your configuration to return the appropriate class name.

``` php
return [
'event_to_class' => function ($event) {
// map account_created to AccountCreated
return Str::studly($event->getType());
}
];
```

Expand All @@ -265,7 +317,9 @@ If you discover any security related issues, please email pawel.trauth@digitalri

## Credits

- [Pawel Trauth](https://github.com/digitalrisks)
- [Pawel Trauth](https://github.com/eithed)
- [Craig Morris](https://github.com/morrislaptop)
- [Kani Robinson](https://github.com/kanirobinson)
- [All Contributors](../../contributors)

## License
Expand Down
3 changes: 0 additions & 3 deletions config/eventstore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@
'subscription_streams' => array_filter(explode(',', env('EVENTSTORE_SUBSCRIPTION_STREAMS'))),
'volatile_streams' => array_filter(explode(',', env('EVENTSTORE_VOLATILE_STREAMS'))),
'group' => env('EVENTSTORE_SUBSCRIPTION_GROUP', env('APP_NAME', 'laravel')),
'event_to_class' => function ($event) {
return 'App\Events\\' . $event->getType();
}
];
23 changes: 12 additions & 11 deletions src/Console/Commands/EventStoreWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace DigitalRisks\LaravelEventStore\Console\Commands;

use DigitalRisks\LaravelEventStore\Contracts\CouldBeReceived;
use Illuminate\Console\Command;

use Carbon\Carbon;
use DigitalRisks\LaravelEventStore\EventStore as LaravelEventStore;
use EventLoop\EventLoop;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use ReflectionClass;
use ReflectionProperty;
use Rxnet\EventStore\EventStore;
use Rxnet\EventStore\Data\EventRecord as EventData;
use Rxnet\EventStore\EventStore;
use Rxnet\EventStore\Record\AcknowledgeableEventRecord;
use Rxnet\EventStore\Record\EventRecord;
use Rxnet\EventStore\Record\JsonEventRecord;
Expand Down Expand Up @@ -135,8 +134,7 @@ protected function safeGetMetadata(EventRecord $event)
{
try {
return $event->getMetadata();
}
catch (TypeError $e) {
} catch (TypeError $e) {
return [];
}
}
Expand All @@ -147,8 +145,7 @@ public function dispatch(EventRecord $eventRecord): void

if ($localEvent = $this->mapToLocalEvent($event)) {
event($localEvent);
}
else {
} else {
event($event->getType(), $event);
}
}
Expand All @@ -167,14 +164,18 @@ private function makeSerializableEvent(EventRecord $event): JsonEventRecord

protected function mapToLocalEvent($event)
{
$eventToClass = config('eventstore.event_to_class');
$eventToClass = LaravelEventStore::$eventToClass;
$className = $eventToClass ? $eventToClass($event) : 'App\Events\\' . $event->getType();

if (! class_exists($className)) return;
if (!class_exists($className)) {
return;
}

$reflection = new ReflectionClass($className);

if (! $reflection->implementsInterface(CouldBeReceived::class)) return;
if (!$reflection->implementsInterface(CouldBeReceived::class)) {
return;
}

$localEvent = new $className();
$props = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
Expand Down
28 changes: 28 additions & 0 deletions src/EventStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace DigitalRisks\LaravelEventStore;

class EventStore
{
/**
* Variable for event to class.
*
* @var callable
*/
public static $eventToClass;

/**
* Set the event class based on current event key.
*
* @param callable|null $callback
* @return void
*/
public static function eventToClass(?callable $callback = null)
{
$callback = $callback ?: function ($event) {
return 'App\Events\\' . $event->getType();
};

static::$eventToClass = $callback;
}
}
22 changes: 19 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use DigitalRisks\LaravelEventStore\Console\Commands\EventStoreReset;
use DigitalRisks\LaravelEventStore\Console\Commands\EventStoreWorker;
use DigitalRisks\LaravelEventStore\Contracts\ShouldBeStored;
use DigitalRisks\LaravelEventStore\EventStore;
use DigitalRisks\LaravelEventStore\Listeners\SendToEventStoreListener;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;

class ServiceProvider extends LaravelServiceProvider
{
Expand All @@ -16,9 +17,10 @@ class ServiceProvider extends LaravelServiceProvider
*/
public function boot()
{
$this->eventClasses();
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/eventstore.php' => config_path('eventstore.php'),
__DIR__ . '/../config/eventstore.php' => config_path('eventstore.php'),
], 'config');

$this->commands([
Expand All @@ -30,12 +32,26 @@ public function boot()
Event::listen(ShouldBeStored::class, SendToEventStoreListener::class);
}

/**
* Set the eventToClass method.
*
* @return void
*/
public function eventClasses()
{
EventStore::eventToClass();
}

/**
* Register the application services.
*/
public function register()
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/eventstore.php', 'eventstore');
$this->mergeConfigFrom(__DIR__ . '/../config/eventstore.php', 'eventstore');

$this->app->singleton(EventStore::class, function () {
return new EventStore;
});
}
}
22 changes: 22 additions & 0 deletions src/Traits/AddsUserMetaData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace DigitalRisks\LaravelEventStore\Traits;

trait AddsUserMetaData
{
/**
* @metadata
*/
public function collectUserMetaData()
{
$user = request()->user();

if (!$user) {
return [];
}

return [
'user' => $user->toArray()
];
}
}
5 changes: 4 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace DigitalRisks\LaravelEventStore\Tests;

use Orchestra\Testbench\TestCase as Orchestra;
use DigitalRisks\LaravelEventStore\EventStore;
use DigitalRisks\LaravelEventStore\ServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
protected function getPackageProviders($app)
{
EventStore::eventToClass();

return [
ServiceProvider::class,
];
Expand Down
12 changes: 6 additions & 6 deletions tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace DigitalRisks\LaravelEventStore\Tests;

use DigitalRisks\LaravelEventStore\Console\Commands\EventStoreWorker;
use DigitalRisks\LaravelEventStore\EventStore;
use DigitalRisks\LaravelEventStore\Tests\Fixtures\TestEvent;
use DigitalRisks\LaravelEventStore\Tests\Traits\InteractsWithEventStore;
use DigitalRisks\LaravelEventStore\Tests\Traits\MakesEventRecords;
use Illuminate\Support\Facades\Event;
use Rxnet\EventStore\Record\EventRecord;
use DigitalRisks\LaravelEventStore\Tests\Fixtures\TestEvent;

class WorkerTest extends TestCase
{
Expand Down Expand Up @@ -37,11 +38,10 @@ public function test_it_dispatches_a_classed_event_from_a_subscribed_event()
Event::fake();
$worker = resolve(EventStoreWorker::class);
$event = $this->makeEventRecord('test_event', ['hello' => 'world']);
config([
'eventstore.event_to_class' => function ($event) {
return 'DigitalRisks\LaravelEventStore\Tests\Fixtures\TestEvent';
}
]);

EventStore::eventToClass(function ($event) {
return 'DigitalRisks\LaravelEventStore\Tests\Fixtures\TestEvent';
});

// Act.
$worker->dispatch($event);
Expand Down

0 comments on commit 5c70f4c

Please sign in to comment.