Add workflow guard to block external send without public view

Introduce `EntityWorkflowGuardSendExternalIfNoPublicView` class to prevent workflows from transitioning to an external send state if the entity lacks a public view. Included unit tests to verify functionality for entities both with and without public views.
This commit is contained in:
2024-10-23 15:26:25 +02:00
parent 71aaf01687
commit fd69568842
2 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
<?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\MainBundle\Workflow\EventSubscriber;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\EntityWorkflowManager;
use Chill\MainBundle\Workflow\EntityWorkflowWithPublicViewInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\TransitionBlocker;
final readonly class EntityWorkflowGuardSendExternalIfNoPublicView implements EventSubscriberInterface
{
public function __construct(private Registry $registry, private EntityWorkflowManager $entityWorkflowManager) {}
public static function getSubscribedEvents()
{
return ['workflow.guard' => [
['guardSendExternalIfNoStoredObject', 0],
]];
}
public function guardSendExternalIfNoStoredObject(GuardEvent $event)
{
$entityWorkflow = $event->getSubject();
if (!$entityWorkflow instanceof EntityWorkflow) {
return;
}
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
foreach ($event->getTransition()->getTos() as $to) {
$metadata = $workflow->getMetadataStore()->getPlaceMetadata($to);
if (true === ($metadata['isSentExternal'] ?? false)
&& !$this->entityWorkflowManager->getHandler($entityWorkflow) instanceof EntityWorkflowWithPublicViewInterface) {
$event->addTransitionBlocker(
new TransitionBlocker(
'No document associated with this entityWorkflow, not able to send external',
'a95e57d8-9136-11ef-a208-43b111cfc66d'
)
);
return;
}
}
}
}