Skip to content

Commit

Permalink
Merge pull request #52 from BlueTeck/event
Browse files Browse the repository at this point in the history
add UserInvitedToTeam event
  • Loading branch information
mpociot authored Aug 29, 2016
2 parents 4a5f812 + 53aef45 commit b7730a0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ phpunit.phar
/.settings
.env.*.php
.env.php
bin
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +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)
- [Attach/Detach/Invite Events](#events)
- [Limit Models to current Team](#scope)
- [License](#license)

Expand Down Expand Up @@ -363,14 +363,16 @@ if( $invite ) // valid token found
The `denyInvite` method is only responsible for deleting the invitation from the database.

<a name="events" />
### Attaching/Detaching Events
### Attaching/Detaching/Invite Events

If you need to run additional processes after attaching or detaching a team from a user, you can Listen for these events:
If you need to run additional processes after attaching/detaching a team from a user or inviting a user, you can Listen for these events:

```php
\Mpociot\Teamwork\Events\UserJoinedTeam

\Mpociot\Teamwork\Events\UserLeftTeam

\Mpociot\Teamwork\Events\UserInvitedToTeam
```

In your `EventServiceProvider` add your listener(s):
Expand All @@ -389,10 +391,13 @@ protected $listen = [
\Mpociot\Teamwork\Events\UserLeftTeam::class => [
App\Listeners\YourLeftTeamListener::class,
],
\Mpociot\Teamwork\Events\UserInvitedToTeam::class => [
App\Listeners\YourUserInvitedToTeamListener::class,
],
];
```

Each event exposes the User and Team's ID. In your listener, you can access them like so:
The UserJoinedTeam and UserLeftTeam event exposes the User and Team's ID. In your listener, you can access them like so:

```php
<?php
Expand Down Expand Up @@ -429,6 +434,43 @@ class YourJoinedTeamListener
}
```

The UserInvitedToTeam event contains an invite object which could be accessed like this:

```php
<?php

namespace App\Listeners;

use Mpociot\Teamwork\Events\UserInvitedToTeam;

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

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

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

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

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

namespace Mpociot\Teamwork\Events;

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

class UserInvitedToTeam
{
use SerializesModels;

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

public function __construct($invite)
{
$this->invite = $invite;
}

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

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

use Illuminate\Support\Facades\Config;
use Mpociot\Teamwork\Events\UserInvitedToTeam;

/**
* This file is part of Teamwork
Expand Down Expand Up @@ -80,6 +81,7 @@ public function inviteToTeam( $user, $team = null, callable $success = null )

if ( !is_null( $success ) )
{
event(new UserInvitedToTeam($invite));
return $success( $invite );
}
}
Expand Down
8 changes: 7 additions & 1 deletion tests/TeamworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,13 @@ public function testCanInviteToTeamWithoutCallback()
'team_id' => $team->getKey()
]);
}


public function testInviteToTeamFiresEvent()
{
$team = TeamworkTeam::create(['name' => 'Test-Team 1']);
$invite = $this->createInvite($team);
$this->expectsEvents(\Mpociot\Teamwork\Events\UserInvitedToTeam::class);
}

}

0 comments on commit b7730a0

Please sign in to comment.