From ce8d9bcea98d49a61f0092e4677b294a5e98b3cb Mon Sep 17 00:00:00 2001 From: Micheal Mand Date: Mon, 13 Jun 2016 14:21:03 -0600 Subject: [PATCH] Add events to attach/detach. Signed-off-by: Micheal Mand --- README.md | 68 +++++++++++++++++++ .../Teamwork/Events/UserJoinedTeam.php | 43 ++++++++++++ src/Mpociot/Teamwork/Events/UserLeftTeam.php | 43 ++++++++++++ src/Mpociot/Teamwork/Traits/UserHasTeams.php | 6 ++ tests/UserHasTeamsTraitTest.php | 18 +++++ 5 files changed, 178 insertions(+) create mode 100644 src/Mpociot/Teamwork/Events/UserJoinedTeam.php create mode 100644 src/Mpociot/Teamwork/Events/UserLeftTeam.php diff --git a/README.md b/README.md index 9ebae4e..41bb1d8 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Teamwork is the fastest and easiest method to add a User / Team association with - [Inviting others](#inviting-others) - [Accepting invites](#accepting-invites) - [Denying invites](#denying-invites) + - [Attach/Detach Events](#events) - [Limit Models to current Team](#scope) - [License](#license) @@ -355,6 +356,73 @@ if( $invite ) // valid token found The `denyInvite` method is only responsible for deleting the invitation from the database. + +### Attaching/Detaching Events + +If you need to run additional processes after attaching or detaching a team from a user, you can Listen for these events: + +```php +\Mpociot\Teamwork\Events\UserJoinedTeam + +\Mpociot\Teamwork\Events\UserLeftTeam +``` + +In your `EventServiceProvider` add your listener(s): + +```php +/** + * The event listener mappings for the application. + * + * @var array + */ +protected $listen = [ + ... + \Mpociot\Teamwork\Events\UserJoinedTeam::class => [ + App\Listeners\YourJoinedTeamListener::class, + ], + \Mpociot\Teamwork\Events\UserLeftTeam::class => [ + App\Listeners\YourLeftTeamListener::class, + ], +]; +``` + +Each event exposes the User and Team's ID. In your listener, you can access them like so: + +```php +getUser(); + // $teamId = $event->getTeamId(); + + // Do something with the user and team ID. + } +} +``` + ### Limit Models to current Team diff --git a/src/Mpociot/Teamwork/Events/UserJoinedTeam.php b/src/Mpociot/Teamwork/Events/UserJoinedTeam.php new file mode 100644 index 0000000..82478ba --- /dev/null +++ b/src/Mpociot/Teamwork/Events/UserJoinedTeam.php @@ -0,0 +1,43 @@ +user = $user; + $this->team_id = $team_id; + } + + /** + * @return Model + */ + public function getUser() + { + return $this->user; + } + + /** + * @return int + */ + public function getTeamId() + { + return $this->team_id; + } +} \ No newline at end of file diff --git a/src/Mpociot/Teamwork/Events/UserLeftTeam.php b/src/Mpociot/Teamwork/Events/UserLeftTeam.php new file mode 100644 index 0000000..4e85738 --- /dev/null +++ b/src/Mpociot/Teamwork/Events/UserLeftTeam.php @@ -0,0 +1,43 @@ +user = $user; + $this->team_id = $team_id; + } + + /** + * @return Model + */ + public function getUser() + { + return $this->user; + } + + /** + * @return int + */ + public function getTeamId() + { + return $this->team_id; + } +} \ No newline at end of file diff --git a/src/Mpociot/Teamwork/Traits/UserHasTeams.php b/src/Mpociot/Teamwork/Traits/UserHasTeams.php index b848d26..b9a71f3 100644 --- a/src/Mpociot/Teamwork/Traits/UserHasTeams.php +++ b/src/Mpociot/Teamwork/Traits/UserHasTeams.php @@ -10,6 +10,8 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\Config; +use Mpociot\Teamwork\Events\UserJoinedTeam; +use Mpociot\Teamwork\Events\UserLeftTeam; use Mpociot\Teamwork\Exceptions\UserNotInTeamException; trait UserHasTeams @@ -158,6 +160,8 @@ public function attachTeam( $team, $pivotData = [] ) { $this->teams()->attach( $team, $pivotData ); + event(new UserJoinedTeam($this, $team)); + if( $this->relationLoaded('teams') ) { $this->load('teams'); } @@ -176,6 +180,8 @@ public function detachTeam( $team ) $team = $this->retrieveTeamId( $team ); $this->teams()->detach( $team ); + event(new UserLeftTeam($this, $team)); + if( $this->relationLoaded('teams') ) { $this->load('teams'); } diff --git a/tests/UserHasTeamsTraitTest.php b/tests/UserHasTeamsTraitTest.php index f26e18c..1efdd11 100644 --- a/tests/UserHasTeamsTraitTest.php +++ b/tests/UserHasTeamsTraitTest.php @@ -189,6 +189,24 @@ public function testDetachTeamResetsCurrentTeam() $this->assertNull($this->user->currentTeam); } + public function testAttachTeamFiresEvent() + { + $this->expectsEvents(\Mpociot\Teamwork\Events\UserJoinedTeam::class); + $this->doesntExpectEvents(\Mpociot\Teamwork\Events\UserLeftTeam::class); + + $team1 = TeamworkTeam::create(['name' => 'Test-Team 1']); + $this->user->attachTeam($team1); + } + + public function testDetachTeamFiresEvent() + { + $this->expectsEvents(\Mpociot\Teamwork\Events\UserLeftTeam::class); + + $team1 = TeamworkTeam::create(['name' => 'Test-Team 1']); + $this->user->attachTeam($team1); + $this->user->detachTeam($team1); + } + public function testCanAttachMultipleTeams() { $team1 = TeamworkTeam::create(['name' => 'Test-Team 1']);