Updated UDS env
testing/ogcore-api/pipeline/head There was a failure building this commit Details

pull/88/head
Manuel Aranda Rosales 2026-04-16 16:43:03 +02:00
parent 192d091e48
commit 4e6e57f6bc
1 changed files with 46 additions and 10 deletions

View File

@ -5,10 +5,11 @@ namespace App\Service\UDS;
use AllowDynamicProperties;
use App\Entity\OrganizationalUnit;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@ -22,19 +23,33 @@ class UDSClient
private string $scrambler;
private string $udsAPIurl;
private string $udsAuthLogin;
private string $udsAuthUsername;
private string $udsAuthPassword;
public function __construct(
private HttpClientInterface $httpClient,
private readonly EntityManagerInterface $entityManager,
#[Autowire(env: 'REMOTE_PC_URL')]
private readonly string $udsAPIurl,
#[Autowire(env: 'REMOTE_PC_AUTH_LOGIN')]
private readonly string $udsAuthLogin,
#[Autowire(env: 'REMOTE_PC_AUTH_USERNAME')]
private readonly string $udsAuthUsername,
#[Autowire(env: 'REMOTE_PC_AUTH_PASSWORD')]
private readonly string $udsAuthPassword,
private readonly LoggerInterface $logger,
private readonly KernelInterface $kernel,
)
{
$envFile = $this->kernel->getProjectDir() . DIRECTORY_SEPARATOR . 'env.json';
if (!is_file($envFile)) {
throw new \RuntimeException('Configuration file not found: ' . $envFile);
}
$config = json_decode(file_get_contents($envFile), true);
$vars = $config['vars'] ?? [];
$this->udsAPIurl = $vars['UDS_URL'] ?? throw new \RuntimeException('UDS_URL not set in env.json');
$this->udsAuthLogin = $vars['UDS_AUTH_LOGIN'] ?? throw new \RuntimeException('UDS_AUTH_LOGIN not set in env.json');
$this->udsAuthUsername = $vars['UDS_AUTH_USERNAME'] ?? throw new \RuntimeException('UDS_AUTH_USERNAME not set in env.json');
$this->udsAuthPassword = $vars['UDS_AUTH_PASSWORD'] ?? throw new \RuntimeException('UDS_AUTH_PASSWORD not set in env.json');
}
/**
@ -89,14 +104,35 @@ class UDSClient
]
]);
$statusCode = $response->getStatusCode();
$data = json_decode($response->getContent(false), true);
if ($statusCode !== 200 || empty($data['token'])) {
$this->logger->error('UDS login failed', [
'status_code' => $statusCode,
'response' => $data,
'uds_url' => $this->udsAPIurl,
'uds_auth' => $this->udsAuthLogin,
'uds_username' => $this->udsAuthUsername,
]);
throw new \RuntimeException('UDS login failed with status ' . $statusCode);
}
$this->token = $data['token'];
$this->scrambler = $data['scrambler'];
} catch (TransportExceptionInterface $e) {
throw new TransportException('Error while logging in to UDS');
$this->logger->error('UDS login transport error', [
'message' => $e->getMessage(),
'uds_url' => $this->udsAPIurl,
]);
throw new TransportException('Error while logging in to UDS: ' . $e->getMessage(), 0, $e);
} catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) {
$this->logger->error('UDS login HTTP error', [
'message' => $e->getMessage(),
'uds_url' => $this->udsAPIurl,
]);
throw new \RuntimeException('UDS login HTTP error: ' . $e->getMessage(), 0, $e);
}
}