Mpociot\Teamwork\Teamwork::inviteToTeam PHP Method

inviteToTeam() public method

Either provide a email address or an object with an email property. If no team is given, the current_team_id will be used instead.
public inviteToTeam ( string | User $user, null | Team $team = null, callable $success = null )
$user string | User
$team null | Team
$success callable
    public function inviteToTeam($user, $team = null, callable $success = null)
    {
        if (is_null($team)) {
            $team = $this->user()->current_team_id;
        } elseif (is_object($team)) {
            $team = $team->getKey();
        } elseif (is_array($team)) {
            $team = $team["id"];
        }
        if (is_object($user) && isset($user->email)) {
            $email = $user->email;
        } elseif (is_string($user)) {
            $email = $user;
        } else {
            throw new \Exception('The provided object has no "email" attribute and is not a string.');
        }
        $invite = $this->app->make(Config::get('teamwork.invite_model'));
        $invite->user_id = $this->user()->getKey();
        $invite->team_id = $team;
        $invite->type = 'invite';
        $invite->email = $email;
        $invite->accept_token = md5(uniqid(microtime()));
        $invite->deny_token = md5(uniqid(microtime()));
        $invite->save();
        if (!is_null($success)) {
            event(new UserInvitedToTeam($invite));
            return $success($invite);
        }
    }

Usage Example

Example #1
0
 public function testCanNotInviteToUserWithoutEmail()
 {
     /*
     |------------------------------------------------------------
     | Set
     |------------------------------------------------------------
     */
     $email = "*****@*****.**";
     $team_id = 1;
     $app = m::mock('App');
     $teamwork = new Teamwork($app);
     $app->auth = m::mock('Auth');
     $user = m::mock('User');
     $user->current_team_id = $team_id;
     $user->shouldReceive('getKey')->never();
     $app->auth->shouldReceive('user')->andReturn($user);
     $app->shouldReceive('make')->with('Mpociot\\Teamwork\\TeamInvite')->never();
     $this->setExpectedException('Exception', 'The provided object has no "email" attribute and is not a string.');
     $teamwork->inviteToTeam($user);
 }
All Usage Examples Of Mpociot\Teamwork\Teamwork::inviteToTeam