|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Clarkeash\Doorman; |
|
4
|
|
|
|
|
5
|
|
|
use Clarkeash\Doorman\Exceptions\DoormanException; |
|
6
|
|
|
use Clarkeash\Doorman\Exceptions\ExpiredInviteCode; |
|
7
|
|
|
use Clarkeash\Doorman\Exceptions\InvalidInviteCode; |
|
8
|
|
|
use Clarkeash\Doorman\Exceptions\MaxUsesReached; |
|
9
|
|
|
use Clarkeash\Doorman\Exceptions\NotYourInviteCode; |
|
10
|
|
|
use Clarkeash\Doorman\Models\BaseInvite; |
|
11
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
12
|
|
|
use Illuminate\Support\Str; |
|
13
|
|
|
|
|
14
|
|
|
class Doorman |
|
15
|
|
|
{ |
|
16
|
|
|
public $error = ''; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var BaseInvite |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $invite; |
|
22
|
|
|
|
|
23
|
34 |
|
public function __construct(BaseInvite $invite) |
|
24
|
|
|
{ |
|
25
|
34 |
|
$this->invite = $invite; |
|
26
|
34 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $code |
|
30
|
|
|
* @param string|null $email |
|
31
|
|
|
* @throws ExpiredInviteCode |
|
32
|
|
|
* @throws InvalidInviteCode |
|
33
|
|
|
* @throws MaxUsesReached |
|
34
|
|
|
* @throws NotYourInviteCode |
|
35
|
|
|
*/ |
|
36
|
10 |
|
public function redeem($code, string $email = null) |
|
37
|
|
|
{ |
|
38
|
10 |
|
$invite = $this->prep($code, $email); |
|
39
|
|
|
|
|
40
|
6 |
|
$invite->increment('uses'); |
|
|
|
|
|
|
41
|
6 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $code |
|
45
|
|
|
* @param string|null $email |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
12 |
|
public function check($code, string $email = null) |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
12 |
|
$this->prep($code, $email); |
|
53
|
7 |
|
return true; |
|
54
|
5 |
|
} catch (DoormanException $e) { |
|
55
|
5 |
|
$this->error = $e->getMessage(); |
|
56
|
5 |
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param $code |
|
62
|
|
|
* @param string|null $email |
|
63
|
|
|
* |
|
64
|
|
|
* @return \Clarkeash\Doorman\Models\BaseInvite |
|
65
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\ExpiredInviteCode |
|
66
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\InvalidInviteCode |
|
67
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\MaxUsesReached |
|
68
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\NotYourInviteCode |
|
69
|
|
|
*/ |
|
70
|
22 |
|
protected function prep($code, string $email = null) |
|
71
|
|
|
{ |
|
72
|
22 |
|
$this->error = ''; |
|
73
|
22 |
|
$invite = $this->lookupInvite($code); |
|
74
|
20 |
|
$this->validateInvite($invite, $email); |
|
75
|
|
|
|
|
76
|
13 |
|
return $invite; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $code |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Clarkeash\Doorman\Models\BaseInvite |
|
83
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\InvalidInviteCode |
|
84
|
|
|
*/ |
|
85
|
22 |
|
protected function lookupInvite($code): BaseInvite |
|
86
|
|
|
{ |
|
87
|
|
|
try { |
|
88
|
22 |
|
return $this->invite->where('code', '=', Str::upper($code))->firstOrFail(); |
|
|
|
|
|
|
89
|
2 |
|
} catch (ModelNotFoundException $e) { |
|
90
|
2 |
|
throw new InvalidInviteCode(trans('doorman::messages.invalid', ['code' => $code])); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param \Clarkeash\Doorman\Models\BaseInvite $invite |
|
96
|
|
|
* @param string|null $email |
|
97
|
|
|
* |
|
98
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\ExpiredInviteCode |
|
99
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\MaxUsesReached |
|
100
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\NotYourInviteCode |
|
101
|
|
|
*/ |
|
102
|
20 |
|
protected function validateInvite(BaseInvite $invite, string $email = null) |
|
103
|
|
|
{ |
|
104
|
20 |
|
if ($invite->isFull()) { |
|
105
|
2 |
|
throw new MaxUsesReached(trans('doorman::messages.maxed', ['code' => $invite->code])); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
18 |
|
if ($invite->hasExpired()) { |
|
109
|
2 |
|
throw new ExpiredInviteCode(trans('doorman::messages.expired', ['code' => $invite->code])); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
16 |
|
if ($invite->isRestricted() && !$invite->isRestrictedFor($email)) { |
|
113
|
3 |
|
throw new NotYourInviteCode(trans('doorman::messages.restricted', ['code' => $invite->code])); |
|
114
|
|
|
} |
|
115
|
13 |
|
} |
|
116
|
|
|
|
|
117
|
12 |
|
public function generate() |
|
118
|
|
|
{ |
|
119
|
12 |
|
return app(Generator::class); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.