Files
myep-team/src/Controller/Teamer/Application/CreateController.php
T
2025-08-21 14:20:43 +02:00

60 lines
2.0 KiB
PHP

<?php
namespace App\Controller\Teamer\Application;
use App\Entity\Application;
use App\Entity\Assignment;
use App\Entity\User;
use App\Event\ApplicationCreatedEvent;
use App\Form\TeamerApplicationType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class CreateController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher,
) {
}
#[Route('/teamer/apply/{uuid}', name: 'app_teamer_application_create')]
#[IsGranted('ROLE_TEAMER')]
#[IsGranted('APPLY', subject: 'assignment')]
public function index(Assignment $assignment, Request $request): Response
{
/** @var User $user */
$user = $this->getUser();
$teamer = $user->getTeamer();
$application = new Application($assignment, $teamer);
$form = $this->createForm(TeamerApplicationType::class, $application);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($application);
$this->entityManager->flush();
$this->eventDispatcher->dispatch(
new ApplicationCreatedEvent($application),
ApplicationCreatedEvent::NAME
);
$this->addFlash('success', 'Deine Bewerbung wurde entgegengenommen');
return $this->redirectToRoute('app_teamer_index');
}
return $this->render('teamer/application/create.html.twig', [
'assignment' => $assignment,
'teamer' => $teamer,
'form' => $form,
]);
}
}