Teamwork::inviteToTeam()   B
last analyzed

Complexity

Conditions 8
Paths 20

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.1315
c 0
b 0
f 0
cc 8
nc 20
nop 3
1
<?php
2
3
namespace Mpociot\Teamwork;
4
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Foundation\Application;
7
use Mpociot\Teamwork\Events\UserInvitedToTeam;
8
9
class Teamwork
10
{
11
    /**
12
     * Laravel application.
13
     *
14
     * @var \Illuminate\Foundation\Application
15
     */
16
    public $app;
17
18
    /**
19
     * Create a new Teamwork instance.
20
     *
21
     * @param \Illuminate\Foundation\Application $app
22
     */
23
    public function __construct(Application $app)
24
    {
25
        $this->app = $app;
26
    }
27
28
    /**
29
     * Get the currently authenticated user or null.
30
     */
31
    public function user()
32
    {
33
        return $this->app->auth->user();
34
    }
35
36
    /**
37
     * Invite an email adress to a team.
38
     * Either provide a email address or an object with an email property.
39
     *
40
     * If no team is given, the current_team_id will be used instead.
41
     *
42
     * @param string|User $user
43
     * @param null|Team $team
44
     * @param callable $success
45
     * @return TeamInvite
46
     * @throws \Exception
47
     */
48
    public function inviteToTeam($user, $team = null, callable $success = null)
49
    {
50
        if (is_null($team)) {
51
            $team = $this->user()->current_team_id;
52
        } elseif (is_object($team)) {
53
            $team = $team->getKey();
54
        } elseif (is_array($team)) {
55
            $team = $team['id'];
56
        }
57
58
        if (is_object($user) && isset($user->email)) {
59
            $email = $user->email;
60
        } elseif (is_string($user)) {
61
            $email = $user;
62
        } else {
63
            throw new \Exception('The provided object has no "email" attribute and is not a string.');
64
        }
65
66
        $invite = $this->app->make(Config::get('teamwork.invite_model'));
67
        $invite->user_id = $this->user()->getKey();
68
        $invite->team_id = $team;
69
        $invite->type = 'invite';
70
        $invite->email = $email;
71
        $invite->accept_token = md5(uniqid(microtime()));
72
        $invite->deny_token = md5(uniqid(microtime()));
73
        $invite->save();
74
75
        if (! is_null($success)) {
76
            event(new UserInvitedToTeam($invite));
77
            $success($invite);
78
        }
79
80
        return $invite;
81
    }
82
83
    /**
84
     * Checks if the given email address has a pending invite for the
85
     * provided Team.
86
     * @param $email
87
     * @param Team|array|int $team
88
     * @return bool
89
     */
90
    public function hasPendingInvite($email, $team)
91
    {
92
        if (is_object($team)) {
93
            $team = $team->getKey();
94
        }
95
        if (is_array($team)) {
96
            $team = $team['id'];
97
        }
98
99
        return $this->app->make(Config::get('teamwork.invite_model'))->where('email', '=', $email)->where('team_id', '=', $team)->first() ? true : false;
100
    }
101
102
    /**
103
     * @param $token
104
     * @return mixed
105
     */
106
    public function getInviteFromAcceptToken($token)
107
    {
108
        return $this->app->make(Config::get('teamwork.invite_model'))->where('accept_token', '=', $token)->first();
109
    }
110
111
    /**
112
     * @param TeamInvite $invite
113
     */
114
    public function acceptInvite(TeamInvite $invite)
115
    {
116
        $this->user()->attachTeam($invite->team);
117
        $invite->delete();
118
    }
119
120
    /**
121
     * @param $token
122
     * @return mixed
123
     */
124
    public function getInviteFromDenyToken($token)
125
    {
126
        return $this->app->make(Config::get('teamwork.invite_model'))->where('deny_token', '=', $token)->first();
127
    }
128
129
    /**
130
     * @param TeamInvite $invite
131
     */
132
    public function denyInvite(TeamInvite $invite)
133
    {
134
        $invite->delete();
135
    }
136
}
137