Passed
Push — ft/states ( 40b31d...61e8cc )
by Ben
07:05
created

Invitation::stateOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Users\Invites;
4
5
use Thinktomorrow\Chief\States\State\StatefulContract;
6
use Thinktomorrow\Chief\Users\User;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\URL;
9
10
class Invitation extends Model implements StatefulContract
11
{
12
    public $guarded = [];
13
14
    protected $dates = ['expires_at'];
15
16
    /**
17
     * Minutes from now that invitation will expire.
18
     *
19
     * @var int
20
     */
21
    private static $expires = 60 * 24 * 3;
22
23 17
    public static function make(string $invitee_id, string $inviter_id, $expires = null)
24
    {
25 17
        $token = InvitationToken::generate();
26
27 17
        return self::create([
28 17
            'invitee_id' => $invitee_id,
29 17
            'inviter_id' => $inviter_id,
30 17
            'state'      => InvitationState::NONE,
31 17
            'token'      => $token,
32 17
            'expires_at' => now()->addMinutes($expires ?? self::$expires),
33
        ]);
34
    }
35
36 10
    public static function findByToken(string $token)
37
    {
38 10
        return self::where('token', $token)->first();
39
    }
40
41 7
    public function invitee()
42
    {
43 7
        return $this->belongsTo(User::class, 'invitee_id');
44
    }
45
46 1
    public function inviter()
47
    {
48 1
        return $this->belongsTo(User::class, 'inviter_id');
49
    }
50
51 10
    public function acceptUrl()
52
    {
53 10
        return URL::temporarySignedRoute(
54 10
            'invite.accept', $this->expires_at, ['token' => $this->token]
55
        );
56
    }
57
58 6
    public function denyUrl()
59
    {
60 6
        return URL::temporarySignedRoute(
61 6
            'invite.deny', $this->expires_at, ['token' => $this->token]
62
        );
63
    }
64
65 9
    public function stateOf($key): string
66
    {
67 9
        return $this->$key;
68
    }
69
70 16
    public function changeStateOf($key, $state)
71
    {
72 16
        $this->$key = $state;
73 16
        $this->save();
74 16
    }
75
76
    public function present()
77
    {
78
        return new InvitationPresenter($this);
79
    }
80
}
81