<?php
namespace App\Controller;
use App\Service\Gauge\GaugeControlManager;
use App\Service\Output\DailyStatusManager;
use App\Service\RemoteImport\RemoteImportLogManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class InternalApiController extends AbstractController
{
private GaugeControlManager $gaugeControlManager;
private ParameterBagInterface $appParams;
private DailyStatusManager $dailyStatusManager;
private RemoteImportLogManager $remoteImportLogManager;
public function __construct(
ParameterBagInterface $appParams,
GaugeControlManager $gaugeControlManager,
DailyStatusManager $dailyStatusManager,
RemoteImportLogManager $remoteImportLogManager
)
{
$this->gaugeControlManager = $gaugeControlManager;
$this->appParams = $appParams;
$this->dailyStatusManager = $dailyStatusManager;
$this->remoteImportLogManager = $remoteImportLogManager;
}
/**
* @Route("/api-internal/remote-data-control", name="gauge_remote_data_control", methods={"POST"})
*
* @param Request $request
* @return JsonResponse
*/
public function limitControl(Request $request): JsonResponse
{
if (!$this->isAuthenticated($request)) {
return new JsonResponse(['message' => 'UNAUTHORIZED'], Response::HTTP_UNAUTHORIZED);
}
try {
$requestContent = $request->getContent();
$data = json_decode($requestContent, true);
if (!is_array($data) || !is_array($data['ids']) || !isset($data['dtFrom'])) {
return new JsonResponse(['message' => 'wrong data'], Response::HTTP_BAD_REQUEST);
}
$controlFrom = new \DateTime($data['dtFrom']);
$this->gaugeControlManager->createTasks($data['ids'], $controlFrom);
return new JsonResponse(['message' => 'data accepted!'], Response::HTTP_ACCEPTED);
} catch (\Exception $exception) {
return new JsonResponse([
'status' => 'error',
'message' => $exception->getMessage()
], Response::HTTP_BAD_REQUEST);
}
return new JsonResponse(['message' => 'OK'], Response::HTTP_ACCEPTED);
}
/**
* @Route("/api-internal/remote-dataset-update", name="gauge_remote_dataset_update", methods={"POST"})
*
* @param Request $request
* @return JsonResponse
*/
public function remoteDatasetUpdate(Request $request): JsonResponse
{
if (!$this->isAuthenticated($request)) {
return new JsonResponse(['message' => 'UNAUTHORIZED'], Response::HTTP_UNAUTHORIZED);
}
try {
$requestContent = $request->getContent();
$data = json_decode($requestContent, true);
if (!is_array($data) || !is_array($data['ids']) || !isset($data['dtFrom'])) {
return new JsonResponse(['message' => 'wrong data'], Response::HTTP_BAD_REQUEST);
}
$controlFrom = new \DateTime($data['dtFrom']);
$this->dailyStatusManager->createRemoteDatasetTasks($data['ids'], $controlFrom);
return new JsonResponse(['message' => 'data accepted!'], Response::HTTP_ACCEPTED);
} catch (\Exception $exception) {
return new JsonResponse([
'status' => 'error',
'message' => $exception->getMessage()
], Response::HTTP_BAD_REQUEST);
}
return new JsonResponse(['message' => 'OK'], Response::HTTP_ACCEPTED);
}
/**
* Receive and log import results from the data aggregator.
*
* @Route("/api-internal/remote-import-result", name="remote_import_result", methods={"POST"})
*/
public function remoteImportResult(Request $request): JsonResponse
{
if (!$this->isAuthenticated($request)) {
return new JsonResponse(['message' => 'UNAUTHORIZED'], Response::HTTP_UNAUTHORIZED);
}
try {
$data = json_decode($request->getContent(), true);
if (!is_array($data)) {
return new JsonResponse(['message' => 'Invalid JSON'], Response::HTTP_BAD_REQUEST);
}
if (!isset($data['gauge_id'], $data['imported'], $data['skipped'])) {
return new JsonResponse(['message' => 'Missing required fields: gauge_id, imported, skipped'], Response::HTTP_BAD_REQUEST);
}
$userId = isset($data['user_id']) ? (int) $data['user_id'] : null;
$this->remoteImportLogManager->logResult($data, $userId);
return new JsonResponse(['message' => 'result logged'], Response::HTTP_ACCEPTED);
} catch (\InvalidArgumentException $e) {
return new JsonResponse([
'status' => 'error',
'message' => $e->getMessage()
], Response::HTTP_BAD_REQUEST);
} catch (\Exception $e) {
return new JsonResponse([
'status' => 'error',
'message' => 'Internal error: ' . $e->getMessage()
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
/**
* @param Request $request
* @return bool
*/
private function isAuthenticated(Request $request):bool
{
$token = $request->headers->get('api-key');
return $token === sha1($this->appParams->get('internal.token.salt') . date('Ymd'));
}
}