handle invalid email address
This commit is contained in:
		
							
								
								
									
										35
									
								
								app/Listeners/Email/DisablePersonDueToInvalidEmail.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								app/Listeners/Email/DisablePersonDueToInvalidEmail.php
									
									
									
									
									
										Normal 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();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										61
									
								
								app/Listeners/Email/SendInvalidEmailNotification.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								app/Listeners/Email/SendInvalidEmailNotification.php
									
									
									
									
									
										Normal 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));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user