From 4e6e57f6bc6805012cb8e2758b0c6c3aa6267eca Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Thu, 16 Apr 2026 16:43:03 +0200 Subject: [PATCH] Updated UDS env --- src/Service/UDS/UDSClient.php | 56 ++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/src/Service/UDS/UDSClient.php b/src/Service/UDS/UDSClient.php index 5842986..f3a1c1b 100644 --- a/src/Service/UDS/UDSClient.php +++ b/src/Service/UDS/UDSClient.php @@ -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); } }