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
|
38 |
|
public function __construct(BaseInvite $invite) |
24
|
|
|
{ |
25
|
38 |
|
$this->invite = $invite; |
26
|
38 |
|
} |
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
|
11 |
|
public function check($code, string $email = null) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
11 |
|
$this->prep($code, $email); |
53
|
6 |
|
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
|
21 |
|
protected function prep($code, string $email = null) |
71
|
|
|
{ |
72
|
21 |
|
$this->error = ''; |
73
|
21 |
|
$invite = $this->lookupInvite($code); |
74
|
19 |
|
$this->validateInvite($invite, $email); |
75
|
|
|
|
76
|
12 |
|
return $invite; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $code |
81
|
|
|
* |
82
|
|
|
* @return \Clarkeash\Doorman\Models\BaseInvite |
83
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\InvalidInviteCode |
84
|
|
|
*/ |
85
|
21 |
|
protected function lookupInvite($code): BaseInvite |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
21 |
|
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
|
19 |
|
protected function validateInvite(BaseInvite $invite, string $email = null) |
103
|
|
|
{ |
104
|
19 |
|
if ($invite->isFull()) { |
105
|
2 |
|
throw new MaxUsesReached(trans('doorman::messages.maxed', ['code' => $invite->code])); |
106
|
|
|
} |
107
|
|
|
|
108
|
17 |
|
if ($invite->hasExpired()) { |
109
|
2 |
|
throw new ExpiredInviteCode(trans('doorman::messages.expired', ['code' => $invite->code])); |
110
|
|
|
} |
111
|
|
|
|
112
|
15 |
|
if ($invite->isRestricted() && !$invite->isRestrictedFor($email)) { |
113
|
3 |
|
throw new NotYourInviteCode(trans('doorman::messages.restricted', ['code' => $invite->code])); |
114
|
|
|
} |
115
|
12 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return Generator |
119
|
|
|
*/ |
120
|
17 |
|
public function generate() |
121
|
|
|
{ |
122
|
17 |
|
return app(Generator::class); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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.