src/Controller/Internal/InternalWorkspaceController.php line 27
<?phpnamespace App\Controller\Internal;use App\Security\WorkspaceName;use App\Services\ClientService;use App\Services\WorkspaceData;use Doctrine\DBAL\Connection;use Psr\Log\LoggerInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\Routing\Annotation\Route;/*** The account API's checks on this service, under /internal/, which only our key opens* (ServiceKeySubscriber): whether the service is well, and what it holds of a workspace, for the* workspace's deletion. See docs/INTEGRATION.md.*/#[Route('/internal/v1', name: 'internal_')]final class InternalWorkspaceController extends AbstractController{private const MAX_FIELD_LENGTH = 255;/** 200 while the service and its database answer; 503 when the database does not. */#[Route('/health/', name: 'health', methods: 'GET')]public function health(Connection $connection, LoggerInterface $logger): JsonResponse{try {$connection->executeQuery('SELECT 1');} catch (\Throwable $e) {$logger->error('[internal] health: the database does not answer: ' . $e->getMessage());return $this->json(['status' => 'down', 'reason' => 'database'], 503);}return $this->json(['status' => 'ok']);}/** Counts what the workspace holds here, changing nothing. */#[Route('/workspaces/{workspace}/', name: 'workspace_held', requirements: ['workspace' => WorkspaceName::PATTERN], methods: 'GET')]public function held(string $workspace, WorkspaceData $data): JsonResponse{return $this->json(['workspace' => $workspace, 'held' => $data->count($workspace)]);}/*** Registers the workspace as a client, or updates it. Body: {"name": "Inkyfada", "website":*/#[Route('/workspaces/{workspace}/', name: 'workspace_register', requirements: ['workspace' => WorkspaceName::PATTERN], methods: 'PUT')]public function register(string $workspace, Request $request, ClientService $clientService): JsonResponse{$body = $request->getContent() === '' ? [] : json_decode($request->getContent(), true);if (!is_array($body)) {return $this->json(['error' => 'The body must be a JSON object.'], 400);}foreach (['name', 'website'] as $field) {$value = $body[$field] ?? null;if ($value !== null && (!is_string($value) || trim($value) === '' || strlen($value) > self::MAX_FIELD_LENGTH)) {return $this->json(['error' => sprintf('%s must be a text of 1 to %d characters.', $field, self::MAX_FIELD_LENGTH)], 400);}}$client = $clientService->register($workspace, $body['name'] ?? null, $body['website'] ?? null);return $this->json(['workspace' => $workspace, 'client' => $client->toArray()]);}/** Removes everything the workspace holds here, its client row included. Safe to repeat. */#[Route('/workspaces/{workspace}/', name: 'workspace_delete', requirements: ['workspace' => WorkspaceName::PATTERN], methods: 'DELETE')]public function delete(string $workspace, WorkspaceData $data, LoggerInterface $logger): JsonResponse{$deleted = $data->remove($workspace);$logger->info('[internal] workspace data removed', ['workspace' => $workspace, 'deleted' => $deleted]);return $this->json(['workspace' => $workspace, 'deleted' => $deleted]);}}