|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Models\User; |
| 6 | +use Illuminate\Http\RedirectResponse; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Illuminate\View\View; |
| 9 | + |
| 10 | +class NotificationUnsubscribeController extends Controller |
| 11 | +{ |
| 12 | + public function unsubscribe(Request $request, User $user): RedirectResponse|View |
| 13 | + { |
| 14 | + $user->update(['receives_new_plugin_notifications' => false]); |
| 15 | + |
| 16 | + if ($request->user()?->is($user)) { |
| 17 | + return redirect() |
| 18 | + ->route('customer.settings', ['tab' => 'notifications']) |
| 19 | + ->with('new-plugin-notifications-disabled', true); |
| 20 | + } |
| 21 | + |
| 22 | + return view('notifications.unsubscribed', [ |
| 23 | + 'maskedEmail' => $this->maskEmail($user->email), |
| 24 | + 'resubscribeUrl' => $this->signedResubscribeUrl($user), |
| 25 | + ]); |
| 26 | + } |
| 27 | + |
| 28 | + public function resubscribe(Request $request, User $user): RedirectResponse|View |
| 29 | + { |
| 30 | + $user->update(['receives_new_plugin_notifications' => true]); |
| 31 | + |
| 32 | + if ($request->user()?->is($user)) { |
| 33 | + return redirect() |
| 34 | + ->route('customer.settings', ['tab' => 'notifications']) |
| 35 | + ->with('new-plugin-notifications-enabled', true); |
| 36 | + } |
| 37 | + |
| 38 | + return view('notifications.resubscribed', [ |
| 39 | + 'maskedEmail' => $this->maskEmail($user->email), |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + public static function signedUnsubscribeUrl(User $user): string |
| 44 | + { |
| 45 | + return url()->signedRoute('notifications.unsubscribe', ['user' => $user]); |
| 46 | + } |
| 47 | + |
| 48 | + private function signedResubscribeUrl(User $user): string |
| 49 | + { |
| 50 | + return url()->signedRoute('notifications.resubscribe', ['user' => $user]); |
| 51 | + } |
| 52 | + |
| 53 | + private function maskEmail(string $email): string |
| 54 | + { |
| 55 | + [$local, $domain] = explode('@', $email); |
| 56 | + |
| 57 | + if (strlen($local) <= 2) { |
| 58 | + $maskedLocal = $local[0].str_repeat('*', max(1, strlen($local) - 1)); |
| 59 | + } else { |
| 60 | + $maskedLocal = $local[0].str_repeat('*', strlen($local) - 2).$local[strlen($local) - 1]; |
| 61 | + } |
| 62 | + |
| 63 | + return $maskedLocal.'@'.$domain; |
| 64 | + } |
| 65 | +} |
0 commit comments