src/Controller/InternalApiController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\Gauge\GaugeControlManager;
  4. use App\Service\Output\DailyStatusManager;
  5. use App\Service\RemoteImport\RemoteImportLogManager;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class InternalApiController extends AbstractController
  13. {
  14. private GaugeControlManager $gaugeControlManager;
  15. private ParameterBagInterface $appParams;
  16. private DailyStatusManager $dailyStatusManager;
  17. private RemoteImportLogManager $remoteImportLogManager;
  18. public function __construct(
  19. ParameterBagInterface $appParams,
  20. GaugeControlManager $gaugeControlManager,
  21. DailyStatusManager $dailyStatusManager,
  22. RemoteImportLogManager $remoteImportLogManager
  23. )
  24. {
  25. $this->gaugeControlManager = $gaugeControlManager;
  26. $this->appParams = $appParams;
  27. $this->dailyStatusManager = $dailyStatusManager;
  28. $this->remoteImportLogManager = $remoteImportLogManager;
  29. }
  30. /**
  31. * @Route("/api-internal/remote-data-control", name="gauge_remote_data_control", methods={"POST"})
  32. *
  33. * @param Request $request
  34. * @return JsonResponse
  35. */
  36. public function limitControl(Request $request): JsonResponse
  37. {
  38. if (!$this->isAuthenticated($request)) {
  39. return new JsonResponse(['message' => 'UNAUTHORIZED'], Response::HTTP_UNAUTHORIZED);
  40. }
  41. try {
  42. $requestContent = $request->getContent();
  43. $data = json_decode($requestContent, true);
  44. if (!is_array($data) || !is_array($data['ids']) || !isset($data['dtFrom'])) {
  45. return new JsonResponse(['message' => 'wrong data'], Response::HTTP_BAD_REQUEST);
  46. }
  47. $controlFrom = new \DateTime($data['dtFrom']);
  48. $this->gaugeControlManager->createTasks($data['ids'], $controlFrom);
  49. return new JsonResponse(['message' => 'data accepted!'], Response::HTTP_ACCEPTED);
  50. } catch (\Exception $exception) {
  51. return new JsonResponse([
  52. 'status' => 'error',
  53. 'message' => $exception->getMessage()
  54. ], Response::HTTP_BAD_REQUEST);
  55. }
  56. return new JsonResponse(['message' => 'OK'], Response::HTTP_ACCEPTED);
  57. }
  58. /**
  59. * @Route("/api-internal/remote-dataset-update", name="gauge_remote_dataset_update", methods={"POST"})
  60. *
  61. * @param Request $request
  62. * @return JsonResponse
  63. */
  64. public function remoteDatasetUpdate(Request $request): JsonResponse
  65. {
  66. if (!$this->isAuthenticated($request)) {
  67. return new JsonResponse(['message' => 'UNAUTHORIZED'], Response::HTTP_UNAUTHORIZED);
  68. }
  69. try {
  70. $requestContent = $request->getContent();
  71. $data = json_decode($requestContent, true);
  72. if (!is_array($data) || !is_array($data['ids']) || !isset($data['dtFrom'])) {
  73. return new JsonResponse(['message' => 'wrong data'], Response::HTTP_BAD_REQUEST);
  74. }
  75. $controlFrom = new \DateTime($data['dtFrom']);
  76. $this->dailyStatusManager->createRemoteDatasetTasks($data['ids'], $controlFrom);
  77. return new JsonResponse(['message' => 'data accepted!'], Response::HTTP_ACCEPTED);
  78. } catch (\Exception $exception) {
  79. return new JsonResponse([
  80. 'status' => 'error',
  81. 'message' => $exception->getMessage()
  82. ], Response::HTTP_BAD_REQUEST);
  83. }
  84. return new JsonResponse(['message' => 'OK'], Response::HTTP_ACCEPTED);
  85. }
  86. /**
  87. * Receive and log import results from the data aggregator.
  88. *
  89. * @Route("/api-internal/remote-import-result", name="remote_import_result", methods={"POST"})
  90. */
  91. public function remoteImportResult(Request $request): JsonResponse
  92. {
  93. if (!$this->isAuthenticated($request)) {
  94. return new JsonResponse(['message' => 'UNAUTHORIZED'], Response::HTTP_UNAUTHORIZED);
  95. }
  96. try {
  97. $data = json_decode($request->getContent(), true);
  98. if (!is_array($data)) {
  99. return new JsonResponse(['message' => 'Invalid JSON'], Response::HTTP_BAD_REQUEST);
  100. }
  101. if (!isset($data['gauge_id'], $data['imported'], $data['skipped'])) {
  102. return new JsonResponse(['message' => 'Missing required fields: gauge_id, imported, skipped'], Response::HTTP_BAD_REQUEST);
  103. }
  104. $userId = isset($data['user_id']) ? (int) $data['user_id'] : null;
  105. $this->remoteImportLogManager->logResult($data, $userId);
  106. return new JsonResponse(['message' => 'result logged'], Response::HTTP_ACCEPTED);
  107. } catch (\InvalidArgumentException $e) {
  108. return new JsonResponse([
  109. 'status' => 'error',
  110. 'message' => $e->getMessage()
  111. ], Response::HTTP_BAD_REQUEST);
  112. } catch (\Exception $e) {
  113. return new JsonResponse([
  114. 'status' => 'error',
  115. 'message' => 'Internal error: ' . $e->getMessage()
  116. ], Response::HTTP_INTERNAL_SERVER_ERROR);
  117. }
  118. }
  119. /**
  120. * @param Request $request
  121. * @return bool
  122. */
  123. private function isAuthenticated(Request $request):bool
  124. {
  125. $token = $request->headers->get('api-key');
  126. return $token === sha1($this->appParams->get('internal.token.salt') . date('Ymd'));
  127. }
  128. }