handle invalid email address

This commit is contained in:
Denis Duliçi
2023-04-24 11:02:45 +03:00
parent d1cd9a5cd4
commit c15d9349b0
11 changed files with 286 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Listeners\Email;
use App\Events\Email\InvalidEmailDetected as Event;
class DisablePersonDueToInvalidEmail
{
public function handle(Event $event): void
{
$this->disableContact($event);
$this->disableUser($event);
}
public function disableContact(Event $event): void
{
if (empty($event->contact)) {
return;
}
$event->contact->enabled = false;
$event->contact->save();
}
public function disableUser(Event $event): void
{
if (empty($event->user)) {
return;
}
$event->user->enabled = false;
$event->user->save();
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Listeners\Email;
use App\Events\Email\InvalidEmailDetected as Event;
use App\Notifications\Email\InvalidEmail;
use Illuminate\Support\Str;
class SendInvalidEmailNotification
{
public function handle(Event $event): void
{
$users = company()->users;
$this->notifyAdminsAboutInvalidContactEmail($event, $users);
$this->notifyAdminsAboutInvalidUserEmail($event, $users);
}
public function notifyAdminsAboutInvalidContactEmail(Event $event, $users): void
{
if (empty($event->contact)) {
return;
}
if ($event->contact->isCustomer() || $event->contact->isVendor() || $event->contact->isEmployee()) {
$type = trans('general.' . Str::plural($event->contact->type), 1);
} else {
$type = ucfirst($event->contact->type);
}
foreach ($users as $user) {
if ($user->cannot('read-notifications')) {
continue;
}
$user->notify(new InvalidEmail($event->email, $type, $event->error));
}
}
public function notifyAdminsAboutInvalidUserEmail(Event $event, $users): void
{
if (empty($event->user)) {
return;
}
$type = trans('general.users', 1);
foreach ($users as $user) {
if ($user->cannot('read-notifications')) {
continue;
}
if ($user->email == $event->email) {
continue;
}
$user->notify(new InvalidEmail($event->email, $type, $event->error));
}
}
}