Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor controllers using ADR pattern #272

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Controller/Dlc/CreateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Controller\Dlc;

use App\Form\DataTransformerRegistry;
use App\Form\Dlc\DlcFormType;
use App\Form\Dlc\Dto\DlcFormDto;
use App\Security\Enum\PermissionsEnum;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class CreateAction extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
private DataTransformerRegistry $dataTransformerRegistry
) {
}

#[Route('/dlc/create', name: 'app_dlc_create')]
#[IsGranted(PermissionsEnum::DLC_CREATE)]
public function __invoke(Request $request): Response
{
$dlcFormDto = new DlcFormDto();
$form = $this->createForm(DlcFormType::class, $dlcFormDto);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$dlc = $this->dataTransformerRegistry->transformToEntity($dlcFormDto);

$this->entityManager->persist($dlc);
$this->entityManager->flush();

return $this->redirectToRoute('app_dlc_list');
}

return $this->render('dlc/form.html.twig', [
'form' => $form->createView(),
]);
}
}
31 changes: 31 additions & 0 deletions src/Controller/Dlc/DeleteAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Controller\Dlc;

use App\Entity\Dlc\Dlc;
use App\Security\Enum\PermissionsEnum;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DeleteAction extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
) {
}

#[Route('/dlc/{id}/delete', name: 'app_dlc_delete')]
#[IsGranted(PermissionsEnum::DLC_DELETE, 'dlc')]
public function __invoke(Dlc $dlc): Response
{
$this->entityManager->remove($dlc);
$this->entityManager->flush();

return $this->redirectToRoute('app_dlc_list');
}
}
31 changes: 31 additions & 0 deletions src/Controller/Dlc/ListAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Controller\Dlc;

use App\Repository\Dlc\DlcRepository;
use App\Security\Enum\PermissionsEnum;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ListAction extends AbstractController
{
public function __construct(
private DlcRepository $dlcRepository
) {
}

#[Route('/dlc/list', name: 'app_dlc_list')]
#[IsGranted(PermissionsEnum::DLC_LIST)]
public function __invoke(): Response
{
$dlcs = $this->dlcRepository->findBy([], ['name' => 'ASC']);

return $this->render('dlc/list.html.twig', [
'dlcs' => $dlcs,
]);
}
}
47 changes: 47 additions & 0 deletions src/Controller/Dlc/UpdateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Controller\Dlc;

use App\Entity\Dlc\Dlc;
use App\Form\DataTransformerRegistry;
use App\Form\Dlc\DlcFormType;
use App\Form\Dlc\Dto\DlcFormDto;
use App\Security\Enum\PermissionsEnum;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class UpdateAction extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
private DataTransformerRegistry $dataTransformerRegistry
) {
}

#[Route('/dlc/{id}/update', name: 'app_dlc_update')]
#[IsGranted(PermissionsEnum::DLC_UPDATE, 'dlc')]
public function __invoke(Request $request, Dlc $dlc): Response
{
$dlcFormDto = $this->dataTransformerRegistry->transformFromEntity(new DlcFormDto(), $dlc);
$form = $this->createForm(DlcFormType::class, $dlcFormDto);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->dataTransformerRegistry->transformToEntity($dlcFormDto, $dlc);

$this->entityManager->flush();

return $this->redirectToRoute('app_dlc_list');
}

return $this->render('dlc/form.html.twig', [
'form' => $form->createView(),
]);
}
}
109 changes: 0 additions & 109 deletions src/Controller/DlcController.php

This file was deleted.

35 changes: 35 additions & 0 deletions src/Controller/Home/IndexAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Controller\Home;

use App\Service\Mission\MissionClientInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class IndexAction extends AbstractController
{
public function __construct(
private MissionClientInterface $missionClient,
private LoggerInterface $logger
) {
}

#[Route('/', name: 'app_home_index')]
public function __invoke(): Response
{
try {
$nearestMission = $this->missionClient->getNextUpcomingMission();
} catch (\Exception $ex) {
$this->logger->warning('Could not fetch nearest mission', ['ex' => $ex]);
$nearestMission = null;
}

return $this->render('home/index/index.html.twig', [
'nearestMission' => $nearestMission,
]);
}
}
18 changes: 18 additions & 0 deletions src/Controller/Home/JoinUsAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Controller\Home;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class JoinUsAction extends AbstractController
{
#[Route('/join-us', name: 'app_home_join_us')]
public function __invoke(): Response
{
return $this->render('home/join_us/join_us.html.twig');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,24 @@

declare(strict_types=1);

namespace App\Controller;
namespace App\Controller\Home;

use App\Service\Mission\MissionClientInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/", name="app_home")
*/
class HomeController extends AbstractController
class MissionsAction extends AbstractController
{
public function __construct(
private MissionClientInterface $missionClient,
private LoggerInterface $logger
) {
}

/**
* @Route("", name="_index")
*/
public function indexAction(): Response
{
try {
$nearestMission = $this->missionClient->getNextUpcomingMission();
} catch (\Exception $ex) {
$this->logger->warning('Could not fetch nearest mission', ['ex' => $ex]);
$nearestMission = null;
}

return $this->render('home/index/index.html.twig', [
'nearestMission' => $nearestMission,
]);
}

/**
* @Route("/join-us", name="_join_us")
*/
public function joinUsAction(): Response
{
return $this->render('home/join_us/join_us.html.twig');
}

/**
* @Route("/missions", name="_missions")
*/
public function missionsAction(): Response
#[Route('/missions', name: 'app_home_missions')]
public function __invoke(): Response
{
try {
$upcomingMissions = $this->missionClient->getUpcomingMissions();
Expand Down
Loading
Loading