|
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\Invite; |
|
11
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
12
|
|
|
use Illuminate\Support\Str; |
|
13
|
|
|
|
|
14
|
|
|
class Doorman |
|
15
|
|
|
{ |
|
16
|
|
|
public $error = ''; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param $code |
|
20
|
|
|
* @param string|null $email |
|
21
|
|
|
*/ |
|
22
|
|
|
public function redeem($code, string $email = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$invite = $this->prep($code, $email); |
|
25
|
|
|
|
|
26
|
|
|
$invite->increment('uses'); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param $code |
|
31
|
|
|
* @param string|null $email |
|
32
|
|
|
* |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
|
public function check($code, string $email = null) |
|
36
|
|
|
{ |
|
37
|
|
|
try { |
|
38
|
|
|
$this->prep($code, $email); |
|
39
|
|
|
return true; |
|
40
|
|
|
} catch (DoormanException $e) { |
|
41
|
|
|
$this->error = $e->getMessage(); |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function prep($code, string $email = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->error = ''; |
|
49
|
|
|
$invite = $this->lookupInvite($code); |
|
50
|
|
|
$this->validateInvite($invite, $email); |
|
51
|
|
|
|
|
52
|
|
|
return $invite; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $code |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Clarkeash\Doorman\Models\Invite |
|
59
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\InvalidInviteCode |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function lookupInvite($code): Invite |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
|
|
return Invite::where('code', '=', Str::upper($code))->firstOrFail(); |
|
65
|
|
|
} catch (ModelNotFoundException $e) { |
|
66
|
|
|
throw new InvalidInviteCode(trans('doorman::messages.invalid', [ 'code' => $code ])); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param \Clarkeash\Doorman\Models\Invite $invite |
|
72
|
|
|
* @param string|null $email |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\ExpiredInviteCode |
|
75
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\MaxUsesReached |
|
76
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\NotYourInviteCode |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function validateInvite(Invite $invite, string $email = null) |
|
79
|
|
|
{ |
|
80
|
|
|
if ($invite->isFull()) { |
|
81
|
|
|
throw new MaxUsesReached(trans('doorman::messages.maxed', [ 'code' => $invite->code ])); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($invite->hasExpired()) { |
|
85
|
|
|
throw new ExpiredInviteCode(trans('doorman::messages.expired', [ 'code' => $invite->code ])); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($invite->isRestricted() && !$invite->isRestrictedFor($email)) { |
|
89
|
|
|
throw new NotYourInviteCode(trans('doorman::messages.restricted', [ 'code' => $invite->code ])); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function generate() |
|
94
|
|
|
{ |
|
95
|
|
|
return app(Generator::class); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
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.