mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-29 19:13:49 +00:00
Add TransitionHasDestineeIfIsSentExternal validator
This commit introduces a new validator to ensure that transitions marked as 'sent' have a designated external recipient. It includes related tests for scenarios with and without recipients and covers integration with the workflow context.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?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\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Check that a transition does have at least one external if the 'to' is 'isSentExternal'.
|
||||
*/
|
||||
#[\Attribute]
|
||||
class TransitionHasDestineeIfIsSentExternal extends Constraint
|
||||
{
|
||||
public $messageDestineeRequired = 'workflow.transition_has_destinee_if_sent_external';
|
||||
public $messageDestineeNotNecessary = 'workflow.transition_destinee_not_necessary';
|
||||
public $codeNoNecessaryDestinee = 'd78ea142-819d-11ef-a459-b7009a3e4caf';
|
||||
public $codeDestineeUnauthorized = 'eb8051fc-8227-11ef-8c3b-7f2de85bdc5b';
|
||||
|
||||
public function getTargets(): string
|
||||
{
|
||||
return self::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
<?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\Validator;
|
||||
|
||||
use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
|
||||
final class TransitionHasDestineeIfIsSentExternalValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(private readonly Registry $registry) {}
|
||||
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof TransitionHasDestineeIfIsSentExternal) {
|
||||
throw new UnexpectedTypeException($constraint, TransitionHasDestineeIfIsSentExternal::class);
|
||||
}
|
||||
|
||||
if (!$value instanceof WorkflowTransitionContextDTO) {
|
||||
throw new UnexpectedTypeException($value, WorkflowTransitionContextDTO::class);
|
||||
}
|
||||
|
||||
if (null == $value->transition) {
|
||||
return;
|
||||
}
|
||||
|
||||
$workflow = $this->registry->get($value->entityWorkflow, $value->entityWorkflow->getWorkflowName());
|
||||
$isSentExternal = false;
|
||||
foreach ($value->transition->getTos() as $to) {
|
||||
$metadata = $workflow->getMetadataStore()->getPlaceMetadata($to);
|
||||
|
||||
$isSentExternal = $isSentExternal ? true : $metadata['isSentExternal'] ?? false;
|
||||
}
|
||||
|
||||
if (!$isSentExternal) {
|
||||
if (0 !== count($value->futureDestineeThirdParties)) {
|
||||
$this->context->buildViolation($constraint->messageDestineeRequired)
|
||||
->atPath('futureDestineeThirdParties')
|
||||
->setCode($constraint->codeDestineeUnauthorized)
|
||||
->addViolation();
|
||||
}
|
||||
if (0 !== count($value->futureDestineeEmails)) {
|
||||
$this->context->buildViolation($constraint->messageDestineeRequired)
|
||||
->atPath('futureDestineeEmails')
|
||||
->setCode($constraint->codeDestineeUnauthorized)
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (0 === count($value->futureDestineeEmails) && 0 === count($value->futureDestineeThirdParties)) {
|
||||
$this->context->buildViolation($constraint->messageDestineeRequired)
|
||||
->atPath('futureDestineeThirdParties')
|
||||
->setCode($constraint->codeNoNecessaryDestinee)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
@@ -14,6 +14,7 @@ namespace Chill\MainBundle\Workflow;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\UserGroup;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Workflow\Validator\TransitionHasDestineeIfIsSentExternal;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\ThirdPartyBundle\Validator\ThirdPartyHasEmail;
|
||||
@@ -24,6 +25,7 @@ use Symfony\Component\Workflow\Transition;
|
||||
/**
|
||||
* Context for a transition on an workflow entity.
|
||||
*/
|
||||
#[TransitionHasDestineeIfIsSentExternal]
|
||||
class WorkflowTransitionContextDTO
|
||||
{
|
||||
/**
|
||||
|
Reference in New Issue
Block a user