Skip to content

Commit

Permalink
Déplace les identifiants Litteralis vers une unique variable d'enviro…
Browse files Browse the repository at this point in the history
…nnement
  • Loading branch information
florimondmanca committed Jan 8, 2025
1 parent c0534af commit bdcc2e5
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 14 deletions.
5 changes: 1 addition & 4 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)%'
Expand Down
30 changes: 30 additions & 0 deletions src/Application/Regulation/DTO/LitteralisCredentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Application\Regulation\DTO;

final class LitteralisCredentials
{
private array $credentials;

public function __construct()
{
$this->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'];
}
}
11 changes: 7 additions & 4 deletions src/Infrastructure/Litteralis/Fougeres/FougeresExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
11 changes: 7 additions & 4 deletions src/Infrastructure/Litteralis/MEL/MELExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace App\Infrastructure\Symfony\DependencyInjection;

use App\Application\Regulation\DTO\LitteralisCredentials;
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;

class LitteralisCredentialsEnvVarProcessor implements EnvVarProcessorInterface
{
public function __construct(
private readonly string $dialogOrgId,
) {
}

public function getEnv(string $prefix, string $name, \Closure $getEnv): LitteralisCredentials
{
/** @var string */
$env = $getEnv($name);

$credentials = new LitteralisCredentials();

if (!$env) {
return $credentials;
}

try {
$value = json_decode($env, associative: true, flags: JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException(
\sprintf('litteralis_credentials: value "%s" for env var %s is not valid JSON: %s', $env, $name, $e->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',
];
}
}

0 comments on commit bdcc2e5

Please sign in to comment.