<?php
namespace App\Controller\Admin;
use App\Service\Currency\CurrencyRateService;
use App\Service\Google\GoogleService;
use App\Service\Respy\RespyService;
use App\Service\SessionService;
use App\Service\Validator\ValidatorService;
use DateTime;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class HomeController
* @package App\Controller\Admin
*/
class HomeController extends AbstractController
{
/**
* @Route("/admin", name="admin_home")
*
* @param CurrencyRateService $currencyRateService
* @param SessionService $sessionService
* @return Response
*/
public function admin(CurrencyRateService $currencyRateService, SessionService $sessionService): Response
{
$user = $sessionService->getUser();
if ($user) {
$user->setLastLoginDate(new DateTime());
$manager = $this->getDoctrine()->getManager();
$manager->persist($user);
$manager->flush();
}
return $this->render('admin/home/home.html.twig', [
'lastCurrency' => $currencyRateService->lastRate(),
]);
}
/**
* @Route("/admin/googlelatlong", name="google_latlong")
*
* @param ValidatorService $validatorService
* @param RespyService $respy
* @param GoogleService $googleService
* @return JsonResponse
*/
public function googleLatLong(ValidatorService $validatorService, RespyService $respy, GoogleService $googleService): JsonResponse
{
$constraints = new Assert\Collection([
'term' => [
new Assert\NotBlank()
]
]);
try {
$input = $validatorService->validate($constraints);
if ($response = $googleService->getGoogleAddressLatLong($input['term'])) {
$respy->success();
$respy->setData($response);
}
} catch (Exception $exception) {
$respy->setError($exception->getMessage());
$respy->setErrorCode($exception->getCode());
}
return new JsonResponse($respy->getResponse());
}
}