<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use App\Event\VirusCamp\VirusCampEvent;
use App\Event\VirusCamp\FileInfectedEvent;
use App\Service\DynamicHostService;
use App\Repository\SystemEmailRepository;
use App\Entity\SystemEmail;
use App\Service\NotificationService;
class ScanListenerSubscriber implements EventSubscriberInterface
{
public function __construct(
private DynamicHostService $dynamicHostService,
private SystemEmailRepository $systemEmailRepository,
private NotificationService $notificationService,
){}
public function scanFailedEvent(VirusCampEvent $event): void
{
$user = $event->getFrom();
$admins = $this->dynamicHostService->getMailAdmin();
$email = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::FILE_INFECTED_WITH_VIRUS]);
$emailAdmin = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::FILE_INFECTED_WITH_VIRUS_ADMIN]);
if (null !== $emailAdmin) {
foreach ($admins as $admin) {
$this->notificationService->create($emailAdmin, $admin->value, $user, null, null, null, null, true);
}
}
if (null != $email) {
$this->notificationService->create($email,$user?->getEmail(), $user, null, null, null, null, true);
}
}
public function onFileInfected(FileInfectedEvent $event): void
{
$user = $event->getFrom();
$file = $event->getFile();
$admins = $this->dynamicHostService->getMailAdmin();
$emailAdmin = $this->systemEmailRepository->findOneBy(['code' => SystemEmail::FILE_INFECTED_WITH_VIRUS_ADMIN]);
if (null !== $emailAdmin) {
foreach ($admins as $admin) {
if(!is_null($file)){
if($file instanceof FileMessage){
$this->notificationService->create($emailAdmin, $admin->value, $file?->getMessages()?->getUser(), null, null, null, null, true);
}else{
$this->notificationService->create($emailAdmin, $admin->value, $file?->getUser(), null, null, null, null, true);
}
}
}
}
}
public static function getSubscribedEvents(): array
{
return [
VirusCampEvent::NAME => 'scanFailedEvent',
FileInfectedEvent::NAME => 'onFileInfected',
];
}
}