Commit 3f0fe848 authored by Clouard Regis's avatar Clouard Regis
Browse files

Refactored Controller

parent 0751700b
......@@ -8,7 +8,6 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
......@@ -16,7 +15,6 @@ use App\Form\PlannerType;
use App\Entity\User;
use App\Model\CreateUser;
// TODO
class AdminController extends AbstractController
{
/**
......@@ -49,7 +47,7 @@ class AdminController extends AbstractController
/**
* @Route("/admin/add_planner", name="add_planner")
*/
public function add_user(Request $request, TranslatorInterface $translator, UserPasswordEncoderInterface $encoder, Swift_Mailer $mailer): Response
public function add_user(Request $request, TranslatorInterface $translator, UserPasswordHasherInterface $encoder, Swift_Mailer $mailer): Response
{
$planner = new User();
$formBuilder = $this->createForm(PlannerType::class, $planner, ['password' => false]);
......@@ -83,10 +81,8 @@ class AdminController extends AbstractController
/**
* @Route("/admin/change_planner_password/{id}", name="change_planner_password")
* @param int $id
* @return RedirectResponse
*/
public function password_modification(int $id, UserPasswordEncoderInterface $encoder, TranslatorInterface $translator, Swift_Mailer $mailer): RedirectResponse
public function password_modification(int $id, UserPasswordHasherInterface $encoder, TranslatorInterface $translator, Swift_Mailer $mailer): RedirectResponse
{
$user = $this->getDoctrine()->getRepository(User::class)->find($id);
$creator = new CreateUser($user, $translator);
......
......@@ -17,7 +17,6 @@ use App\Form\GpxImportType;
use App\Entity\User;
use function simplexml_load_string;
// TODO
class CourseController extends AbstractController
{
const MAX_DISTANCE_IN_METERS = 100_000;
......@@ -79,7 +78,7 @@ class CourseController extends AbstractController
public function tracks($id, TrackStatistics $statistics, Request $request): Response
{
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (!$course) { // TODO Warning
if (!$course) {
throw $this->createNotFoundException("Ce parcours n'existe pas : ".$id);
}
$courseDate = $course->getStartDate();
......@@ -145,6 +144,9 @@ class CourseController extends AbstractController
public function courseInfo($id, TrackStatistics $statistics): Response
{
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (!$course) {
throw $this->createNotFoundException('Ce parcours n\'existe pas : '.$id);
}
$courseDate = $course->getStartDate();
if ($courseDate !== null) {
$dateDiff = $courseDate->diff(new DateTime('NOW'));
......@@ -159,9 +161,6 @@ class CourseController extends AbstractController
);
}
}
if (!$course) { // TODO Test inutile puisque deka passer par $course-> getStartDate()
throw $this->createNotFoundException('Ce parcours n\'existe pas : '.$id);
}
$xml = $course->getXml();
$simplexml = simplexml_load_string($xml);
$lengths = $statistics->computeLegLengths($simplexml);
......
......@@ -5,6 +5,7 @@ namespace App\Controller;
use App\Entity\Event;
use App\Entity\ParticipantMakeEventCourse;
use App\Entity\Track;
use DateTime;
use PHPUnit\Util\Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
......@@ -18,7 +19,6 @@ use App\Model\CreateTrack;
use App\Model\CreateMissingControlPoint;
use function dirname;
// TODO
class DataController extends AbstractController
{
/**
......@@ -30,7 +30,7 @@ class DataController extends AbstractController
foreach ($courses as $course) {
$courseDate = $course->getStartDate();
if ($courseDate !== null) {
$dateDiff = $courseDate->diff(new \DateTime('NOW'));
$dateDiff = $courseDate->diff(new DateTime('NOW'));
if ($dateDiff->invert === 1) {
unset($courses[array_search($course, $courses)]);
}
......@@ -39,7 +39,7 @@ class DataController extends AbstractController
foreach ($courses as $course) {
$courseDate = $course->getEndDate();
if ($courseDate !== null) {
$dateDiff = $courseDate->diff(new \DateTime('NOW'));
$dateDiff = $courseDate->diff(new DateTime('NOW'));
if ($dateDiff->invert === 0) {
unset($courses[array_search($course, $courses)]);
}
......@@ -76,7 +76,7 @@ class DataController extends AbstractController
$response = new Response($fileContent);
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
$id . "." . "xml"
$id."."."xml"
);
$response->headers->set('Content-Disposition', $disposition);
......@@ -116,7 +116,7 @@ class DataController extends AbstractController
return $this->json(["error" => "invalid id"]);
}
$encryptVariables = json_decode(file_get_contents((dirname(__DIR__, 2) . '/config/secret/.image.keys')), true);
$encryptVariables = json_decode(file_get_contents((dirname(__DIR__, 2).'/config/secret/.image.keys')), true);
$key = $encryptVariables["image_file_key"];
$initializationVector = $encryptVariables["image_file_iv"];
$data = $this->encryptFile($key, $initializationVector, $course->getImage());
......@@ -124,7 +124,7 @@ class DataController extends AbstractController
$gzData = gzencode($data);
$response = new Response($gzData);
$compressedFilename = $id . ".bin" . ".gz";
$compressedFilename = $id.".bin".".gz";
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', 'application/octet-stream');
$disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $compressedFilename);
......@@ -147,7 +147,7 @@ class DataController extends AbstractController
$response = new Response($fileContent);
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
$id . "." . "kml"
$id."."."kml"
);
$response->headers->set('Content-Disposition', $disposition);
......
......@@ -25,7 +25,6 @@ use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
// TODO
class EventController extends AbstractController
{
private Security $security;
......@@ -67,7 +66,7 @@ class EventController extends AbstractController
public function create_event(Request $request): Response
{
$event = new Event();
$event->setCreator($this->security->getUser());
$event->setCreator((User::class)($this->security->getUser()));
$eventForm = $this->createForm(EventType::class, $event);
$eventForm->handleRequest($request);
......@@ -89,14 +88,11 @@ class EventController extends AbstractController
/**
* @Route("/my_event/details/{id}", name="my_event_details")
* @param int $id
* @return Response
*/
public function details(int $id, Request $request): Response
{
$user = $this->security->getUser();
$user = (User::class)($this->security->getUser());
$event = $this->getDoctrine()->getRepository(Event::class)->find($id);
$formsView = [];
$participants = $event->getParticipants();
$eventCourses = $event->getEventCourses();
......@@ -149,7 +145,6 @@ class EventController extends AbstractController
/**
* @Route("/my_event/details/{id}/csv", name="my_event_csv")
* @param $id
*/
public function get_my_event_csv($id)
{
......@@ -175,7 +170,7 @@ class EventController extends AbstractController
*/
public function updateEvent($id): RedirectResponse
{
$user = $this->security->getUser();
$user = (User::class)($this->security->getUser());
$event = $this->getDoctrine()->getRepository(Event::class)->find($id);
if ($event != null && (in_array($event, $user->getEvents()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles()))) {
$event->update($this->getDoctrine()->getManager(), $this->getDoctrine()->getRepository(ParticipantMakeEventCourse::class));
......@@ -193,7 +188,7 @@ class EventController extends AbstractController
*/
public function deleteEvent($id): RedirectResponse
{
$user = $this->security->getUser();
$user = (User::class)($this->security->getUser());
$entityManager = $this->getDoctrine()->getManager();
$event = $this->getDoctrine()->getRepository(Event::class)->find($id);
if ($event != null && (in_array($event, $user->getEvents()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles()))) {
......@@ -224,7 +219,6 @@ class EventController extends AbstractController
if ($participantForm->isSubmitted() && $participantForm->isValid()) {
$error = $participant->create($event, $this->getDoctrine()->getManager());
if ($error === "") {
$success = "success";
return $this->redirect($request->getUri());
}
}
......@@ -249,7 +243,7 @@ class EventController extends AbstractController
*/
public function deleteParticipant(int $idEvent, int $id): RedirectResponse
{
$user = $this->security->getUser();
$user = (User::class)($this->security->getUser());
$entityManager = $this->getDoctrine()->getManager();
$participant = $this->getDoctrine()->getRepository(Participant::class)->find($id);
$event = $this->getDoctrine()->getRepository(Event::class)->find($idEvent);
......@@ -287,7 +281,7 @@ class EventController extends AbstractController
$modifyFormsView = [];
foreach ($this->getDoctrine()->getRepository(Event::class)->find($id)->getEventCourses() as $eventCourse) {
$modifyEventCourse = new ModifyEventCourse($eventCourse);
$modifyEventCourseForm = $this->createForm(ModifyEventCourseType::class, $modifyEventCourse,);
$modifyEventCourseForm = $this->createForm(ModifyEventCourseType::class, $modifyEventCourse);
$modifyFormsView[$eventCourse->getId()] = $modifyEventCourseForm->createView();
$modifyEventCourseForm->handleRequest($request);
if ($modifyEventCourseForm->isSubmitted() && $modifyEventCourseForm->isValid() && $modifyEventCourse->getId() === $eventCourse->getId()) {
......@@ -316,7 +310,7 @@ class EventController extends AbstractController
*/
public function deleteEventCourse(int $idEvent, int $id): RedirectResponse
{
$user = $this->security->getUser();
$user = (User::class)($this->security->getUser());
$entityManager = $this->getDoctrine()->getManager();
$eventCourse = $this->getDoctrine()->getRepository(EventCourse::class)->find($id);
$event = $this->getDoctrine()->getRepository(Event::class)->find($idEvent);
......@@ -359,10 +353,7 @@ class EventController extends AbstractController
$participantForm->handleRequest($request);
if ($participantForm->isSubmitted() && $participantForm->isValid()) {
$error = $participant->create($event, $this->getDoctrine()->getManager());
if ($error == "") {
$success = "success";
}
$participant->create($event, $this->getDoctrine()->getManager());
return $this->redirect($request->getUri());
}
......
......@@ -4,6 +4,7 @@ namespace App\Controller;
use App\Entity\MissingControlPoint;
use App\Entity\Track;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\HttpFoundation\RedirectResponse;
......@@ -23,7 +24,6 @@ use App\Model\FileUploader;
use function in_array;
use function simplexml_load_string;
// TODO
class PlannerController extends AbstractController
{
private Security $security;
......@@ -58,6 +58,7 @@ class PlannerController extends AbstractController
/**
* @Route("/planner/add_course/{id}", name="add_course", defaults={"id": -1})
* @throws Exception
*/
public function add_course(int $id, TranslatorInterface $translator, UrlGeneratorInterface $router): Response
{
......@@ -115,11 +116,8 @@ class PlannerController extends AbstractController
*/
public function missing_control_point($id): Response
{
$user = $this->security->getUser();
/* $entityManager = $this->getDoctrine()->getManager();*/
$user = (User::class)($this->security->getUser());
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (in_array($course, $user->getCourses()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles())) {
$controlPoints = $course->getMissingControlPoints();
......@@ -139,7 +137,7 @@ class PlannerController extends AbstractController
*/
public function delete($id): RedirectResponse
{
$user = $this->security->getUser();
$user = (User::class)($this->security->getUser());
$entityManager = $this->getDoctrine()->getManager();
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if ($course != null && (in_array($course, $user->getCourses()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles()))) {
......@@ -156,7 +154,7 @@ class PlannerController extends AbstractController
*/
public function delete_missing_control_point($id): RedirectResponse
{
$user = $this->security->getUser();
$user = (User::class)($this->security->getUser());
$entityManager = $this->getDoctrine()->getManager();
$controlPoint = $this->getDoctrine()->getRepository(MissingControlPoint::class)->find($id);
$course = $controlPoint->getCourse();
......@@ -175,7 +173,7 @@ class PlannerController extends AbstractController
*/
public function preview($id): Response
{
$user = $this->security->getUser();
$user = (User::class)($this->security->getUser());
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (in_array($course, $user->getCourses()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles())) {
return $this->render(
......@@ -236,7 +234,8 @@ class PlannerController extends AbstractController
$success = "";
$response = new JsonResponse();
foreach ($request->files->all() as $file) {
$success = FileUploader::uploadFile($file, $this->security->getUser()->getId());
$user = (User::class)($this->security->getUser());
$success = FileUploader::uploadFile($file, $user->getId());
if (!$success) {
return $response->fromJsonString($success);
}
......@@ -247,6 +246,7 @@ class PlannerController extends AbstractController
/**
* @Route("/planner/upload_course/{id}", name="upload_course", defaults={"id": -1})
* @throws Exception
*/
public function upload_course(int $id, TranslatorInterface $translator, UrlGeneratorInterface $router): JsonResponse
{
......
......@@ -4,18 +4,17 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use App\Form\PlannerType;
use App\Entity\User;
use App\Model\CreateUser;
use function in_array;
// TODO
class ProfileController extends AbstractController
{
private Security $security;
......@@ -46,7 +45,7 @@ class ProfileController extends AbstractController
/**
* @Route("/profile/modify_planner_info", name="modify_planner_info")
*/
public function modify_planner_info(Request $request, TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder): Response
public function modify_planner_info(Request $request, TranslatorInterface $translator, UserPasswordHasherInterface $passwordEncoder): Response
{
$planner = $this->security->getUser();
$formBuilder = $this->createForm(PlannerType::class, $planner, ['password' => true]);
......
......@@ -6,18 +6,16 @@ use LogicException;
use Swift_Mailer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use App\Entity\User;
use App\Model\CreateUser;
// TODO
class SecurityController extends AbstractController
{
/**
......@@ -42,7 +40,7 @@ class SecurityController extends AbstractController
/**
* @Route("/reset-password", name="reset_password")
*/
public function reset_password(Request $request, TranslatorInterface $translator, UserPasswordEncoderInterface $encoder, Swift_Mailer $mailer)
public function reset_password(Request $request, TranslatorInterface $translator, UserPasswordHasherInterface $encoder, Swift_Mailer $mailer)
{
$form = $this->createFormBuilder()
->add('username', TextType::class, ['label' => 'user.identifier'])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment