mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-20 05:35:00 +00:00
Introduce ConvertToPdfBeforeSignatureStepEventSubscriber to convert documents to PDF when reaching a signature step in the workflow. Includes tests to ensure the conversion process only triggers when necessary.
76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\DocStoreBundle\Workflow;
|
|
|
|
use Chill\DocStoreBundle\Service\StoredObjectToPdfConverter;
|
|
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
|
use Chill\MainBundle\Workflow\EntityWorkflowManager;
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\Workflow\Event\CompletedEvent;
|
|
use Symfony\Component\Workflow\WorkflowEvents;
|
|
|
|
/**
|
|
* Event subscriber to convert objects to PDF when the document reach a signature step.
|
|
*/
|
|
class ConvertToPdfBeforeSignatureStepEventSubscriber implements EventSubscriberInterface
|
|
{
|
|
public function __construct(
|
|
private readonly EntityWorkflowManager $entityWorkflowManager,
|
|
private readonly StoredObjectToPdfConverter $storedObjectToPdfConverter,
|
|
private readonly RequestStack $requestStack,
|
|
) {}
|
|
|
|
public static function getSubscribedEvents(): array
|
|
{
|
|
return [
|
|
WorkflowEvents::COMPLETED => 'convertToPdfBeforeSignatureStepEvent',
|
|
];
|
|
}
|
|
|
|
public function convertToPdfBeforeSignatureStepEvent(CompletedEvent $event): void
|
|
{
|
|
$entityWorkflow = $event->getSubject();
|
|
if (!$entityWorkflow instanceof EntityWorkflow) {
|
|
return;
|
|
}
|
|
|
|
$tos = $event->getTransition()->getTos();
|
|
$workflow = $event->getWorkflow();
|
|
$metadataStore = $workflow->getMetadataStore();
|
|
|
|
foreach ($tos as $to) {
|
|
$metadata = $metadataStore->getPlaceMetadata($to);
|
|
if (array_key_exists('isSignature', $metadata) && 0 < count($metadata['isSignature'])) {
|
|
$this->convertToPdf($entityWorkflow);
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function convertToPdf(EntityWorkflow $entityWorkflow): void
|
|
{
|
|
$storedObject = $this->entityWorkflowManager->getAssociatedStoredObject($entityWorkflow);
|
|
|
|
if (null === $storedObject) {
|
|
return;
|
|
}
|
|
|
|
if ('application/pdf' === $storedObject->getCurrentVersion()->getType()) {
|
|
return;
|
|
}
|
|
|
|
$this->storedObjectToPdfConverter->addConvertedVersion($storedObject, $this->requestStack->getCurrentRequest()->getLocale(), 'pdf');
|
|
}
|
|
}
|