Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
48 / 48 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| WaypointsController | |
100.00% |
48 / 48 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| edit | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| remove | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| map | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
4 | |||
| info | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getImageThumbnail | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller; |
| 6 | |
| 7 | use App\Entity\Waypoint; |
| 8 | use App\Enum\UserRole; |
| 9 | use App\Form\WaypointFormType; |
| 10 | use App\Repository\WaypointRepository; |
| 11 | use App\Service\WayPointHelper; |
| 12 | use Doctrine\ORM\EntityManagerInterface; |
| 13 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 14 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 15 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 16 | use Symfony\Component\HttpFoundation\RedirectResponse; |
| 17 | use Symfony\Component\HttpFoundation\Request; |
| 18 | use Symfony\Component\HttpFoundation\Response; |
| 19 | use Symfony\Component\Routing\Attribute\Route; |
| 20 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 21 | use UnexpectedValueException; |
| 22 | |
| 23 | class WaypointsController extends AbstractController |
| 24 | { |
| 25 | public function __construct( |
| 26 | private readonly WaypointRepository $repository, |
| 27 | private readonly WayPointHelper $wayPointHelper, |
| 28 | private readonly EntityManagerInterface $entityManager |
| 29 | ) {} |
| 30 | |
| 31 | #[Route(path: '/waypoint/{id}', name: 'waypoints_edit', methods: [ |
| 32 | 'GET', |
| 33 | 'POST', |
| 34 | ])] |
| 35 | #[IsGranted('ROLE_ADMIN')] |
| 36 | public function edit( |
| 37 | Request $request, |
| 38 | Waypoint $waypoint |
| 39 | ): RedirectResponse|Response |
| 40 | { |
| 41 | $form = $this->createForm(WaypointFormType::class, $waypoint); |
| 42 | $form->handleRequest($request); |
| 43 | |
| 44 | if ($form->isSubmitted() && $form->isValid()) { |
| 45 | $waypoint = $form->getData(); |
| 46 | $this->entityManager->persist($waypoint); |
| 47 | $this->entityManager->flush(); |
| 48 | $this->addFlash('success', 'Waypoint updated!'); |
| 49 | |
| 50 | return $this->redirectToRoute('default'); |
| 51 | } |
| 52 | |
| 53 | return $this->render( |
| 54 | 'waypoint/edit.html.twig', |
| 55 | [ |
| 56 | 'form' => $form, |
| 57 | 'waypoint' => $waypoint, |
| 58 | ] |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | #[Route(path: '/waypoint-remove/{id}', name: 'waypoints_remove', methods: ['GET'])] |
| 63 | #[IsGranted('ROLE_ADMIN')] |
| 64 | public function remove( |
| 65 | Waypoint $waypoint |
| 66 | ): RedirectResponse |
| 67 | { |
| 68 | $this->entityManager->remove($waypoint); |
| 69 | |
| 70 | $this->entityManager->flush(); |
| 71 | |
| 72 | $this->addFlash('success', 'Waypoint removed!'); |
| 73 | |
| 74 | return $this->redirectToRoute('default'); |
| 75 | } |
| 76 | |
| 77 | #[Route(path: '/waypoints_map', name: 'map-waypoints', methods: ['GET'])] |
| 78 | #[IsGranted('ROLE_AGENT')] |
| 79 | public function map(Request $request): JsonResponse |
| 80 | { |
| 81 | $bounds = $request->query->get('bounds'); |
| 82 | if ($bounds) { |
| 83 | $bounds = explode(',', $bounds); |
| 84 | if (4 !== count($bounds)) { |
| 85 | throw new UnexpectedValueException('Invalid bounds'); |
| 86 | } |
| 87 | |
| 88 | $waypoints = $this->repository->findInBounds( |
| 89 | (float)$bounds[0], |
| 90 | (float)$bounds[1], |
| 91 | (float)$bounds[2], |
| 92 | (float)$bounds[3] |
| 93 | ); |
| 94 | } else { |
| 95 | $waypoints = $this->repository->findAll(); |
| 96 | } |
| 97 | |
| 98 | $wps = []; |
| 99 | |
| 100 | foreach ($waypoints as $waypoint) { |
| 101 | $w = []; |
| 102 | |
| 103 | $w['name'] = $waypoint->getName(); |
| 104 | $w['lat'] = $waypoint->getLat(); |
| 105 | $w['lng'] = $waypoint->getLon(); |
| 106 | $w['id'] = $waypoint->getId(); |
| 107 | |
| 108 | $wps[] = $w; |
| 109 | } |
| 110 | |
| 111 | return $this->json($wps); |
| 112 | } |
| 113 | |
| 114 | #[Route(path: '/waypoints_info/{id}', name: 'waypoints-info', methods: ['GET'])] |
| 115 | #[IsGranted('ROLE_ADMIN')] |
| 116 | public function info(Waypoint $waypoint): Response |
| 117 | { |
| 118 | return $this->render( |
| 119 | 'waypoint/info.html.twig', |
| 120 | [ |
| 121 | 'waypoint' => $waypoint, |
| 122 | ] |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | #[Route(path: '/waypoint_thumb/{guid:waypoint}', name: 'waypoint_thumbnail', methods: ['GET'])] |
| 127 | #[IsGranted(UserRole::AGENT->value)] |
| 128 | public function getImageThumbnail(Waypoint $waypoint): BinaryFileResponse |
| 129 | { |
| 130 | return new BinaryFileResponse($this->wayPointHelper->getThumbnailPath($waypoint->getGuid(), $waypoint->getImage() ?? '')); |
| 131 | } |
| 132 | } |