Skip to content

Commit d9d3fc5

Browse files
committed
Mail Markdown + Refactoring
1 parent e14cb09 commit d9d3fc5

7 files changed

+140
-164
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ vendor
44
composer.lock
55
phpunit.xml
66
.DS_Store
7-
7+
.phpcd
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Jrean\UserVerification\Mail;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
7+
use Illuminate\Mail\Mailable;
8+
use Illuminate\Queue\SerializesModels;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
11+
class VerificationTokenGenerated extends Mailable
12+
{
13+
use Queueable, SerializesModels;
14+
15+
/**
16+
* User instance.
17+
*
18+
* @var \Illuminate\Contracts\Auth\Authenticatable
19+
*/
20+
public $user;
21+
22+
/**
23+
* The subject of the message.
24+
*
25+
* @var string
26+
*/
27+
public $subject;
28+
29+
/**
30+
* The person the message is from.
31+
*
32+
* @var mixed
33+
*/
34+
public $from;
35+
36+
/**
37+
* The person name the message is from.
38+
*
39+
* @var mixed
40+
*/
41+
public $name;
42+
43+
/**
44+
* Create a new message instance.
45+
*
46+
* @return void
47+
*/
48+
public function __construct(
49+
AuthenticatableContract $user,
50+
$subject,
51+
$from = null,
52+
$name = null
53+
)
54+
{
55+
$this->user = $user;
56+
$this->subject = $subject;
57+
$this->from = $from;
58+
$this->name = $name;
59+
}
60+
61+
/**
62+
* Build the message.
63+
*
64+
* @return $this
65+
*/
66+
public function build()
67+
{
68+
if (! empty($this->from)) {
69+
$this->from($this->from, $this->name);
70+
}
71+
72+
$this->subject($this->subject);
73+
74+
if (config('user-verification.email.type') == 'markdown') {
75+
$this->markdown('laravel-user-verification::email-markdown');
76+
} else {
77+
$this->view('laravel-user-verification::email');
78+
}
79+
80+
return $this;
81+
}
82+
}

src/UserVerification.php

+32-156
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
use Illuminate\Database\Schema\Builder;
1212
use Illuminate\Support\Facades\DB;
1313
use Illuminate\Support\Str;
14+
use Jrean\UserVerification\Mail\VerificationTokenGenerated;
1415
use Jrean\UserVerification\Events\UserVerified;
1516
use Jrean\UserVerification\Events\VerificationEmailSent;
1617
use Jrean\UserVerification\Exceptions\ModelNotCompliantException;
17-
use Jrean\UserVerification\Exceptions\UserNotFoundException;
18-
use Jrean\UserVerification\Exceptions\UserIsVerifiedException;
1918
use Jrean\UserVerification\Exceptions\TokenMismatchException;
19+
use Jrean\UserVerification\Exceptions\UserIsVerifiedException;
20+
use Jrean\UserVerification\Exceptions\UserNotFoundException;
2021

2122
class UserVerification
2223
{
@@ -34,13 +35,6 @@ class UserVerification
3435
*/
3536
protected $schema;
3637

37-
/**
38-
* E-mail view name.
39-
*
40-
* @var string
41-
*/
42-
protected $emailView;
43-
4438
/**
4539
* Create a new instance.
4640
*
@@ -50,9 +44,8 @@ class UserVerification
5044
*/
5145
public function __construct(MailerContract $mailer, Builder $schema)
5246
{
53-
$this->mailer = $mailer;
54-
$this->schema = $schema;
55-
$this->emailView = 'laravel-user-verification::email';
47+
$this->mailer = $mailer;
48+
$this->schema = $schema;
5649
}
5750

5851
/**
@@ -109,13 +102,17 @@ protected function saveToken(AuthenticatableContract $user, $token)
109102
*
110103
* @throws \Jrean\UserVerification\Exceptions\ModelNotCompliantException
111104
*/
112-
public function send(AuthenticatableContract $user, $subject = null, $from = null, $name = null)
105+
public function send(AuthenticatableContract $user, $subject, $from = null, $name = null)
113106
{
114107
if (! $this->isCompliant($user)) {
115108
throw new ModelNotCompliantException();
116109
}
117110

118-
return (boolean) $this->emailVerificationLink($user, $subject, $from, $name);
111+
$status = (boolean) $this->emailVerificationLink($user, $subject, $from, $name);
112+
113+
if ($status) event(new VerificationEmailSent($user));
114+
115+
return $status;
119116
}
120117

121118
/**
@@ -129,34 +126,17 @@ public function send(AuthenticatableContract $user, $subject = null, $from = nul
129126
*
130127
* @throws \Jrean\UserVerification\Exceptions\ModelNotCompliantException
131128
*/
132-
public function sendQueue(AuthenticatableContract $user, $subject = null, $from = null, $name = null)
129+
public function sendQueue(AuthenticatableContract $user, $subject, $from = null, $name = null)
133130
{
134131
if (! $this->isCompliant($user)) {
135132
throw new ModelNotCompliantException();
136133
}
137134

138-
return (boolean) $this->emailQueueVerificationLink($user, $subject, $from, $name);
139-
}
135+
$status = (boolean) $this->emailQueueVerificationLink($user, $subject, $from, $name);
140136

141-
/**
142-
* Queue on the given queue and send by e-mail a link containing the verification token.
143-
*
144-
* @param string $queue
145-
* @param \Illuminate\Contracts\Auth\Authenticatable $user
146-
* @param string $subject
147-
* @param string $from
148-
* @param string $name
149-
* @return bool
150-
*
151-
* @throws \Jrean\UserVerification\Exceptions\ModelNotCompliantException
152-
*/
153-
public function sendQueueOn($queue, AuthenticatableContract $user, $subject = null, $from = null, $name = null)
154-
{
155-
if (! $this->isCompliant($user)) {
156-
throw new ModelNotCompliantException();
157-
}
137+
if ($status) event(new VerificationEmailSent($user));
158138

159-
return (boolean) $this->emailQueueOnVerificationLink($queue, $user, $subject, $from, $name);
139+
return $status;
160140
}
161141

162142
/**
@@ -171,48 +151,17 @@ public function sendQueueOn($queue, AuthenticatableContract $user, $subject = nu
171151
*
172152
* @throws \Jrean\UserVerification\Exceptions\ModelNotCompliantException
173153
*/
174-
public function sendLater($seconds, AuthenticatableContract $user, $subject = null, $from = null, $name = null)
154+
public function sendLater($seconds, AuthenticatableContract $user, $subject, $from = null, $name = null)
175155
{
176156
if (! $this->isCompliant($user)) {
177157
throw new ModelNotCompliantException();
178158
}
179159

180-
return (boolean) $this->emailLaterVerificationLink($seconds, $user, $subject, $from, $name);
181-
}
160+
$status = (boolean) $this->emailLaterVerificationLink($seconds, $user, $subject, $from, $name);
182161

183-
/**
184-
* Send later on the given queue by e-mail a link containing the verification token.
185-
*
186-
* @param string $queue
187-
* @param int $seconds
188-
* @param \Illuminate\Contracts\Auth\Authenticatable $user
189-
* @param string $subject
190-
* @param string $from
191-
* @param string $name
192-
* @return bool
193-
*
194-
* @throws \Jrean\UserVerification\Exceptions\ModelNotCompliantException
195-
*/
196-
public function sendLaterOn($queue, $seconds, AuthenticatableContract $user, $subject = null, $from = null, $name = null)
197-
{
198-
if (! $this->isCompliant($user)) {
199-
throw new ModelNotCompliantException();
200-
}
162+
if ($status) event(new VerificationEmailSent($user));
201163

202-
return (boolean) $this->emailLaterOnVerificationLink($queue, $seconds, $user, $subject, $from, $name);
203-
}
204-
205-
/**
206-
* Set the e-mail view name.
207-
*
208-
* @param string $name
209-
* @return \Jrean\UserVerification
210-
*/
211-
public function emailView($name)
212-
{
213-
$this->emailView = $name;
214-
215-
return $this;
164+
return $status;
216165
}
217166

218167
/**
@@ -333,17 +282,9 @@ protected function updateUser($user)
333282
*/
334283
protected function emailVerificationLink(AuthenticatableContract $user, $subject, $from = null, $name = null)
335284
{
336-
return $this->mailer->send($this->emailView, compact('user'), function ($m) use ($user, $subject, $from, $name) {
337-
if (! empty($from)) {
338-
$m->from($from, $name);
339-
}
340-
341-
$m->to($user->email);
342-
343-
$m->subject(is_null($subject) ? trans('laravel-user-verification::user-verification.verification_email_subject') : $subject);
344-
345-
event(new VerificationEmailSent($user));
346-
});
285+
return $this->mailer
286+
->to($user->email)
287+
->send(new VerificationTokenGenerated($user, $subject, $from, $name));
347288
}
348289

349290
/**
@@ -357,46 +298,13 @@ protected function emailVerificationLink(AuthenticatableContract $user, $subject
357298
*/
358299
protected function emailQueueVerificationLink(AuthenticatableContract $user, $subject, $from = null, $name = null)
359300
{
360-
return $this->mailer->queue($this->emailView, compact('user'), function ($m) use ($user, $subject, $from, $name) {
361-
if (! empty($from)) {
362-
$m->from($from, $name);
363-
}
364-
365-
$m->to($user->email);
366-
367-
$m->subject(is_null($subject) ? trans('laravel-user-verification::user-verification.verification_email_subject') : $subject);
368-
369-
event(new VerificationEmailSent($user));
370-
});
301+
return $this->mailer
302+
->to($user->email)
303+
->queue(new VerificationTokenGenerated($user, $subject, $from, $name));
371304
}
372305

373306
/**
374-
* Prepare and push a job onto the given queue to send the e-mail with the verification token link.
375-
*
376-
* @param string $queue
377-
* @param \Illuminate\Contracts\Auth\Authenticatable $user
378-
* @param string $subject
379-
* @param string $from
380-
* @param string $name
381-
* @return mixed
382-
*/
383-
protected function emailQueueOnVerificationLink($queue, AuthenticatableContract $user, $subject, $from = null, $name = null)
384-
{
385-
return $this->mailer->queueOn($queue, $this->emailView, compact('user'), function ($m) use ($user, $subject, $from, $name) {
386-
if (! empty($from)) {
387-
$m->from($from, $name);
388-
}
389-
390-
$m->to($user->email);
391-
392-
$m->subject(is_null($subject) ? trans('laravel-user-verification::user-verification.verification_email_subject') : $subject);
393-
394-
event(new VerificationEmailSent($user));
395-
});
396-
}
397-
398-
/**
399-
* Prepare and send later the e-mail with the verification token link.
307+
* Prepare and delay the sending of the e-mail with the verification token link.
400308
*
401309
* @param int $seconds
402310
* @param \Illuminate\Contracts\Auth\Authenticatable $user
@@ -407,42 +315,9 @@ protected function emailQueueOnVerificationLink($queue, AuthenticatableContract
407315
*/
408316
protected function emailLaterVerificationLink($seconds, AuthenticatableContract $user, $subject, $from = null, $name = null)
409317
{
410-
return $this->mailer->later($seconds, $this->emailView, compact('user'), function ($m) use ($user, $subject, $from, $name) {
411-
if (! empty($from)) {
412-
$m->from($from, $name);
413-
}
414-
415-
$m->to($user->email);
416-
417-
$m->subject(is_null($subject) ? trans('laravel-user-verification::user-verification.verification_email_subject') : $subject);
418-
419-
event(new VerificationEmailSent($user));
420-
});
421-
}
422-
423-
/**
424-
* Prepare and send later on the given queue the e-mail with the verification token link.
425-
*
426-
* @param int $seconds
427-
* @param \Illuminate\Contracts\Auth\Authenticatable $user
428-
* @param string $subject
429-
* @param string $from
430-
* @param string $name
431-
* @return mixed
432-
*/
433-
protected function emailLaterOnVerificationLink($queue, $seconds, AuthenticatableContract $user, $subject, $from = null, $name = null)
434-
{
435-
return $this->mailer->laterOn($queue, $seconds, $this->emailView, compact('user'), function ($m) use ($user, $subject, $from, $name) {
436-
if (! empty($from)) {
437-
$m->from($from, $name);
438-
}
439-
440-
$m->to($user->email);
441-
442-
$m->subject(is_null($subject) ? trans('laravel-user-verification::user-verification.verification_email_subject') : $subject);
443-
444-
event(new VerificationEmailSent($user));
445-
});
318+
return $this->mailer
319+
->to($user->email)
320+
->later($seconds, new VerificationTokenGenerated($user, $subject, $from, $name));
446321
}
447322

448323
/**
@@ -455,8 +330,9 @@ protected function emailLaterOnVerificationLink($queue, $seconds, Authenticatabl
455330
protected function isCompliant(AuthenticatableContract $user)
456331
{
457332
return $this->hasColumn($user, 'verified')
458-
&& $this->hasColumn($user, 'verification_token') ?
459-
true : false;
333+
&& $this->hasColumn($user, 'verification_token')
334+
? true
335+
: false;
460336
}
461337

462338
/**

src/UserVerificationServiceProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public function boot()
3737
__DIR__ . '/resources/lang' => resource_path('lang/vendor/laravel-user-verification'),
3838
], 'translations');
3939

40+
// migrations
4041
$this->publishes([
4142
__DIR__ . '/resources/migrations/' => database_path('migrations')
4243
], 'migrations');
4344

44-
// configurations
45+
// config
4546
$this->publishes([
4647
__DIR__ . '/config/user-verification.php' => config_path('user-verification.php')
4748
], 'config');
48-
4949
}
5050

5151
/**

0 commit comments

Comments
 (0)