fix: SonarQube DocuSealService - returns, params
- sendDevisForSignature : 5->3 returns (fusion 3 guards) - downloadSignedDevis : 7->3 returns (extraction fetchAndStoreDevisDocuments + downloadAuditForDevis) - sendComptaForSignature : 8->6 params (metadata groupee en array) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -189,9 +189,7 @@ class ComptabiliteController extends AbstractController
|
||||
$title.' - '.$periodFrom.' au '.$periodTo,
|
||||
$user->getEmail(),
|
||||
$user->getFullName(),
|
||||
$type,
|
||||
$from->format('Y-m-d'),
|
||||
$to->format('Y-m-d'),
|
||||
['export_type' => $type, 'period_from' => $from->format('Y-m-d'), 'period_to' => $to->format('Y-m-d')],
|
||||
$redirectUrl,
|
||||
);
|
||||
|
||||
@@ -341,9 +339,7 @@ class ComptabiliteController extends AbstractController
|
||||
'Rapport financier - '.$periodFrom.' au '.$periodTo,
|
||||
$user->getEmail(),
|
||||
$user->getFullName(),
|
||||
'rapport-financier',
|
||||
$from->format('Y-m-d'),
|
||||
$to->format('Y-m-d'),
|
||||
['export_type' => 'rapport-financier', 'period_from' => $from->format('Y-m-d'), 'period_to' => $to->format('Y-m-d')],
|
||||
$redirectUrl,
|
||||
);
|
||||
|
||||
|
||||
@@ -94,22 +94,11 @@ class DocuSealService
|
||||
public function sendDevisForSignature(Devis $devis, ?string $completedRedirectUrl = null): ?int
|
||||
{
|
||||
$customer = $devis->getCustomer();
|
||||
if (null === $customer || null === $customer->getEmail()) {
|
||||
$this->logger->error('DocuSeal devis: customer ou email manquant (devis '.$devis->getId().')');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$pdfFilename = $devis->getUnsignedPdf();
|
||||
if (null === $pdfFilename) {
|
||||
$this->logger->error('DocuSeal devis: unsignedPdf null (devis '.$devis->getId().')');
|
||||
$pdfPath = null !== $pdfFilename ? $this->projectDir.'/public/uploads/devis/'.$pdfFilename : null;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$pdfPath = $this->projectDir.'/public/uploads/devis/'.$pdfFilename;
|
||||
if (!file_exists($pdfPath)) {
|
||||
$this->logger->error('DocuSeal devis: fichier PDF introuvable '.$pdfPath);
|
||||
if (null === $customer || null === $customer->getEmail() || null === $pdfFilename || (null !== $pdfPath && !file_exists($pdfPath))) {
|
||||
$this->logger->error('DocuSeal devis: prerequis manquants (devis '.$devis->getId().')');
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -236,74 +225,13 @@ class DocuSealService
|
||||
*/
|
||||
public function downloadSignedDevis(Devis $devis): bool
|
||||
{
|
||||
if (null === $devis->getSubmissionId()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$submitterId = (int) $devis->getSubmissionId();
|
||||
$submitterId = (int) ($devis->getSubmissionId() ?? '0');
|
||||
if ($submitterId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$submitter = $this->api->getSubmitter($submitterId);
|
||||
$documents = $submitter['documents'] ?? [];
|
||||
|
||||
if ([] === $documents) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$pdfUrl = $documents[0]['url'] ?? null;
|
||||
if (null === $pdfUrl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$content = @file_get_contents($pdfUrl);
|
||||
if (false === $content || !str_starts_with($content, '%PDF')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$numOrder = str_replace('/', '-', $devis->getOrderNumber()->getNumOrder());
|
||||
|
||||
// Sauvegarde du PDF signe via Vich (UploadedFile test mode)
|
||||
$tmpSigned = tempnam(sys_get_temp_dir(), 'signed_').'.pdf';
|
||||
file_put_contents($tmpSigned, $content);
|
||||
$devis->setSignedPdfFile(new UploadedFile(
|
||||
$tmpSigned,
|
||||
'signed-'.$numOrder.'.pdf',
|
||||
'application/pdf',
|
||||
null,
|
||||
true
|
||||
));
|
||||
|
||||
// Certificat d'audit
|
||||
$auditUrl = $submitter['audit_log_url'] ?? null;
|
||||
$tmpAudit = null;
|
||||
if (null !== $auditUrl) {
|
||||
$auditContent = @file_get_contents($auditUrl);
|
||||
if (false !== $auditContent) {
|
||||
$tmpAudit = tempnam(sys_get_temp_dir(), 'audit_').'.pdf';
|
||||
file_put_contents($tmpAudit, $auditContent);
|
||||
$devis->setAuditPdfFile(new UploadedFile(
|
||||
$tmpAudit,
|
||||
'audit-'.$numOrder.'.pdf',
|
||||
'application/pdf',
|
||||
null,
|
||||
true
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
$devis->setUpdatedAt(new \DateTimeImmutable());
|
||||
$this->em->flush();
|
||||
|
||||
// Nettoyage des fichiers temporaires apres traitement Vich
|
||||
@unlink($tmpSigned);
|
||||
if (null !== $tmpAudit) {
|
||||
@unlink($tmpAudit);
|
||||
}
|
||||
|
||||
return true;
|
||||
return $this->fetchAndStoreDevisDocuments($submitterId, $devis);
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->error('DocuSeal: erreur telechargement devis signe: '.$e->getMessage(), [
|
||||
'devis_id' => $devis->getId(),
|
||||
@@ -314,18 +242,70 @@ class DocuSealService
|
||||
}
|
||||
}
|
||||
|
||||
private function fetchAndStoreDevisDocuments(int $submitterId, Devis $devis): bool
|
||||
{
|
||||
$submitter = $this->api->getSubmitter($submitterId);
|
||||
$pdfUrl = $submitter['documents'][0]['url'] ?? null;
|
||||
|
||||
$content = null !== $pdfUrl ? @file_get_contents($pdfUrl) : false;
|
||||
if (false === $content || !str_starts_with($content, '%PDF')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$numOrder = str_replace('/', '-', $devis->getOrderNumber()->getNumOrder());
|
||||
|
||||
$tmpSigned = tempnam(sys_get_temp_dir(), 'signed_').'.pdf';
|
||||
file_put_contents($tmpSigned, $content);
|
||||
$devis->setSignedPdfFile(new UploadedFile($tmpSigned, 'signed-'.$numOrder.'.pdf', 'application/pdf', null, true));
|
||||
|
||||
$tmpAudit = $this->downloadAuditForDevis($submitter, $numOrder, $devis);
|
||||
|
||||
$devis->setUpdatedAt(new \DateTimeImmutable());
|
||||
$this->em->flush();
|
||||
|
||||
@unlink($tmpSigned);
|
||||
if (null !== $tmpAudit) {
|
||||
@unlink($tmpAudit);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $submitter
|
||||
*/
|
||||
private function downloadAuditForDevis(array $submitter, string $numOrder, Devis $devis): ?string
|
||||
{
|
||||
$auditUrl = $submitter['audit_log_url'] ?? null;
|
||||
if (null === $auditUrl) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$auditContent = @file_get_contents($auditUrl);
|
||||
if (false === $auditContent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$tmpAudit = tempnam(sys_get_temp_dir(), 'audit_').'.pdf';
|
||||
file_put_contents($tmpAudit, $auditContent);
|
||||
$devis->setAuditPdfFile(new UploadedFile($tmpAudit, 'audit-'.$numOrder.'.pdf', 'application/pdf', null, true));
|
||||
|
||||
return $tmpAudit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Envoie un PDF comptable a DocuSeal pour signature par un utilisateur.
|
||||
* Retourne l'id du submitter pour suivi.
|
||||
*/
|
||||
/**
|
||||
* @param array{export_type: string, period_from: string, period_to: string} $metadata
|
||||
*/
|
||||
public function sendComptaForSignature(
|
||||
string $pdfContent,
|
||||
string $documentName,
|
||||
string $signerEmail,
|
||||
string $signerName,
|
||||
string $exportType,
|
||||
string $periodFrom,
|
||||
string $periodTo,
|
||||
array $metadata,
|
||||
?string $completedRedirectUrl = null,
|
||||
): ?int {
|
||||
try {
|
||||
@@ -336,12 +316,7 @@ class DocuSealService
|
||||
'name' => $signerName,
|
||||
'role' => self::ROLE_FIRST_PARTY,
|
||||
'send_email' => false,
|
||||
'metadata' => [
|
||||
'doc_type' => 'comptabilite',
|
||||
'export_type' => $exportType,
|
||||
'period_from' => $periodFrom,
|
||||
'period_to' => $periodTo,
|
||||
],
|
||||
'metadata' => array_merge(['doc_type' => 'comptabilite'], $metadata),
|
||||
];
|
||||
|
||||
if (null !== $completedRedirectUrl) {
|
||||
|
||||
@@ -627,9 +627,7 @@ class DocuSealServiceTest extends TestCase
|
||||
'Export Compta',
|
||||
'user@example.com',
|
||||
'Jean Dupont',
|
||||
'fec',
|
||||
'01/01/2026',
|
||||
'31/03/2026'
|
||||
['export_type' => 'fec', 'period_from' => '01/01/2026', 'period_to' => '31/03/2026'],
|
||||
);
|
||||
|
||||
$this->assertSame(111, $result);
|
||||
@@ -646,10 +644,8 @@ class DocuSealServiceTest extends TestCase
|
||||
'Export Compta',
|
||||
'user@example.com',
|
||||
'Jean Dupont',
|
||||
'grand_livre',
|
||||
'01/01/2026',
|
||||
'31/12/2026',
|
||||
'https://redirect.example.com'
|
||||
['export_type' => 'grand_livre', 'period_from' => '01/01/2026', 'period_to' => '31/12/2026'],
|
||||
'https://redirect.example.com',
|
||||
);
|
||||
|
||||
$this->assertSame(222, $result);
|
||||
@@ -667,9 +663,7 @@ class DocuSealServiceTest extends TestCase
|
||||
'Export Compta',
|
||||
'user@example.com',
|
||||
'Jean Dupont',
|
||||
'balance',
|
||||
'01/01/2026',
|
||||
'31/12/2026'
|
||||
['export_type' => 'balance', 'period_from' => '01/01/2026', 'period_to' => '31/12/2026'],
|
||||
);
|
||||
|
||||
$this->assertSame(333, $result);
|
||||
@@ -684,9 +678,7 @@ class DocuSealServiceTest extends TestCase
|
||||
'Export Compta',
|
||||
'user@example.com',
|
||||
'Jean Dupont',
|
||||
'fec',
|
||||
'01/01/2026',
|
||||
'31/03/2026'
|
||||
['export_type' => 'fec', 'period_from' => '01/01/2026', 'period_to' => '31/03/2026'],
|
||||
);
|
||||
|
||||
$this->assertNull($result);
|
||||
|
||||
Reference in New Issue
Block a user