<?php
namespace App\Controller;
use App\Entity\Subject;
use App\Form\SearchSubjectType;
use App\Privilege\RoleFactory;
use App\Service\ButtonRedirectService;
use App\Service\NotificationFactory;
use App\Service\OpinionService;
use App\Service\SubjectCompleteFacade;
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;
/**
* @Route("/subject", name="subject")
*/
class SubjectController extends AbstractController
{
/**
* @var OpinionService
*/
private OpinionService $opinionService;
/**
* @var SubjectCompleteFacade
*/
private SubjectCompleteFacade $subjectCompleteService;
/**
* @var NotificationFactory
*/
private NotificationFactory $notificationFactory;
public function __construct(
OpinionService $opinionService,
SubjectCompleteFacade $subjectCompleteService,
NotificationFactory $notificationFactory
)
{
$this->opinionService = $opinionService;
$this->subjectCompleteService = $subjectCompleteService;
$this->notificationFactory = $notificationFactory;
}
/**
* @IsGranted(RoleFactory::IS_AUTHENTICATED_FULLY)
* @Route("/search", name="_search")
* @param Request $request
*
* @return Response
*/
public function index(Request $request): Response
{
$searchForm = $this->createForm(SearchSubjectType::class);
$searchForm->handleRequest($request);
if ($searchForm->isSubmitted() && $searchForm->isValid()) {
$subject = $this->subjectCompleteService->getSubject($searchForm->getData());
if (!$subject) {
$this->notificationFactory->addWarning('controller.subject.search.danger', [
'{{ nip }}' => $searchForm->getData()->getNip(),
]);
return $this->redirectToRoute('subject_search');
}
$buttonName = $searchForm->getClickedButton() ? $searchForm->getClickedButton()->getName() : null;
switch ($buttonName){
case SearchSubjectType::BUTTON_NAME_ADD_OPINION:
return $this->redirectToRoute('subject_add_opinion', [
'subject_nip' => $subject->getNip(),
]);
default:
return $this->redirectToRoute('subject_opinions', [
'subject_nip' => $subject->getNip(),
]);
}
}
return $this->render('subject/index.html.twig', [
'searchForm' => $searchForm->createView(),
]);
}
}