Trigger an event before a user is created, allowing customising its data via listeners

parent e47cd4e5
<?php
namespace Modules\User\Events;
final class UserIsCreating
{
/**
* @var array
*/
private $attributes;
public $original;
public function __construct(array $attributes)
{
$this->attributes = $attributes;
$this->original = $attributes;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = array_merge($this->attributes, $attributes);
}
}
...@@ -7,6 +7,7 @@ use Cartalyst\Sentinel\Laravel\Facades\Sentinel; ...@@ -7,6 +7,7 @@ use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Modules\User\Entities\Sentinel\User; use Modules\User\Entities\Sentinel\User;
use Modules\User\Events\UserHasRegistered; use Modules\User\Events\UserHasRegistered;
use Modules\User\Events\UserIsCreating;
use Modules\User\Events\UserIsUpdating; use Modules\User\Events\UserIsUpdating;
use Modules\User\Events\UserWasCreated; use Modules\User\Events\UserWasCreated;
use Modules\User\Events\UserWasUpdated; use Modules\User\Events\UserWasUpdated;
...@@ -48,7 +49,9 @@ class SentinelUserRepository implements UserRepository ...@@ -48,7 +49,9 @@ class SentinelUserRepository implements UserRepository
public function create(array $data, $activated = false) public function create(array $data, $activated = false)
{ {
$this->hashPassword($data); $this->hashPassword($data);
$user = $this->user->create((array) $data);
event($event = new UserIsCreating($data));
$user = $this->user->create($event->getAttributes());
if ($activated) { if ($activated) {
$this->activateUser($user); $this->activateUser($user);
......
...@@ -5,6 +5,7 @@ namespace Modules\User\Tests; ...@@ -5,6 +5,7 @@ namespace Modules\User\Tests;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Modules\User\Entities\Sentinel\User; use Modules\User\Entities\Sentinel\User;
use Modules\User\Events\UserHasRegistered; use Modules\User\Events\UserHasRegistered;
use Modules\User\Events\UserIsCreating;
use Modules\User\Events\UserIsUpdating; use Modules\User\Events\UserIsUpdating;
use Modules\User\Events\UserWasCreated; use Modules\User\Events\UserWasCreated;
use Modules\User\Events\UserWasUpdated; use Modules\User\Events\UserWasUpdated;
...@@ -44,6 +45,69 @@ class SentinelUserRepositoryTest extends BaseUserTestCase ...@@ -44,6 +45,69 @@ class SentinelUserRepositoryTest extends BaseUserTestCase
$this->assertCount(1, $this->user->all()); $this->assertCount(1, $this->user->all());
} }
/** @test */
public function it_fires_event_when_user_is_creating()
{
Event::fake();
$user = $this->user->create([
'email' => 'n.widart@gmail.com',
'password' => 'demo1234',
]);
Event::assertDispatched(UserIsCreating::class, function ($e) use ($user) {
return $e->getAttributes()['email'] === $user->email;
});
}
/** @test */
public function it_can_change_data_when_it_is_creating_event()
{
Event::listen(UserIsCreating::class, function (UserIsCreating $event) {
$event->setAttributes(['email' => 'john@doe.com']);
});
$user = $this->user->create([
'email' => 'n.widart@gmail.com',
'password' => 'demo1234',
]);
$this->assertEquals('john@doe.com', $user->email);
}
/** @test */
public function it_can_change_the_data_multiple_times()
{
Event::listen(UserIsCreating::class, function (UserIsCreating $event) {
$event->setAttributes(['email' => 'john@doe.com']);
});
Event::listen(UserIsCreating::class, function (UserIsCreating $event) {
$event->setAttributes(['email' => 'jane@doe.com']);
});
$user = $this->user->create([
'email' => 'n.widart@gmail.com',
'password' => 'demo1234',
]);
$this->assertEquals('jane@doe.com', $user->email);
}
/** @test */
public function it_makes_sure_the_event_contains_original_attributes()
{
Event::fake();
$this->user->create([
'email' => 'n.widart@gmail.com',
'password' => 'demo1234',
]);
Event::assertDispatched(UserIsCreating::class, function ($e) {
return $e->original['email'] === 'n.widart@gmail.com';
});
}
/** @test */ /** @test */
public function it_fires_event_when_user_created() public function it_fires_event_when_user_created()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment