mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-03 13:33:48 +00:00
105 worflow
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Workflow\Validator;
|
||||
|
||||
/**
|
||||
* Validator which will test that:.
|
||||
*
|
||||
* * a handler exists;
|
||||
* * a related entity does exists;
|
||||
* * a workflow can be associated with this entity.
|
||||
*
|
||||
* @Annotation
|
||||
*/
|
||||
class EntityWorkflowCreation extends \Symfony\Component\Validator\Constraint
|
||||
{
|
||||
public string $messageEntityNotFound = 'Related entity is not found';
|
||||
|
||||
public string $messageHandlerNotFound = 'Handler not found for this entity';
|
||||
|
||||
public string $messageWorkflowNotAvailable = 'Workflow is not valid';
|
||||
|
||||
public function getTargets()
|
||||
{
|
||||
return [self::CLASS_CONSTRAINT];
|
||||
}
|
||||
}
|
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Workflow\Validator;
|
||||
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowManager;
|
||||
use Chill\MainBundle\Workflow\Exception\HandlerNotFoundException;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Workflow\WorkflowInterface;
|
||||
use function count;
|
||||
|
||||
class EntityWorkflowCreationValidator extends \Symfony\Component\Validator\ConstraintValidator
|
||||
{
|
||||
private EntityWorkflowManager $entityWorkflowManager;
|
||||
|
||||
public function __construct(EntityWorkflowManager $entityWorkflowManager)
|
||||
{
|
||||
$this->entityWorkflowManager = $entityWorkflowManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EntityWorkflow $value
|
||||
* @param Constraint|EntityWorkflowCreation $constraint
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (!$value instanceof EntityWorkflow) {
|
||||
throw new UnexpectedValueException($value, EntityWorkflow::class);
|
||||
}
|
||||
|
||||
if (!$constraint instanceof EntityWorkflowCreation) {
|
||||
throw new UnexpectedTypeException($constraint, EntityWorkflowCreation::class);
|
||||
}
|
||||
|
||||
try {
|
||||
$handler = $this->entityWorkflowManager->getHandler($value);
|
||||
} catch (HandlerNotFoundException $e) {
|
||||
$this->context->buildViolation($constraint->messageHandlerNotFound)
|
||||
->addViolation();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (null === $handler->getRelatedEntity($value)) {
|
||||
$this->context->buildViolation($constraint->messageEntityNotFound)
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
$workflows = $this->entityWorkflowManager->getSupportedWorkflows($value);
|
||||
|
||||
$matched = array_filter($workflows, static function (WorkflowInterface $workflow) use ($value) {
|
||||
return $workflow->getName() === $value->getWorkflowName();
|
||||
});
|
||||
|
||||
if (0 === count($matched)) {
|
||||
$this->context->buildViolation($constraint->messageWorkflowNotAvailable)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user