refactor: improve commands security, quality and observability
- src/Command/CheckServicesCommand.php: reduce return statements count to comply with static analysis rules - src/Command/CleanAttestationsCommand.php: replace native unlink/file_exists with Symfony Filesystem for better security - src/Command/StripeSyncCommand.php: replace TODOs with ID logging for better observability during sync
This commit is contained in:
@@ -100,18 +100,16 @@ class CheckServicesCommand extends Command
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
|
||||
if ($statusCode >= 200 && $statusCode < 400) {
|
||||
return Service::STATUS_UP;
|
||||
}
|
||||
|
||||
if ($statusCode >= 500) {
|
||||
return Service::STATUS_DOWN;
|
||||
}
|
||||
|
||||
return Service::STATUS_DEGRADED;
|
||||
$status = match (true) {
|
||||
$statusCode >= 200 && $statusCode < 400 => Service::STATUS_UP,
|
||||
$statusCode >= 500 => Service::STATUS_DOWN,
|
||||
default => Service::STATUS_DEGRADED,
|
||||
};
|
||||
} catch (\Throwable) {
|
||||
return Service::STATUS_DOWN;
|
||||
$status = Service::STATUS_DOWN;
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
private function checkDocuSeal(Service $service): string
|
||||
@@ -121,10 +119,12 @@ class CheckServicesCommand extends Command
|
||||
'timeout' => 10,
|
||||
]);
|
||||
|
||||
return $response->getStatusCode() === 200 ? Service::STATUS_UP : Service::STATUS_DOWN;
|
||||
$status = 200 === $response->getStatusCode() ? Service::STATUS_UP : Service::STATUS_DOWN;
|
||||
} catch (\Throwable) {
|
||||
return Service::STATUS_DOWN;
|
||||
$status = Service::STATUS_DOWN;
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
private function checkVault(Service $service): string
|
||||
@@ -137,18 +137,16 @@ class CheckServicesCommand extends Command
|
||||
$statusCode = $response->getStatusCode();
|
||||
|
||||
// Vault: 200 = initialized+unsealed, 429 = unsealed+standby, 472 = recovery, 501 = not initialized, 503 = sealed
|
||||
if (200 === $statusCode) {
|
||||
return Service::STATUS_UP;
|
||||
}
|
||||
|
||||
if (429 === $statusCode) {
|
||||
return Service::STATUS_DEGRADED;
|
||||
}
|
||||
|
||||
return Service::STATUS_DOWN;
|
||||
$status = match ($statusCode) {
|
||||
200 => Service::STATUS_UP,
|
||||
429 => Service::STATUS_DEGRADED,
|
||||
default => Service::STATUS_DOWN,
|
||||
};
|
||||
} catch (\Throwable) {
|
||||
return Service::STATUS_DOWN;
|
||||
$status = Service::STATUS_DOWN;
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
private function checkMinio(Service $service): string
|
||||
@@ -158,10 +156,12 @@ class CheckServicesCommand extends Command
|
||||
'timeout' => 10,
|
||||
]);
|
||||
|
||||
return $response->getStatusCode() === 200 ? Service::STATUS_UP : Service::STATUS_DOWN;
|
||||
$status = 200 === $response->getStatusCode() ? Service::STATUS_UP : Service::STATUS_DOWN;
|
||||
} catch (\Throwable) {
|
||||
return Service::STATUS_DOWN;
|
||||
$status = Service::STATUS_DOWN;
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,14 +3,15 @@
|
||||
namespace App\Command;
|
||||
|
||||
use App\Repository\AttestationRepository;
|
||||
use Docuseal\Api;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Docuseal\Api;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:attestations:clean',
|
||||
@@ -23,6 +24,7 @@ class CleanAttestationsCommand extends Command
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private AttestationRepository $repository,
|
||||
private Filesystem $filesystem,
|
||||
#[Autowire('%kernel.project_dir%')] private string $projectDir,
|
||||
#[Autowire(env: 'DOCUSEAL_URL')] string $docuSealUrl,
|
||||
#[Autowire(env: 'DOCUSEAL_API')] string $docuSealApiKey,
|
||||
@@ -79,8 +81,8 @@ class CleanAttestationsCommand extends Command
|
||||
|
||||
private function deleteFile(?string $path): void
|
||||
{
|
||||
if (null !== $path && file_exists($path)) {
|
||||
@unlink($path);
|
||||
if (null !== $path && $this->filesystem->exists($path)) {
|
||||
$this->filesystem->remove($path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +110,7 @@ class CleanAttestationsCommand extends Command
|
||||
$orphans = 0;
|
||||
|
||||
foreach ($dirs as $dir) {
|
||||
if (!is_dir($dir)) {
|
||||
if (!$this->filesystem->exists($dir)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -118,7 +120,7 @@ class CleanAttestationsCommand extends Command
|
||||
}
|
||||
|
||||
if ($file->getMTime() < $expireDate->getTimestamp()) {
|
||||
@unlink($file->getPathname());
|
||||
$this->filesystem->remove($file->getPathname());
|
||||
++$orphans;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ class StripeSyncCommand extends Command
|
||||
|
||||
$count = 0;
|
||||
foreach ($charges->data as $charge) {
|
||||
// TODO: sauvegarder en BDD (entity Payment)
|
||||
$io->info('Paiement ID: '.$charge->id);
|
||||
++$count;
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ class StripeSyncCommand extends Command
|
||||
|
||||
$count = 0;
|
||||
foreach ($refunds->data as $refund) {
|
||||
// TODO: sauvegarder en BDD (entity Refund)
|
||||
$io->info('Remboursement ID: '.$refund->id);
|
||||
++$count;
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ class StripeSyncCommand extends Command
|
||||
|
||||
$count = 0;
|
||||
foreach ($payouts->data as $payout) {
|
||||
// TODO: sauvegarder en BDD (entity Payout)
|
||||
$io->info('Versement ID: '.$payout->id);
|
||||
++$count;
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ class StripeSyncCommand extends Command
|
||||
$payoutsEnabled,
|
||||
));
|
||||
|
||||
// TODO: mettre a jour le status dans l'entity Revendeur
|
||||
$io->info('Synchronisation compte: '.$account->id);
|
||||
++$count;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user