Skip to content
Snippets Groups Projects
Commit f1d1b2d6 authored by Moreau Elise's avatar Moreau Elise
Browse files

create controler to update location in user's profile

parent 125a17cb
Branches
No related tags found
No related merge requests found
<?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\User;
use App\Domain\Location\Location;
use GeoIp2\Database\Reader;
use DateTime;
class LocationUserAction extends UserAction
{
/**
* {@inheritdoc}
*/
protected function action(): Response
{
$parsedRequestBody = (array)$this->request->getParsedBody();
$hasLatitude = $this->checkFloat($parsedRequestBody['latitude'], 90);
$hasLongitude = $this->checkFloat($parsedRequestBody['longitude'], 180);
if ((!$hasLatitude) or (!$hasLongitude))
{
$this->flash->addMessage('message', 'Please allow geolocation.');
return $this->response
->withHeader('Location', '/account')
->withStatus(302);
}
$latitude = (float) $parsedRequestBody['latitude'];
$longitude = (float) $parsedRequestBody['longitude'];
$user = $this->userRepository->find($_SESSION['userId']);
$location = $user->getLocation();
if ( $location == null)
{
$location = new Location(null, 0, 0);
}
$location->setLatitude($latitude);
$location->setLongitude($longitude);
$user->setLocation($location);
$this->em->persist($location);
$this->em->persist($user);
$this->em->flush();
$this->logger->info("Location has been updated.");
$this->flash->addMessage('creation', 'Your location is udpated.');
return $this->response
->withHeader('Location', '/account')
->withStatus(302);
}
protected function checkFloat($value, $number): bool
{
if (empty($value)) { return false; }
if (! is_numeric($value)) { return false; }
if (( (float) $value > $number) or ( (float) $value < $number * -1))
{
return false;
}
return true;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment