|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Clarkeash\Doorman; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Clarkeash\Doorman\Exceptions\DuplicateException; |
|
7
|
|
|
use Clarkeash\Doorman\Models\Invite; |
|
8
|
|
|
|
|
9
|
|
|
class Generator |
|
10
|
|
|
{ |
|
11
|
|
|
protected $amount = 1; |
|
12
|
|
|
protected $uses = 1; |
|
13
|
|
|
protected $email = null; |
|
14
|
|
|
protected $expiry; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Clarkeash\Doorman\DoormanManager |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $manager; |
|
20
|
|
|
|
|
21
|
10 |
|
public function __construct(DoormanManager $manager) |
|
22
|
|
|
{ |
|
23
|
10 |
|
$this->manager = $manager; |
|
24
|
10 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param int $amount |
|
28
|
|
|
* |
|
29
|
|
|
* @return $this |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function times(int $amount = 1) |
|
32
|
|
|
{ |
|
33
|
2 |
|
$this->amount = $amount; |
|
34
|
|
|
|
|
35
|
2 |
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param int $amount |
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function uses(int $amount = 1) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$this->uses = $amount; |
|
46
|
|
|
|
|
47
|
1 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $email |
|
52
|
|
|
* |
|
53
|
|
|
* @return $this |
|
54
|
|
|
* @throws \Clarkeash\Doorman\Exceptions\DuplicateException |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function for(string $email) |
|
57
|
|
|
{ |
|
58
|
3 |
|
if (Invite::where('for', strtolower($email))->first()) { |
|
59
|
1 |
|
throw new DuplicateException('You cannot create more than 1 invite code for an email'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
$this->email = $email; |
|
63
|
|
|
|
|
64
|
3 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param \Carbon\Carbon $date |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function expiresOn(Carbon $date) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$this->expiry = $date; |
|
75
|
|
|
|
|
76
|
1 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param int $days |
|
81
|
|
|
* |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function expiresIn($days = 14) |
|
85
|
|
|
{ |
|
86
|
1 |
|
$this->expiry = Carbon::now(config('app.timezone'))->addDays($days)->endOfDay(); |
|
87
|
|
|
|
|
88
|
1 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return \Clarkeash\Doorman\Models\Invite |
|
93
|
|
|
*/ |
|
94
|
8 |
|
protected function build(): Invite |
|
95
|
|
|
{ |
|
96
|
8 |
|
$invite = new Invite; |
|
97
|
8 |
|
$invite->code = $this->manager->code(); |
|
|
|
|
|
|
98
|
8 |
|
$invite->for = $this->email; |
|
|
|
|
|
|
99
|
8 |
|
$invite->max = $this->uses; |
|
|
|
|
|
|
100
|
8 |
|
$invite->valid_until = $this->expiry; |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
8 |
|
return $invite; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return \Illuminate\Support\Collection |
|
107
|
|
|
*/ |
|
108
|
9 |
|
public function make() |
|
109
|
|
|
{ |
|
110
|
9 |
|
$invites = collect(); |
|
111
|
|
|
|
|
112
|
9 |
|
if (!is_null($this->email) && $this->amount > 1) { |
|
113
|
1 |
|
throw new DuplicateException('You cannot create more than 1 invite code for an email'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
8 |
|
for ($i = 0; $i < $this->amount; $i++) { |
|
117
|
8 |
|
$invite = $this->build(); |
|
118
|
|
|
|
|
119
|
8 |
|
$invites->push($invite); |
|
120
|
|
|
|
|
121
|
8 |
|
$invite->save(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
8 |
|
return $invites; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.