feat: decouple logic using events

This commit is contained in:
Björn Fromme
2024-07-18 11:09:49 +02:00
parent 174ae5e5e3
commit 3a77697d9d
13 changed files with 190 additions and 27 deletions
@@ -3,17 +3,20 @@
namespace App\Controller\Admin\Application; namespace App\Controller\Admin\Application;
use App\Entity\Application; use App\Entity\Application;
use App\Event\ApplicationDeletedEvent;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; 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;
use Symfony\Component\Security\Http\Attribute\IsGranted; use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class DeleteController extends AbstractController class DeleteController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly LoggerInterface $logger private readonly LoggerInterface $logger
) { ) {
} }
@@ -26,10 +29,13 @@ class DeleteController extends AbstractController
$this->entityManager->remove($application); $this->entityManager->remove($application);
$this->entityManager->flush(); $this->entityManager->flush();
$application->getAssignment()->updateStaffingStatus(); $this->eventDispatcher->dispatch(
$this->entityManager->flush(); new ApplicationDeletedEvent($application),
ApplicationDeletedEvent::NAME
);
$this->addFlash('success', 'Die Bewerbung wurde gelöscht'); $this->addFlash('success', 'Die Bewerbung wurde gelöscht');
$this->logger->info('Delete application', [ $this->logger->info('Delete application', [
'destination' => (string) $application->getAssignment()->getDestination(), 'destination' => (string) $application->getAssignment()->getDestination(),
'teamer' => (string) $application->getTeamer(), 'teamer' => (string) $application->getTeamer(),
@@ -39,11 +39,10 @@ class DisposeController extends AbstractController
$this->entityManager->remove($application); $this->entityManager->remove($application);
$this->entityManager->flush(); $this->entityManager->flush();
// Update staffing status of related assignment $this->eventDispatcher->dispatch(
$disposition->getAssignment()->updateStaffingStatus(); new DispositionCreatedEvent($disposition),
$this->entityManager->flush(); DispositionCreatedEvent::NAME
);
$this->eventDispatcher->dispatch(new DispositionCreatedEvent($disposition), DispositionCreatedEvent::NAME);
$this->addFlash('success', 'Teamer:in wurde eingeteilt'); $this->addFlash('success', 'Teamer:in wurde eingeteilt');
$this->logger->info('Create disposition', [ $this->logger->info('Create disposition', [
@@ -36,7 +36,6 @@ class StatusController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$this->eventDispatcher->dispatch(new ApplicationStatusEvent($statusDto), ApplicationStatusEvent::NAME);
$application $application
->setStatus($statusDto->getStatus()) ->setStatus($statusDto->getStatus())
->setComment($statusDto->getComment()) ->setComment($statusDto->getComment())
@@ -44,6 +43,11 @@ class StatusController extends AbstractController
; ;
$this->entityManager->flush(); $this->entityManager->flush();
$this->eventDispatcher->dispatch(
new ApplicationStatusEvent($statusDto),
ApplicationStatusEvent::NAME
);
$this->addFlash('success', 'Der Status der Bewerbung wurde aktualisiert'); $this->addFlash('success', 'Der Status der Bewerbung wurde aktualisiert');
$this->logger->info('Update application status', [ $this->logger->info('Update application status', [
'application_id' => $application->getId(), 'application_id' => $application->getId(),
@@ -43,8 +43,6 @@ class DeleteController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$this->eventDispatcher->dispatch(new DispositionDeletedEvent($disposition), DispositionDeletedEvent::NAME);
$this->addFlash('success', 'Die Einteilung wurde gelöscht'); $this->addFlash('success', 'Die Einteilung wurde gelöscht');
$this->logger->info('Delete disposition', [ $this->logger->info('Delete disposition', [
'disposition_id' => $disposition->getId(), 'disposition_id' => $disposition->getId(),
@@ -54,9 +52,10 @@ class DeleteController extends AbstractController
$this->entityManager->remove($disposition); $this->entityManager->remove($disposition);
$this->entityManager->flush(); $this->entityManager->flush();
// Update staffing status of related assignment $this->eventDispatcher->dispatch(
$disposition->getAssignment()->updateStaffingStatus(); new DispositionDeletedEvent($disposition),
$this->entityManager->flush(); DispositionDeletedEvent::NAME
);
return new HxRedirectResponse($this->generateUrl('app_administrative_assignment_detail', [ return new HxRedirectResponse($this->generateUrl('app_administrative_assignment_detail', [
'uuid' => $disposition->getAssignment()->getUuid(), 'uuid' => $disposition->getAssignment()->getUuid(),
@@ -4,6 +4,7 @@ namespace App\Controller\Administrative\Document;
use App\Controller\Traits\ReturnUrlTrait; use App\Controller\Traits\ReturnUrlTrait;
use App\Entity\Upload; use App\Entity\Upload;
use App\Event\DocumentConfirmedEvent;
use App\Event\DocumentRejectedEvent; use App\Event\DocumentRejectedEvent;
use App\Form\DocumentCheckType; use App\Form\DocumentCheckType;
use App\Htmx\HxRedirectResponse; use App\Htmx\HxRedirectResponse;
@@ -46,16 +47,20 @@ class CheckController extends AbstractController
switch ($formData->getStatus()) { switch ($formData->getStatus()) {
case Upload::STATUS_CHECKED: case Upload::STATUS_CHECKED:
$this->confirmDocument($document, 'confirm_contract', Upload::STATUS_CHECKED, $formData); $this->confirmDocument($document, 'confirm_contract', Upload::STATUS_CHECKED, $formData);
// Contract confirmed: Update staffing status of related assignment $this->eventDispatcher->dispatch(
$document->getDisposition()->getAssignment()->updateStaffingStatus(); new DocumentConfirmedEvent($document),
$this->entityManager->flush(); DocumentConfirmedEvent::NAME
);
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);
break; break;
case Upload::STATUS_REJECTED: case Upload::STATUS_REJECTED:
$this->rejectDocument($document, $formData->getComment()); $this->rejectDocument($document, $formData->getComment());
$this->eventDispatcher->dispatch(new DocumentRejectedEvent($formData), DocumentRejectedEvent::NAME); $this->eventDispatcher->dispatch(
new DocumentRejectedEvent($formData),
DocumentRejectedEvent::NAME
);
break; break;
default: default:
$document $document
@@ -5,6 +5,7 @@ namespace App\Controller\Teamer\Application;
use App\Entity\Application; use App\Entity\Application;
use App\Entity\Assignment; use App\Entity\Assignment;
use App\Entity\User; use App\Entity\User;
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 Psr\Log\LoggerInterface;
@@ -13,11 +14,13 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted; use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class CreateController extends AbstractController class CreateController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly LoggerInterface $logger private readonly LoggerInterface $logger
) { ) {
} }
@@ -39,9 +42,10 @@ class CreateController extends AbstractController
$this->entityManager->persist($application); $this->entityManager->persist($application);
$this->entityManager->flush(); $this->entityManager->flush();
// Update staffing status of related assignment $this->eventDispatcher->dispatch(
$assignment->updateStaffingStatus(); new ApplicationCreatedEvent($application),
$this->entityManager->flush(); ApplicationCreatedEvent::NAME
);
$this->addFlash('success', 'Deine Bewerbung wurde entgegengenommen'); $this->addFlash('success', 'Deine Bewerbung wurde entgegengenommen');
$this->logger->info('Create application', [ $this->logger->info('Create application', [
@@ -5,6 +5,7 @@ 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\Entity\User;
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 Psr\Log\LoggerInterface;
@@ -13,6 +14,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted; use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class WithdrawController extends AbstractController class WithdrawController extends AbstractController
{ {
@@ -20,6 +22,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 LoggerInterface $logger private readonly LoggerInterface $logger
) { ) {
} }
@@ -37,9 +40,10 @@ class WithdrawController extends AbstractController
$this->entityManager->remove($application); $this->entityManager->remove($application);
$this->entityManager->flush(); $this->entityManager->flush();
// Update staffing status of related assignment $this->eventDispatcher->dispatch(
$assignment->updateStaffingStatus(); new ApplicationDeletedEvent($application),
$this->entityManager->flush(); ApplicationDeletedEvent::NAME
);
$this->logger->info('Withdraw application', [ $this->logger->info('Withdraw application', [
'teamer_id' => $teamer->getId(), 'teamer_id' => $teamer->getId(),
+3 -2
View File
@@ -136,6 +136,7 @@ class Assignment implements BlameableEntityInterface, TimestampableEntityInterfa
public function updateStaffingStatus(): void public function updateStaffingStatus(): void
{ {
$confirmedDispositions = $this->getConfirmedDispositions(); $confirmedDispositions = $this->getConfirmedDispositions();
$validApplications = $this->getValidApplications();
if ($this->availableDispositions === $confirmedDispositions->count()) { if ($this->availableDispositions === $confirmedDispositions->count()) {
$this->setStaffingStatus(Assignment::STATUS_STAFFED); $this->setStaffingStatus(Assignment::STATUS_STAFFED);
@@ -145,8 +146,8 @@ class Assignment implements BlameableEntityInterface, TimestampableEntityInterfa
) { ) {
$this->setStaffingStatus(Assignment::STATUS_PARTLY_STAFFED); $this->setStaffingStatus(Assignment::STATUS_PARTLY_STAFFED);
} elseif ( } elseif (
$this->availableDispositions > $this->dispositions->count() $this->availableDispositions > $confirmedDispositions->count()
&& 0 < $this->applications->count() && 0 < $validApplications->count()
) { ) {
$this->setStaffingStatus(Assignment::STATUS_STAFFING); $this->setStaffingStatus(Assignment::STATUS_STAFFING);
} else { } else {
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace App\Event;
use App\Entity\Application;
use Symfony\Contracts\EventDispatcher\Event;
class ApplicationCreatedEvent extends Event
{
public const NAME = 'application.created';
public function __construct(private readonly Application $application)
{}
public function getApplication(): Application
{
return $this->application;
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace App\Event;
use App\Entity\Application;
use Symfony\Contracts\EventDispatcher\Event;
class ApplicationDeletedEvent extends Event
{
public const NAME = 'application.deleted';
public function __construct(private readonly Application $application)
{}
public function getApplication(): Application
{
return $this->application;
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace App\Event;
use App\Entity\Upload;
use Symfony\Contracts\EventDispatcher\Event;
class DocumentConfirmedEvent extends Event
{
public const NAME = 'document.confirmed';
public function __construct(private readonly Upload $document)
{
}
public function getDocument(): Upload
{
return $this->document;
}
}
@@ -0,0 +1,82 @@
<?php
namespace App\EventListener;
use App\Entity\Assignment;
use App\Event\ApplicationCreatedEvent;
use App\Event\ApplicationDeletedEvent;
use App\Event\ApplicationStatusEvent;
use App\Event\DispositionCreatedEvent;
use App\Event\DispositionDeletedEvent;
use App\Event\DocumentConfirmedEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class StaffingStatusSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}
public static function getSubscribedEvents(): array
{
return [
ApplicationCreatedEvent::NAME => 'onApplicationCreated',
ApplicationDeletedEvent::NAME => 'onApplicationDeleted',
ApplicationStatusEvent::NAME => 'onApplicationStatusUpdated',
DispositionCreatedEvent::NAME => 'onDispositionCreated',
DispositionDeletedEvent::NAME => 'onDispositionDeleted',
DocumentConfirmedEvent::NAME => 'onDocumentConfirmed',
];
}
public function onApplicationCreated(ApplicationCreatedEvent $event): void
{
$application = $event->getApplication();
$application->getAssignment()->updateStaffingStatus();
$this->entityManager->flush();
}
public function onApplicationDeleted(ApplicationDeletedEvent $event): void
{
$assignment = $event->getApplication()->getAssignment();
$this->updateStaffingStatus($assignment);
}
public function onApplicationStatusUpdated(ApplicationStatusEvent $event): void
{
$assignment = $event->getApplication()->getAssignment();
$this->updateStaffingStatus($assignment);
}
public function onDispositionCreated(DispositionCreatedEvent $event): void
{
$assignment = $event->getDisposition()->getAssignment();
$this->updateStaffingStatus($assignment);
}
public function onDispositionDeleted(DispositionDeletedEvent $event): void
{
$assignment = $event->getDisposition()->getAssignment();
$this->updateStaffingStatus($assignment);
}
public function onDocumentConfirmed(DocumentConfirmedEvent $event): void
{
$assignment = $event->getDocument()->getDisposition()->getAssignment();
$this->updateStaffingStatus($assignment);
}
private function updateStaffingStatus(Assignment $assignment): void
{
$assignment->updateStaffingStatus();
$this->entityManager->flush();
}
}
+2 -1
View File
@@ -37,9 +37,10 @@ class AssignmentRepository extends ServiceEntityRepository
->innerJoin('assignment.destination', 'destination') ->innerJoin('assignment.destination', 'destination')
->innerJoin('assignment.jobProfile', 'job_profile') ->innerJoin('assignment.jobProfile', 'job_profile')
->where($qb->expr()->isNull('assignment.deletedAt')) ->where($qb->expr()->isNull('assignment.deletedAt'))
->leftJoin('assignment.applications', 'application') ->leftJoin('assignment.applications', 'application', Join::WITH, $qb->expr()->neq('application.status', ':applicationStatus'))
->leftJoin('assignment.dispositions', 'disposition') ->leftJoin('assignment.dispositions', 'disposition')
->leftJoin('disposition.teamer', 'teamer') ->leftJoin('disposition.teamer', 'teamer')
->setParameter('applicationStatus', Application::STATUS_REJECTED)
; ;
$this->applyFilterSettings($filterDto, $qb); $this->applyFilterSettings($filterDto, $qb);