feat: further decouple logic using events

This commit is contained in:
Björn Fromme
2024-07-18 12:42:02 +02:00
parent 3a77697d9d
commit a1882a4af1
10 changed files with 127 additions and 68 deletions
@@ -5,7 +5,6 @@ namespace App\Controller\Admin\Application;
use App\Entity\Application; use App\Entity\Application;
use App\Event\ApplicationDeletedEvent; use App\Event\ApplicationDeletedEvent;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
@@ -16,8 +15,7 @@ class DeleteController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher, private readonly EventDispatcherInterface $eventDispatcher
private readonly LoggerInterface $logger
) { ) {
} }
@@ -36,11 +34,6 @@ class DeleteController extends AbstractController
$this->addFlash('success', 'Die Bewerbung wurde gelöscht'); $this->addFlash('success', 'Die Bewerbung wurde gelöscht');
$this->logger->info('Delete application', [
'destination' => (string) $application->getAssignment()->getDestination(),
'teamer' => (string) $application->getTeamer(),
]);
return $this->redirectToRoute('app_administrative_assignment_detail', [ return $this->redirectToRoute('app_administrative_assignment_detail', [
'uuid' => $application->getAssignment()->getUuid(), 'uuid' => $application->getAssignment()->getUuid(),
]); ]);
@@ -8,7 +8,6 @@ use App\Event\DispositionCreatedEvent;
use App\Form\SpecialAgreementsType; use App\Form\SpecialAgreementsType;
use App\Htmx\HxRedirectResponse; use App\Htmx\HxRedirectResponse;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -20,8 +19,7 @@ class DisposeController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher, private readonly EventDispatcherInterface $eventDispatcher
private readonly LoggerInterface $logger
) { ) {
} }
@@ -45,10 +43,6 @@ class DisposeController extends AbstractController
); );
$this->addFlash('success', 'Teamer:in wurde eingeteilt'); $this->addFlash('success', 'Teamer:in wurde eingeteilt');
$this->logger->info('Create disposition', [
'disposition_id' => $disposition->getId(),
'teamer' => (string) $application->getTeamer(),
]);
$redirectUrl = $this->generateUrl('app_administrative_assignment_detail', [ $redirectUrl = $this->generateUrl('app_administrative_assignment_detail', [
'uuid' => $application->getAssignment()->getUuid(), 'uuid' => $application->getAssignment()->getUuid(),
@@ -8,7 +8,6 @@ use App\Form\ApplicationStatusType;
use App\Htmx\HxRedirectResponse; use App\Htmx\HxRedirectResponse;
use App\Model\ApplicationStatusDto; use App\Model\ApplicationStatusDto;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -20,8 +19,7 @@ class StatusController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher, private readonly EventDispatcherInterface $eventDispatcher
private readonly LoggerInterface $logger
) { ) {
} }
@@ -49,11 +47,6 @@ class StatusController extends AbstractController
); );
$this->addFlash('success', 'Der Status der Bewerbung wurde aktualisiert'); $this->addFlash('success', 'Der Status der Bewerbung wurde aktualisiert');
$this->logger->info('Update application status', [
'application_id' => $application->getId(),
'status_new' => $statusDto->getStatus(),
'teamer' => (string) $application->getTeamer(),
]);
return new HxRedirectResponse($this->generateUrl('app_administrative_assignment_detail', [ return new HxRedirectResponse($this->generateUrl('app_administrative_assignment_detail', [
'uuid' => $application->getAssignment()->getUuid(), 'uuid' => $application->getAssignment()->getUuid(),
@@ -6,7 +6,6 @@ use App\Entity\Disposition;
use App\Event\DispositionDeletedEvent; use App\Event\DispositionDeletedEvent;
use App\Htmx\HxRedirectResponse; use App\Htmx\HxRedirectResponse;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@@ -19,8 +18,7 @@ class DeleteController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher, private readonly EventDispatcherInterface $eventDispatcher
private readonly LoggerInterface $logger
) { ) {
} }
@@ -44,10 +42,6 @@ class DeleteController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$this->addFlash('success', 'Die Einteilung wurde gelöscht'); $this->addFlash('success', 'Die Einteilung wurde gelöscht');
$this->logger->info('Delete disposition', [
'disposition_id' => $disposition->getId(),
'teamer' => (string) $disposition->getTeamer(),
]);
$this->entityManager->remove($disposition); $this->entityManager->remove($disposition);
$this->entityManager->flush(); $this->entityManager->flush();
@@ -54,6 +54,10 @@ class CheckController extends AbstractController
break; break;
case Upload::STATUS_PAID: case Upload::STATUS_PAID:
$this->confirmDocument($document, 'confirm_invoice', Upload::STATUS_PAID, $formData); $this->confirmDocument($document, 'confirm_invoice', Upload::STATUS_PAID, $formData);
$this->eventDispatcher->dispatch(
new DocumentConfirmedEvent($document),
DocumentConfirmedEvent::NAME
);
break; break;
case Upload::STATUS_REJECTED: case Upload::STATUS_REJECTED:
$this->rejectDocument($document, $formData->getComment()); $this->rejectDocument($document, $formData->getComment());
@@ -8,7 +8,6 @@ use App\Entity\User;
use App\Event\FeedbackProvidedEvent; use App\Event\FeedbackProvidedEvent;
use App\Form\FeedbackType; use App\Form\FeedbackType;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -20,8 +19,7 @@ class ProvideController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher, private readonly EventDispatcherInterface $eventDispatcher
private readonly LoggerInterface $logger
) { ) {
} }
@@ -32,7 +30,6 @@ class ProvideController extends AbstractController
/** @var User $user */ /** @var User $user */
$user = $this->getUser(); $user = $this->getUser();
$assignment = $disposition->getAssignment(); $assignment = $disposition->getAssignment();
$destination = $assignment->getDestination();
if (null !== $disposition->getFeedback()) { if (null !== $disposition->getFeedback()) {
$this->addFlash('error', 'Es liegt schon ein Feedback vor.'); $this->addFlash('error', 'Es liegt schon ein Feedback vor.');
@@ -63,12 +60,10 @@ class ProvideController extends AbstractController
$this->entityManager->persist($feedback); $this->entityManager->persist($feedback);
$this->entityManager->flush(); $this->entityManager->flush();
$this->eventDispatcher->dispatch(new FeedbackProvidedEvent($feedback), FeedbackProvidedEvent::NAME); $this->eventDispatcher->dispatch(
$this->logger->info('Feedback provided', [ new FeedbackProvidedEvent($feedback),
'feedback_id' => $feedback->getId(), FeedbackProvidedEvent::NAME
'teamer' => (string) $teamer, );
'destination' => (string) $destination,
]);
$this->addFlash('success', 'Das Feedback wurde entgegengenommen'); $this->addFlash('success', 'Das Feedback wurde entgegengenommen');
@@ -8,7 +8,6 @@ use App\Entity\User;
use App\Event\ApplicationCreatedEvent; use App\Event\ApplicationCreatedEvent;
use App\Form\TeamerApplicationType; use App\Form\TeamerApplicationType;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -20,8 +19,7 @@ class CreateController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher, private readonly EventDispatcherInterface $eventDispatcher
private readonly LoggerInterface $logger
) { ) {
} }
@@ -48,13 +46,6 @@ class CreateController extends AbstractController
); );
$this->addFlash('success', 'Deine Bewerbung wurde entgegengenommen'); $this->addFlash('success', 'Deine Bewerbung wurde entgegengenommen');
$this->logger->info('Create application', [
'teamer_id' => $teamer->getId(),
'teamer_name' => (string) $teamer,
'application_id' => $application->getId(),
'assignment_id' => $assignment->getId(),
'destination' => (string) $assignment->getDestination(),
]);
return $this->redirectToRoute('app_teamer_index'); return $this->redirectToRoute('app_teamer_index');
} }
@@ -4,11 +4,9 @@ namespace App\Controller\Teamer\Application;
use App\Controller\Traits\ReturnUrlTrait; use App\Controller\Traits\ReturnUrlTrait;
use App\Entity\Application; use App\Entity\Application;
use App\Entity\User;
use App\Event\ApplicationDeletedEvent; use App\Event\ApplicationDeletedEvent;
use App\Htmx\HxRedirectResponse; use App\Htmx\HxRedirectResponse;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -22,8 +20,7 @@ class WithdrawController extends AbstractController
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher, private readonly EventDispatcherInterface $eventDispatcher
private readonly LoggerInterface $logger
) { ) {
} }
@@ -32,11 +29,6 @@ class WithdrawController extends AbstractController
public function index(Application $application, Request $request): Response public function index(Application $application, Request $request): Response
{ {
if (true === $request->isMethod('POST')) { if (true === $request->isMethod('POST')) {
/** @var User $user */
$user = $this->getUser();
$teamer = $user->getTeamer();
$assignment = $application->getAssignment();
$this->entityManager->remove($application); $this->entityManager->remove($application);
$this->entityManager->flush(); $this->entityManager->flush();
@@ -45,14 +37,6 @@ class WithdrawController extends AbstractController
ApplicationDeletedEvent::NAME ApplicationDeletedEvent::NAME
); );
$this->logger->info('Withdraw application', [
'teamer_id' => $teamer->getId(),
'teamer_name' => (string) $teamer,
'application_id' => $application->getId(),
'assignment_id' => $assignment->getId(),
'destination' => (string) $assignment->getDestination(),
]);
return new HxRedirectResponse($this->getReturnUrl($request, 'app_teamer_index')); return new HxRedirectResponse($this->getReturnUrl($request, 'app_teamer_index'));
} }
+103
View File
@@ -0,0 +1,103 @@
<?php
namespace App\EventListener;
use App\Event\ApplicationCreatedEvent;
use App\Event\ApplicationDeletedEvent;
use App\Event\ApplicationStatusEvent;
use App\Event\DispositionCreatedEvent;
use App\Event\DispositionDeletedEvent;
use App\Event\FeedbackProvidedEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LoggingSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly LoggerInterface $logger)
{
}
public static function getSubscribedEvents(): array
{
return [
ApplicationCreatedEvent::class => 'onApplicationCreated',
ApplicationDeletedEvent::NAME => 'onApplicationDeleted',
ApplicationStatusEvent::NAME => 'onApplicationStatus',
DispositionCreatedEvent::NAME => 'onDispositionCreated',
DispositionDeletedEvent::NAME => 'onDispositionDeleted',
FeedbackProvidedEvent::NAME => 'onFeedbackProvided',
];
}
public function onApplicationCreated(ApplicationCreatedEvent $event): void
{
$application = $event->getApplication();
$assignment = $application->getAssignment();
$teamer = $application->getTeamer();
$this->logger->info('Create application', [
'teamer' => $teamer->getUuid(),
'teamer_name' => (string) $teamer,
'application' => $application->getUuid(),
'assignment_id' => $assignment->getUuid(),
'destination' => (string) $assignment->getDestination(),
]);
}
public function onApplicationDeleted(ApplicationDeletedEvent $event): void
{
$application = $event->getApplication();
$assignment = $application->getAssignment();
$teamer = $application->getTeamer();
$this->logger->info('Delete application', [
'application' => $application->getUuid(),
'assignment' => $assignment->getUuid(),
'destination' => (string) $assignment->getDestination(),
'teamer' => $teamer->getUuid(),
'teamer_name' => (string) $application->getTeamer(),
]);
}
public function onApplicationStatus(ApplicationStatusEvent $event): void
{
$application = $event->getApplication();
$this->logger->info('Update application status', [
'application' => $application->getUuid(),
'status_new' => $application->getStatus(),
'teamer' => (string) $application->getTeamer(),
]);
}
public function onDispositionCreated(DispositionCreatedEvent $event): void
{
$disposition = $event->getDisposition();
$this->logger->info('Create disposition', [
'disposition' => $disposition->getUuid(),
'teamer' => (string) $disposition->getTeamer(),
]);
}
public function onDispositionDeleted(DispositionDeletedEvent $event): void
{
$disposition = $event->getDisposition();
$this->logger->info('Delete disposition', [
'disposition' => $disposition->getUuid(),
'teamer' => (string) $disposition->getTeamer(),
]);
}
public function onFeedbackProvided(FeedbackProvidedEvent $event): void
{
$feedback = $event->getFeedback();
$this->logger->info('Feedback provided', [
'feedback' => $feedback->getUuid(),
'teamer' => (string) $feedback->getTeamer(),
'destination' => $feedback->getDestinationName(),
]);
}
}
@@ -3,6 +3,7 @@
namespace App\EventListener; namespace App\EventListener;
use App\Entity\Assignment; use App\Entity\Assignment;
use App\Entity\Upload;
use App\Event\ApplicationCreatedEvent; use App\Event\ApplicationCreatedEvent;
use App\Event\ApplicationDeletedEvent; use App\Event\ApplicationDeletedEvent;
use App\Event\ApplicationStatusEvent; use App\Event\ApplicationStatusEvent;
@@ -68,7 +69,14 @@ class StaffingStatusSubscriber implements EventSubscriberInterface
public function onDocumentConfirmed(DocumentConfirmedEvent $event): void public function onDocumentConfirmed(DocumentConfirmedEvent $event): void
{ {
$assignment = $event->getDocument()->getDisposition()->getAssignment(); $document = $event->getDocument();
// Only contract documents have an impact on staffing status
if (Upload::TYPE_CONTRACT !== $document->getType()) {
return;
}
$assignment = $document->getDisposition()->getAssignment();
$this->updateStaffingStatus($assignment); $this->updateStaffingStatus($assignment);
} }