From bdcc2e546dd3aa0e545d8ef9b3b8fbd5e7891d08 Mon Sep 17 00:00:00 2001 From: florimondmanca Date: Wed, 8 Jan 2025 10:58:05 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9place=20les=20identifiants=20Litteralis?= =?UTF-8?q?=20vers=20une=20unique=20variable=20d'environnement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/services.yaml | 5 +- .../Regulation/DTO/LitteralisCredentials.php | 30 ++++++++ .../Litteralis/Fougeres/FougeresExecutor.php | 11 +-- .../Litteralis/MEL/MELExecutor.php | 11 +-- .../Command/MELRegulationGetCommand.php | 5 +- .../LitteralisCredentialsEnvVarProcessor.php | 68 +++++++++++++++++++ 6 files changed, 116 insertions(+), 14 deletions(-) create mode 100644 src/Application/Regulation/DTO/LitteralisCredentials.php create mode 100644 src/Infrastructure/Symfony/DependencyInjection/LitteralisCredentialsEnvVarProcessor.php diff --git a/config/services.yaml b/config/services.yaml index 9f6d336ed..b2f5243ae 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -23,10 +23,7 @@ services: $jopOrgId: '%env(APP_JOP_ORG_ID)%' $featureMap: '%features%' $cifsFilterSet: '%env(cifs_filterset:default::APP_CIFS_FILTERS)%' - $melOrgId: '%env(APP_MEL_ORG_ID)%' - $melCredentials: '%env(APP_MEL_LITTERALIS_CREDENTIALS)%' # format: 'user:pass' - $fougeresOrgId: '%env(APP_FOUGERES_ORG_ID)%' - $fougeresCredentials: '%env(APP_FOUGERES_LITTERALIS_CREDENTIALS)%' # format: 'user:pass' + $litteralisCredentials: "%env(litteralis_credentials:default::APP_LITTERALIS_CREDENTIALS)%" # format: JSON object of "integration_name" => {"orgId": "...", "credentials": "user:pass"} $metabaseSiteUrl: '%env(APP_METABASE_SITE_URL)%' $metabaseSecretKey: '%env(APP_METABASE_SECRET_KEY)%' $mediaLocation: '%env(APP_MEDIA_LOCATION)%' diff --git a/src/Application/Regulation/DTO/LitteralisCredentials.php b/src/Application/Regulation/DTO/LitteralisCredentials.php new file mode 100644 index 000000000..ca211b727 --- /dev/null +++ b/src/Application/Regulation/DTO/LitteralisCredentials.php @@ -0,0 +1,30 @@ +credentials = []; + } + + public function add(string $name, string $orgId, string $credentials): void + { + $this->credentials[$name] = ['orgId' => $orgId, 'credentials' => $credentials]; + } + + public function getOrgId(string $name): string + { + return $this->credentials[$name]['orgId']; + } + + public function getCredentials(string $name): string + { + return $this->credentials[$name]['credentials']; + } +} diff --git a/src/Infrastructure/Litteralis/Fougeres/FougeresExecutor.php b/src/Infrastructure/Litteralis/Fougeres/FougeresExecutor.php index ffea2e3d9..f2ed244c0 100644 --- a/src/Infrastructure/Litteralis/Fougeres/FougeresExecutor.php +++ b/src/Infrastructure/Litteralis/Fougeres/FougeresExecutor.php @@ -4,23 +4,26 @@ namespace App\Infrastructure\Litteralis\Fougeres; +use App\Application\Regulation\DTO\LitteralisCredentials; use App\Infrastructure\IntegrationReport\Reporter; use App\Infrastructure\Litteralis\LitteralisExecutor; final class FougeresExecutor { private const INTEGRATION_NAME = 'Litteralis Fougères'; + private string $orgId; public function __construct( private LitteralisExecutor $executor, - private string $fougeresOrgId, - string $fougeresCredentials, + LitteralisCredentials $litteralisCredentials, ) { - $this->executor->configure($fougeresCredentials); + $this->orgId = $litteralisCredentials->getOrgId('fougeres'); + $credentials = $litteralisCredentials->getCredentials('fougeres'); + $this->executor->configure($credentials); } public function execute(\DateTimeInterface $laterThan, Reporter $reporter): string { - return $this->executor->execute(self::INTEGRATION_NAME, $this->fougeresOrgId, $laterThan, $reporter); + return $this->executor->execute(self::INTEGRATION_NAME, $this->orgId, $laterThan, $reporter); } } diff --git a/src/Infrastructure/Litteralis/MEL/MELExecutor.php b/src/Infrastructure/Litteralis/MEL/MELExecutor.php index c486b8991..cce042bcc 100644 --- a/src/Infrastructure/Litteralis/MEL/MELExecutor.php +++ b/src/Infrastructure/Litteralis/MEL/MELExecutor.php @@ -4,23 +4,26 @@ namespace App\Infrastructure\Litteralis\MEL; +use App\Application\Regulation\DTO\LitteralisCredentials; use App\Infrastructure\IntegrationReport\Reporter; use App\Infrastructure\Litteralis\LitteralisExecutor; final class MELExecutor { private const INTEGRATION_NAME = 'Litteralis MEL'; + private string $orgId; public function __construct( private LitteralisExecutor $executor, - private string $melOrgId, - string $melCredentials, + LitteralisCredentials $litteralisCredentials, ) { - $this->executor->configure($melCredentials); + $this->orgId = $litteralisCredentials->getOrgId('mel'); + $credentials = $litteralisCredentials->getCredentials('mel'); + $this->executor->configure($credentials); } public function execute(\DateTimeInterface $laterThan, Reporter $reporter): string { - return $this->executor->execute(self::INTEGRATION_NAME, $this->melOrgId, $laterThan, $reporter); + return $this->executor->execute(self::INTEGRATION_NAME, $this->orgId, $laterThan, $reporter); } } diff --git a/src/Infrastructure/Symfony/Command/MELRegulationGetCommand.php b/src/Infrastructure/Symfony/Command/MELRegulationGetCommand.php index 862e066e8..da52e836a 100644 --- a/src/Infrastructure/Symfony/Command/MELRegulationGetCommand.php +++ b/src/Infrastructure/Symfony/Command/MELRegulationGetCommand.php @@ -4,6 +4,7 @@ namespace App\Infrastructure\Symfony\Command; +use App\Application\Regulation\DTO\LitteralisCredentials; use App\Infrastructure\Litteralis\LitteralisClient; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; @@ -20,11 +21,11 @@ class MELRegulationGetCommand extends Command { public function __construct( private LitteralisClient $client, - string $melCredentials, + LitteralisCredentials $litteralisCredentials, ) { parent::__construct(); - $client->setCredentials($melCredentials); + $this->client->setCredentials($litteralisCredentials->getCredentials('mel')); } public function configure(): void diff --git a/src/Infrastructure/Symfony/DependencyInjection/LitteralisCredentialsEnvVarProcessor.php b/src/Infrastructure/Symfony/DependencyInjection/LitteralisCredentialsEnvVarProcessor.php new file mode 100644 index 000000000..97c26aa84 --- /dev/null +++ b/src/Infrastructure/Symfony/DependencyInjection/LitteralisCredentialsEnvVarProcessor.php @@ -0,0 +1,68 @@ +getMessage()), + previous: $e, + ); + } + + foreach ($value as $name => $val) { + if (!\is_string($name)) { + continue; + } + + if (empty($val['orgId'])) { + throw new \RuntimeException(\sprintf('litteralis_credentials: "orgId" missing or empty at key "%s"', $name)); + } + + if (empty($val['credentials'])) { + throw new \RuntimeException(\sprintf('litteralis_credentials: "credentials" missing or empty at key "%s"', $name)); + } + + $creds = $val['credentials']; + + if ($creds === '__dialog__') { + $creds = $this->dialogOrgId; + } + + $credentials->add($name, $val['orgId'], $creds); + } + + return $credentials; + } + + public static function getProvidedTypes(): array + { + return [ + 'litteralis_credentials' => 'string', + ]; + } +}