1 | <?php |
||
2 | |||
3 | namespace App; |
||
4 | |||
5 | use App\Notifications\InviteCompetitor; |
||
6 | use Illuminate\Database\Eloquent\Model; |
||
7 | use Illuminate\Support\Carbon; |
||
8 | use OwenIt\Auditing\Contracts\Auditable; |
||
9 | |||
10 | |||
11 | class Invite extends Model implements Auditable |
||
12 | { |
||
13 | use \OwenIt\Auditing\Auditable; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
14 | public $timestamps = true; |
||
15 | protected $table = 'invitation'; |
||
16 | protected $fillable = [ |
||
17 | 'code', |
||
18 | 'email', |
||
19 | |||
20 | 'expiration', |
||
21 | 'active', |
||
22 | 'used', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * Get invite from Token |
||
27 | * @param $token |
||
28 | * @return Invite |
||
29 | */ |
||
30 | 1 | public static function getInviteFromToken($token) |
|
31 | { |
||
32 | 1 | $invite = self::where('code', $token) |
|
33 | 1 | ->where('active', 1) |
|
34 | 1 | ->where('object_type', "App\Tournament") |
|
35 | 1 | ->first(); |
|
36 | 1 | return $invite; |
|
37 | } |
||
38 | |||
39 | public static function sendInvites($reader, $tournament) |
||
40 | { |
||
41 | $reader->each(function ($sheet) use ($tournament) { |
||
42 | // Loop through all rows of spreadsheet |
||
43 | $sheet->each(function ($row) use ($tournament) { |
||
44 | // Check email |
||
45 | $invite = new Invite(); |
||
46 | $code = $invite->generateTournamentInvite($row, $tournament); |
||
47 | $user = new User(); |
||
48 | $user->email = $row; |
||
49 | $user->notify(new InviteCompetitor($user, $tournament, $code)); |
||
50 | }); |
||
51 | }); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Send an email to competitor and generate or update invite |
||
56 | * @param $email |
||
57 | * @param $tournament |
||
58 | * @return String Invitation code | null |
||
59 | */ |
||
60 | 1 | public function generateTournamentInvite($email, Tournament $tournament) |
|
61 | { |
||
62 | 1 | $token = $this->hash_split(hash('sha256', $email)) . $this->hash_split(hash('sha256', time())); |
|
63 | 1 | $invite = Invite::firstOrNew(['email' => $email, 'object_type' => 'App\Tournament', 'object_id' => $tournament->id]); |
|
64 | 1 | $invite->code = $token; |
|
65 | 1 | $invite->email = $email; |
|
66 | 1 | $invite->object_type = 'App\Tournament'; |
|
67 | 1 | $invite->object_id = $tournament->id; |
|
68 | 1 | $invite->expiration = $tournament->registerDateLimit; |
|
69 | 1 | $invite->active = true; |
|
70 | |||
71 | 1 | if ($invite->save()) |
|
72 | 1 | return $token; |
|
73 | |||
74 | return null; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Helper used to hash email into token |
||
79 | * @param $hash |
||
80 | * @return mixed |
||
81 | */ |
||
82 | 1 | protected function hash_split($hash) |
|
83 | { |
||
84 | 1 | $output = str_split($hash, 8); |
|
85 | 1 | return $output[rand(0, 1)]; |
|
86 | } |
||
87 | |||
88 | 1 | public function object() |
|
89 | { |
||
90 | 1 | return $this->morphTo(); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Verify if it is in use |
||
95 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
96 | */ |
||
97 | public function tournament() |
||
98 | { |
||
99 | return $this->belongsTo('App\Tournament'); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Consume the invitation |
||
104 | */ |
||
105 | public function consume() |
||
106 | { |
||
107 | // Use the invitation |
||
108 | $this->update(['used' => 1]); |
||
109 | } |
||
110 | |||
111 | public function hasExpired() |
||
112 | { |
||
113 | return $this->expiration < Carbon::now() && $this->expiration != '0000-00-00'; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param $reader |
||
118 | * @param $tournament |
||
119 | */ |
||
120 | public function checkBadEmailsInExcel($reader, $tournament) |
||
121 | { |
||
122 | $reader->each(function ($sheet) use ($tournament) { |
||
123 | // Loop through all rows |
||
124 | $sheet->each(function ($row) use ($tournament) { |
||
0 ignored issues
–
show
|
|||
125 | // Check email |
||
126 | if (!filter_var($row, FILTER_VALIDATE_EMAIL)) { |
||
127 | $this->emailBadFormat = true; |
||
0 ignored issues
–
show
|
|||
128 | $this->wrongEmail = $row; |
||
0 ignored issues
–
show
|
|||
129 | } |
||
130 | }); |
||
131 | }); |
||
132 | } |
||
133 | |||
134 | } |
||
135 |