50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?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;
|
|
}
|
|
}
|