feat : add DatabaseInfo API resource and provider
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
49
src/State/DatabaseInfoProvider.php
Normal file
49
src/State/DatabaseInfoProvider.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\DatabaseInfo;
|
||||
use App\Repository\EnvironmentRepository;
|
||||
use App\Service\DatabaseService;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final readonly class DatabaseInfoProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EnvironmentRepository $environmentRepository,
|
||||
private DatabaseService $databaseService,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): DatabaseInfo
|
||||
{
|
||||
$id = $uriVariables['id'] ?? null;
|
||||
$environment = $id ? $this->environmentRepository->find($id) : null;
|
||||
|
||||
if (null === $environment) {
|
||||
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $id));
|
||||
}
|
||||
|
||||
$databaseName = $environment->getDatabaseName();
|
||||
|
||||
if (null === $databaseName || '' === $databaseName) {
|
||||
throw new NotFoundHttpException('No database configured for this environment.');
|
||||
}
|
||||
|
||||
$info = $this->databaseService->getDatabaseInfo($databaseName);
|
||||
|
||||
$dto = new DatabaseInfo();
|
||||
$dto->connected = $info['connected'];
|
||||
$dto->name = $info['name'];
|
||||
$dto->size = $info['size'];
|
||||
$dto->tableCount = $info['tableCount'];
|
||||
$dto->activeConnections = $info['activeConnections'];
|
||||
$dto->cacheHitRatio = $info['cacheHitRatio'];
|
||||
$dto->largestTable = $info['largestTable'];
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user