src/Controller/Internal/InternalWorkspaceController.php line 27

  1. <?php
  2. namespace App\Controller\Internal;
  3. use App\Security\WorkspaceName;
  4. use App\Services\ClientService;
  5. use App\Services\WorkspaceData;
  6. use Doctrine\DBAL\Connection;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * The account API's checks on this service, under /internal/, which only our key opens
  14.  * (ServiceKeySubscriber): whether the service is well, and what it holds of a workspace, for the
  15.  * workspace's deletion. See docs/INTEGRATION.md.
  16.  */
  17. #[Route('/internal/v1', name: 'internal_')]
  18. final class InternalWorkspaceController extends AbstractController
  19. {
  20.     private const MAX_FIELD_LENGTH = 255;
  21.     /** 200 while the service and its database answer; 503 when the database does not. */
  22.     #[Route('/health/', name: 'health', methods: 'GET')]
  23.     public function health(Connection $connection, LoggerInterface $logger): JsonResponse
  24.     {
  25.         try {
  26.             $connection->executeQuery('SELECT 1');
  27.         } catch (\Throwable $e) {
  28.             $logger->error('[internal] health: the database does not answer: ' . $e->getMessage());
  29.             return $this->json(['status' => 'down', 'reason' => 'database'], 503);
  30.         }
  31.         return $this->json(['status' => 'ok']);
  32.     }
  33.     /** Counts what the workspace holds here, changing nothing. */
  34.     #[Route('/workspaces/{workspace}/', name: 'workspace_held', requirements: ['workspace' => WorkspaceName::PATTERN], methods: 'GET')]
  35.     public function held(string $workspace, WorkspaceData $data): JsonResponse
  36.     {
  37.         return $this->json(['workspace' => $workspace, 'held' => $data->count($workspace)]);
  38.     }
  39.     /**
  40.      * Registers the workspace as a client, or updates it. Body: {"name": "Inkyfada", "website":
  41.      */
  42.     #[Route('/workspaces/{workspace}/', name: 'workspace_register', requirements: ['workspace' => WorkspaceName::PATTERN], methods: 'PUT')]
  43.     public function register(string $workspace, Request $request, ClientService $clientService): JsonResponse
  44.     {
  45.         $body = $request->getContent() === '' ? [] : json_decode($request->getContent(), true);
  46.         if (!is_array($body)) {
  47.             return $this->json(['error' => 'The body must be a JSON object.'], 400);
  48.         }
  49.         foreach (['name', 'website'] as $field) {
  50.             $value = $body[$field] ?? null;
  51.             if ($value !== null && (!is_string($value) || trim($value) === '' || strlen($value) > self::MAX_FIELD_LENGTH)) {
  52.                 return $this->json(['error' => sprintf('%s must be a text of 1 to %d characters.', $field, self::MAX_FIELD_LENGTH)], 400);
  53.             }
  54.         }
  55.         $client = $clientService->register($workspace, $body['name'] ?? null, $body['website'] ?? null);
  56.         return $this->json(['workspace' => $workspace, 'client' => $client->toArray()]);
  57.     }
  58.     /** Removes everything the workspace holds here, its client row included. Safe to repeat. */
  59.     #[Route('/workspaces/{workspace}/', name: 'workspace_delete', requirements: ['workspace' => WorkspaceName::PATTERN], methods: 'DELETE')]
  60.     public function delete(string $workspace, WorkspaceData $data, LoggerInterface $logger): JsonResponse
  61.     {
  62.         $deleted = $data->remove($workspace);
  63.         $logger->info('[internal] workspace data removed', ['workspace' => $workspace, 'deleted' => $deleted]);
  64.         return $this->json(['workspace' => $workspace, 'deleted' => $deleted]);
  65.     }
  66. }