diff --git a/src/Application/Actions/User/NearContaminatedUsers.php b/src/Application/Actions/User/NearContaminatedUsers.php new file mode 100644 index 0000000000000000000000000000000000000000..1780babb86a1fc8e4ac1a42707b8b62181e26b92 --- /dev/null +++ b/src/Application/Actions/User/NearContaminatedUsers.php @@ -0,0 +1,68 @@ +<?php +declare(strict_types=1); + +namespace App\Application\Actions\User; +use Psr\Http\Message\ResponseInterface as Response; +use Psr\Http\Message\ServerRequestInterface as Request; +use App\Domain\User\UserNotFoundException; + +use Doctrine\ORM\EntityManager; +use Psr\Log\LoggerInterface; +use Slim\Views\Twig; +use Slim\Flash\Messages; + +/** + * Class ListUsersAction + * + * @author : moreau96u + */ +class NearContaminatedUsers extends UserAction +{ + public function __construct(LoggerInterface $logger, EntityManager $em, Twig $twig, Messages $flash){ + + parent::__construct($logger, $em, $twig, $flash); + $this->em = $em; + $this->userRepository = $em->getRepository('App\Domain\User\User'); + $this->groupRepository = $em->getRepository('App\Domain\Group\Group'); + $this->locationRepository = $em->getRepository('App\Domain\Location\Location'); + } + + protected function action(): Response + { + if(!isset($_SESSION['userId'])){ + $this->flash->addMessage('login', 'Please log in or sign up.'); + + return $this->response + ->withHeader('Location', '/login') + ->withStatus(302); + } + + $currentUser = $this->userRepository->find($_SESSION['userId']); + + $location = $this->locationRepository->find($currentUser->getLocationId()); + $userLatitude = $location->getLatitude(); + $userLongitude = $location->getLongitude(); + + $latitude_min = $userLatitude - 1 ; + $latitude_max = $userLatitude + 1 ; + $longitude_min = $userLongitude - 1 ; + $longitude_max = $userLongitude + 1 ; + + $qb = $this->em->createQueryBuilder() + ->select('u') + ->from('App\Domain\User\User', 'u') + ->where('u.contaminated = 1') + ->andWhere('u.location.latitude >= :latitude_min') + ->andWhere('u.location.latitude <= :latitude_max') + ->andWhere('u.location.longitude >= :longitude_min') + ->andWhere('u.location.longitude <= :longitude_max') + ->setParameter('latitude_min', $latitude_min) + ->setParameter('latitude_max', $latitude_max) + ->setParameter('longitude_min', $longitude_min) + ->setParameter('longitude_max', $longitude_max) + ->getQuery() + ->getResult(); + + return $this->respondWithData($qb); + } +}