Skip to content

Commit

Permalink
Merge pull request #42 from mikemand/feature-events
Browse files Browse the repository at this point in the history
Add events to attach/detach.
  • Loading branch information
mpociot authored Jun 13, 2016
2 parents 7da60c9 + ce8d9bc commit aa17c5b
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -355,6 +356,73 @@ if( $invite ) // valid token found

The `denyInvite` method is only responsible for deleting the invitation from the database.

<a name="events" />
### 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
<?php

namespace App\Listeners;

use Mpociot\Teamwork\Events\UserJoinedTeam;

class YourJoinedTeamListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param UserJoinedTeam $event
* @return void
*/
public function handle(UserJoinedTeam $event)
{
// $user = $event->getUser();
// $teamId = $event->getTeamId();

// Do something with the user and team ID.
}
}
```

<a name="scope" />
### Limit Models to current Team

Expand Down
43 changes: 43 additions & 0 deletions src/Mpociot/Teamwork/Events/UserJoinedTeam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Mpociot\Teamwork\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;

class UserJoinedTeam
{
use SerializesModels;

/**
* @type Model
*/
protected $user;

/**
* @type int
*/
protected $team_id;

public function __construct($user, $team_id)
{
$this->user = $user;
$this->team_id = $team_id;
}

/**
* @return Model
*/
public function getUser()
{
return $this->user;
}

/**
* @return int
*/
public function getTeamId()
{
return $this->team_id;
}
}
43 changes: 43 additions & 0 deletions src/Mpociot/Teamwork/Events/UserLeftTeam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Mpociot\Teamwork\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;

class UserLeftTeam
{
use SerializesModels;

/**
* @type Model
*/
protected $user;

/**
* @type int
*/
protected $team_id;

public function __construct($user, $team_id)
{
$this->user = $user;
$this->team_id = $team_id;
}

/**
* @return Model
*/
public function getUser()
{
return $this->user;
}

/**
* @return int
*/
public function getTeamId()
{
return $this->team_id;
}
}
6 changes: 6 additions & 0 deletions src/Mpociot/Teamwork/Traits/UserHasTeams.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand Down
18 changes: 18 additions & 0 deletions tests/UserHasTeamsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit aa17c5b

Please sign in to comment.