Skip to content

Commit 718d401

Browse files
author
Pascal Baljet
committed
Refactor + publish migration only once
1 parent 31c604b commit 718d401

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

src/Http/VerifiesPendingEmails.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function verify(string $token)
2020
throw new InvalidVerificationLinkException(
2121
__('The verification link is not valid anymore.')
2222
);
23-
})->activate();
23+
})->tap(function (PendingUserEmail $pendingUserEmail) {
24+
$pendingUserEmail->activate();
25+
})->user;
2426

2527
if (config('verify-new-email.login_after_verification')) {
2628
Auth::guard()->login($user);

src/MustVerifyNewEmail.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,25 @@ public function newEmail(string $email):?PendingUserEmail
2424
return null;
2525
}
2626

27-
return tap(PendingUserEmail::create([
27+
return $this->createPendingUserEmailModel($email)->tap(function (PendingUserEmail $model) {
28+
$this->sendPendingEmailVerificationMail($model);
29+
});
30+
}
31+
32+
/**
33+
* Createsa new PendingUserModel model for the given email.
34+
*
35+
* @param string $email
36+
* @return \ProtoneMedia\LaravelVerifyNewEmail\PendingUserEmail
37+
*/
38+
public function createPendingUserEmailModel(string $email): PendingUserEmail
39+
{
40+
return PendingUserEmail::create([
2841
'user_type' => get_class($this),
2942
'user_id' => $this->getKey(),
3043
'email' => $email,
3144
'token' => Password::broker()->getRepository()->createNewToken(),
32-
]), function ($pendingUserEmail) {
33-
$this->sendPendingEmailVerificationMail($pendingUserEmail);
34-
});
45+
]);
3546
}
3647

3748
/**

src/PendingUserEmail.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
use Illuminate\Auth\Events\Verified;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Support\Facades\URL;
8+
use Illuminate\Support\Traits\Tappable;
89

910
class PendingUserEmail extends Model
1011
{
12+
use Tappable;
13+
1114
/**
1215
* This model won't be updated.
1316
*/
@@ -50,15 +53,15 @@ public function scopeForUser($query, Model $user)
5053
*/
5154
public function activate()
5255
{
53-
return tap($this->user, function ($user) {
54-
$user->email = $this->email;
55-
$user->save();
56-
$user->markEmailAsVerified();
56+
$user = $this->user;
57+
58+
$user->email = $this->email;
59+
$user->save();
60+
$user->markEmailAsVerified();
5761

58-
static::whereEmail($this->email)->get()->each->delete();
62+
static::whereEmail($this->email)->get()->each->delete();
5963

60-
event(new Verified($user));
61-
});
64+
event(new Verified($user));
6265
}
6366

6467
/**

src/ServiceProvider.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@
22

33
namespace ProtoneMedia\LaravelVerifyNewEmail;
44

5+
use Illuminate\Filesystem\Filesystem;
6+
use Illuminate\Support\Collection;
57
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
68

79
class ServiceProvider extends BaseServiceProvider
810
{
911
/**
1012
* Bootstrap the application services.
1113
*/
12-
public function boot()
14+
public function boot(Filesystem $filesystem)
1315
{
1416
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'verify-new-email');
15-
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
16-
17-
if (!class_exists('CreatePendingUserEmailsTable')) {
18-
$this->publishes([
19-
__DIR__ . '/../database/migrations/create_pending_user_emails_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_pending_user_emails_table.php'),
20-
], 'migrations');
21-
}
2217

2318
if (!config('verify-new-email.route')) {
2419
$this->loadRoutesFrom(__DIR__ . '/routes.php');
2520
}
2621

2722
if ($this->app->runningInConsole()) {
23+
$this->publishes([
24+
__DIR__ . '/../database/migrations/create_pending_user_emails_table.php.stub' => $this->getMigrationFileName($filesystem),
25+
], 'migrations');
26+
2827
$this->publishes([
2928
__DIR__ . '/../config/config.php' => config_path('verify-new-email.php'),
3029
], 'config');
@@ -37,12 +36,29 @@ public function boot()
3736
}
3837
}
3938

39+
/**
40+
* Returns existing migration file if found, else uses the current timestamp.
41+
*
42+
* @param Filesystem $filesystem
43+
* @return string
44+
*/
45+
protected function getMigrationFileName(Filesystem $filesystem): string
46+
{
47+
$timestamp = date('Y_m_d_His');
48+
49+
return Collection::make($this->app->databasePath('migrations') . DIRECTORY_SEPARATOR)
50+
->flatMap(function ($path) use ($filesystem) {
51+
return $filesystem->glob("{$path}*_create_pending_user_emails_table.php");
52+
})
53+
->push($this->app->databasePath("migrations/{$timestamp}_create_pending_user_emails_table.php"))
54+
->first();
55+
}
56+
4057
/**
4158
* Register the application services.
4259
*/
4360
public function register()
4461
{
45-
// Automatically apply the package configuration
4662
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'verify-new-email');
4763
}
4864
}

0 commit comments

Comments
 (0)