Add logging to SignatureStepStateChanger

Enhanced the SignatureStepStateChanger by integrating a LoggerInterface to provide detailed logging at key points in the state transition process. This includes informational messages when marking signatures or skipping transitions, as well as debug messages when determining the next steps.
This commit is contained in:
Julien Fastré 2024-09-11 21:14:07 +02:00
parent 70671dadac
commit 4e588ed0e0
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -15,14 +15,18 @@ use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowSignatureStateEnum; use Chill\MainBundle\Entity\Workflow\EntityWorkflowSignatureStateEnum;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep; use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepSignature; use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepSignature;
use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Workflow\Registry; use Symfony\Component\Workflow\Registry;
class SignatureStepStateChanger class SignatureStepStateChanger
{ {
private const LOG_PREFIX = '[SignatureStepStateChanger] ';
public function __construct( public function __construct(
private readonly Registry $registry, private readonly Registry $registry,
private readonly ClockInterface $clock, private readonly ClockInterface $clock,
private readonly LoggerInterface $logger,
) {} ) {}
public function markSignatureAsSigned(EntityWorkflowStepSignature $signature, ?int $atIndex): void public function markSignatureAsSigned(EntityWorkflowStepSignature $signature, ?int $atIndex): void
@ -33,10 +37,16 @@ class SignatureStepStateChanger
->setStateDate($this->clock->now()) ->setStateDate($this->clock->now())
; ;
$this->logger->info(self::LOG_PREFIX.'Mark signature entity as signed', ['signatureId' => $signature->getId(), 'index' => (string) $atIndex]);
if (!EntityWorkflowStepSignature::isAllSignatureNotPendingForStep($signature->getStep())) { if (!EntityWorkflowStepSignature::isAllSignatureNotPendingForStep($signature->getStep())) {
$this->logger->info(self::LOG_PREFIX.'This is not the last signature, skipping transition to another place', ['signatureId' => $signature->getId()]);
return; return;
} }
$this->logger->debug(self::LOG_PREFIX.'Continuing the process to find a transition', ['signatureId' => $signature->getId()]);
$entityWorkflow = $signature->getStep()->getEntityWorkflow(); $entityWorkflow = $signature->getStep()->getEntityWorkflow();
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName()); $workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
$metadataStore = $workflow->getMetadataStore(); $metadataStore = $workflow->getMetadataStore();
@ -54,12 +64,16 @@ class SignatureStepStateChanger
} }
if (null === $transition) { if (null === $transition) {
$this->logger->info(self::LOG_PREFIX.'The transition is not configured, will not apply a transition', ['signatureId' => $signature->getId()]);
return; return;
} }
$previousUser = $this->getPreviousSender($signature->getStep()); $previousUser = $this->getPreviousSender($signature->getStep());
if (null === $previousUser) { if (null === $previousUser) {
$this->logger->info(self::LOG_PREFIX.'No previous user, will not apply a transition', ['signatureId' => $signature->getId()]);
return; return;
} }
@ -71,6 +85,8 @@ class SignatureStepStateChanger
'transitionAt' => $this->clock->now(), 'transitionAt' => $this->clock->now(),
'transition' => $transition, 'transition' => $transition,
]); ]);
$this->logger->info(self::LOG_PREFIX.'Transition automatically applied', ['signatureId' => $signature->getId()]);
} }
private function getPreviousSender(EntityWorkflowStep $entityWorkflowStep): ?User private function getPreviousSender(EntityWorkflowStep $entityWorkflowStep): ?User