|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Clarkeash\Doorman; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use DateTimeInterface; |
|
7
|
|
|
use Clarkeash\Doorman\Exceptions\DuplicateException; |
|
8
|
|
|
use Clarkeash\Doorman\Models\BaseInvite; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
|
|
11
|
|
|
class Generator |
|
12
|
|
|
{ |
|
13
|
|
|
protected $amount = 1; |
|
14
|
|
|
protected $uses = 1; |
|
15
|
|
|
protected $email = null; |
|
16
|
|
|
protected $expiry; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Clarkeash\Doorman\DoormanManager |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $manager; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var BaseInvite |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $invite; |
|
27
|
|
|
|
|
28
|
17 |
|
public function __construct(DoormanManager $manager, BaseInvite $invite) |
|
29
|
|
|
{ |
|
30
|
17 |
|
$this->manager = $manager; |
|
31
|
17 |
|
$this->invite = $invite; |
|
32
|
17 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param int $amount |
|
36
|
|
|
* |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function times(int $amount = 1) |
|
40
|
|
|
{ |
|
41
|
2 |
|
$this->amount = $amount; |
|
42
|
|
|
|
|
43
|
2 |
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int $amount |
|
48
|
|
|
* |
|
49
|
|
|
* @return $this |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function uses(int $amount = 1) |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->uses = $amount; |
|
54
|
|
|
|
|
55
|
2 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function unlimited() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->uses(0); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $email |
|
68
|
|
|
* |
|
69
|
|
|
* @return $this |
|
70
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\DuplicateException |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function for(string $email) |
|
73
|
|
|
{ |
|
74
|
3 |
|
if ($this->invite->where('for', strtolower($email))->first()) { |
|
75
|
1 |
|
throw new DuplicateException('You cannot create more than 1 invite code for an email'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
$this->email = $email; |
|
79
|
|
|
|
|
80
|
3 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param DateTimeInterface $date |
|
85
|
|
|
* |
|
86
|
|
|
* @return $this |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function expiresOn(DateTimeInterface $date) |
|
89
|
|
|
{ |
|
90
|
4 |
|
$this->expiry = $date; |
|
91
|
|
|
|
|
92
|
4 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param int $days |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function expiresIn($days = 14) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$this->expiry = Carbon::now(config('app.timezone'))->addDays($days)->endOfDay(); |
|
103
|
|
|
|
|
104
|
1 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return \Clarkeash\Doorman\Models\BaseInvite |
|
109
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
110
|
|
|
*/ |
|
111
|
15 |
|
protected function build(): BaseInvite |
|
112
|
|
|
{ |
|
113
|
15 |
|
$invite = app()->make(BaseInvite::class); |
|
114
|
15 |
|
$invite->code = Str::upper($this->manager->code()); |
|
115
|
15 |
|
$invite->for = $this->email; |
|
116
|
15 |
|
$invite->max = $this->uses; |
|
117
|
15 |
|
$invite->valid_until = $this->expiry; |
|
118
|
|
|
|
|
119
|
15 |
|
return $invite; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return \Illuminate\Support\Collection |
|
124
|
|
|
* @throws DuplicateException |
|
125
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
126
|
|
|
*/ |
|
127
|
14 |
|
public function make() |
|
128
|
|
|
{ |
|
129
|
14 |
|
$invites = collect(); |
|
130
|
|
|
|
|
131
|
14 |
|
if (!is_null($this->email) && $this->amount > 1) { |
|
132
|
1 |
|
throw new DuplicateException('You cannot create more than 1 invite code for an email'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
13 |
|
for ($i = 0; $i < $this->amount; $i++) { |
|
136
|
13 |
|
$invite = $this->build(); |
|
137
|
|
|
|
|
138
|
13 |
|
$invites->push($invite); |
|
139
|
|
|
|
|
140
|
13 |
|
$invite->save(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
13 |
|
return $invites; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return BaseInvite |
|
148
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
149
|
|
|
*/ |
|
150
|
2 |
|
public function once(): BaseInvite |
|
151
|
|
|
{ |
|
152
|
2 |
|
return tap($this->build())->save(); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|