105 worflow

This commit is contained in:
2022-01-24 13:17:46 +00:00
committed by Julien Fastré
parent daff4e4200
commit c7dbaae8d6
110 changed files with 5176 additions and 392 deletions

View File

@@ -0,0 +1,39 @@
<?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;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
interface EntityWorkflowHandlerInterface
{
public function getRelatedEntity(EntityWorkflow $entityWorkflow): ?object;
/**
* Return a string representing the role required for seeing the workflow.
*
* Return Null if any check is required, or a role name. The voter will check for
* authorization with the role as attribute, and the
*/
public function getRoleShow(EntityWorkflow $entityWorkflow): ?string;
public function getTemplate(EntityWorkflow $entityWorkflow, array $options = []): string;
public function getTemplateData(EntityWorkflow $entityWorkflow, array $options = []): array;
public function getTemplateTitle(EntityWorkflow $entityWorkflow, array $options = []): string;
public function getTemplateTitleData(EntityWorkflow $entityWorkflow, array $options = []): array;
public function supports(EntityWorkflow $entityWorkflow, array $options = []): bool;
public function supportsFreeze(EntityWorkflow $entityWorkflow, array $options = []): bool;
}

View File

@@ -0,0 +1,48 @@
<?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;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Exception\HandlerNotFoundException;
use Symfony\Component\Workflow\Registry;
class EntityWorkflowManager
{
/**
* @var iterable|EntityWorkflowHandlerInterface[]
*/
private iterable $handlers;
private Registry $registry;
public function __construct(iterable $handlers, Registry $registry)
{
$this->handlers = $handlers;
$this->registry = $registry;
}
public function getHandler(EntityWorkflow $entityWorkflow, array $options = []): EntityWorkflowHandlerInterface
{
foreach ($this->handlers as $handler) {
if ($handler->supports($entityWorkflow, $options)) {
return $handler;
}
}
throw new HandlerNotFoundException();
}
public function getSupportedWorkflows(EntityWorkflow $entityWorkflow): array
{
return $this->registry->all($entityWorkflow);
}
}

View File

@@ -0,0 +1,114 @@
<?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\EventSubscriber;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Templating\Entity\UserRender;
use DateTimeImmutable;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\TransitionBlocker;
class EntityWorkflowTransitionEventSubscriber implements EventSubscriberInterface
{
private LoggerInterface $chillLogger;
private Security $security;
private UserRender $userRender;
public function __construct(
LoggerInterface $chillLogger,
Security $security,
UserRender $userRender
) {
$this->chillLogger = $chillLogger;
$this->security = $security;
$this->userRender = $userRender;
}
public static function getSubscribedEvents(): array
{
return [
'workflow.transition' => 'onTransition',
'workflow.guard' => [
['guardEntityWorkflow', 0],
],
];
}
public function guardEntityWorkflow(GuardEvent $event)
{
if (!$event->getSubject() instanceof EntityWorkflow) {
return;
}
/** @var EntityWorkflow $entityWorkflow */
$entityWorkflow = $event->getSubject();
if ($entityWorkflow->isFinalize()) {
$event->addTransitionBlocker(
new TransitionBlocker(
'workflow.The workflow is finalized',
'd6306280-7535-11ec-a40d-1f7bee26e2c0'
)
);
return;
}
if (!$entityWorkflow->getCurrentStep()->getDestUser()->contains($this->security->getUser())) {
if (!$event->getMarking()->has('initial')) {
$event->addTransitionBlocker(new TransitionBlocker(
'workflow.You are not allowed to apply a transition on this workflow. Only those users are allowed: %users%',
'f3eeb57c-7532-11ec-9495-e7942a2ac7bc',
[
'%users%' => implode(
', ',
$entityWorkflow->getCurrentStep()->getDestUser()->map(function (User $u) {
return $this->userRender->renderString($u, []);
})->toArray()
),
]
));
}
}
}
public function onTransition(Event $event)
{
if (!$event->getSubject() instanceof EntityWorkflow) {
return;
}
/** @var EntityWorkflow $entityWorkflow */
$entityWorkflow = $event->getSubject();
$step = $entityWorkflow->getCurrentStep();
$step
->setTransitionAfter($event->getTransition()->getName())
->setTransitionAt(new DateTimeImmutable('now'))
->setTransitionBy($this->security->getUser());
$this->chillLogger->info('[workflow] apply transition on entityWorkflow', [
'relatedEntityClass' => $entityWorkflow->getRelatedEntityClass(),
'relatedEntityId' => $entityWorkflow->getRelatedEntityId(),
'transition' => $event->getTransition()->getName(),
'by_user' => $this->security->getUser(),
'entityWorkflow' => $entityWorkflow->getId(),
]);
}
}

View File

@@ -0,0 +1,103 @@
<?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\EventSubscriber;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Registry;
use function in_array;
class NotificationOnTransition implements EventSubscriberInterface
{
private EngineInterface $engine;
private EntityManagerInterface $entityManager;
private MetadataExtractor $metadataExtractor;
private Registry $registry;
private Security $security;
public function __construct(EntityManagerInterface $entityManager, EngineInterface $engine, MetadataExtractor $metadataExtractor, Security $security, Registry $registry)
{
$this->entityManager = $entityManager;
$this->engine = $engine;
$this->metadataExtractor = $metadataExtractor;
$this->registry = $registry;
$this->security = $security;
}
public static function getSubscribedEvents(): array
{
return [
'workflow.completed' => 'onCompleted',
];
}
public function onCompleted(Event $event): void
{
if (!$event->getSubject() instanceof EntityWorkflow) {
return;
}
/** @var EntityWorkflow $entityWorkflow */
$entityWorkflow = $event->getSubject();
$dests = array_merge(
$entityWorkflow->getSubscriberToStep()->toArray(),
$entityWorkflow->isFinalize() ? $entityWorkflow->getSubscriberToFinal()->toArray() : [],
$entityWorkflow->getCurrentStep()->getDestUser()->toArray()
);
$place = $this->metadataExtractor->buildArrayPresentationForPlace($entityWorkflow);
$workflow = $this->metadataExtractor->buildArrayPresentationForWorkflow(
$this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName())
);
$visited = [];
foreach ($dests as $subscriber) {
if (
$this->security->getUser() === $subscriber
|| in_array($subscriber->getId(), $visited, true)
) {
continue;
}
$context = [
'entity_workflow' => $entityWorkflow,
'dest' => $subscriber,
'place' => $place,
'workflow' => $workflow,
'is_dest' => $entityWorkflow->getCurrentStep()->getDestUser()->contains($subscriber),
];
$notification = new Notification();
$notification
->setRelatedEntityId($entityWorkflow->getId())
->setRelatedEntityClass(EntityWorkflow::class)
->setTitle($this->engine->render('@ChillMain/Workflow/workflow_notification_on_transition_completed_title.fr.txt.twig', $context))
->setMessage($this->engine->render('@ChillMain/Workflow/workflow_notification_on_transition_completed_content.fr.txt.twig', $context))
->addAddressee($subscriber);
$this->entityManager->persist($notification);
$visited[] = $subscriber->getId();
}
}
}

View File

@@ -0,0 +1,18 @@
<?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\Exception;
use RuntimeException;
class HandlerNotFoundException extends RuntimeException
{
}

View File

@@ -0,0 +1,74 @@
<?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\Helper;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\WorkflowInterface;
use function array_key_exists;
class MetadataExtractor
{
private Registry $registry;
private TranslatableStringHelperInterface $translatableStringHelper;
public function __construct(Registry $registry, TranslatableStringHelperInterface $translatableStringHelper)
{
$this->registry = $registry;
$this->translatableStringHelper = $translatableStringHelper;
}
public function availableWorkflowFor(string $relatedEntityClass, ?int $relatedEntityId = 0): array
{
$blankEntityWorkflow = new EntityWorkflow();
$blankEntityWorkflow
->setRelatedEntityId($relatedEntityId)
->setRelatedEntityClass($relatedEntityClass);
// build the list of available workflows, and extract their names from metadata
$workflows = $this->registry->all($blankEntityWorkflow);
$workflowsList = [];
foreach ($workflows as $workflow) {
$metadata = $workflow->getMetadataStore()->getWorkflowMetadata();
$text = array_key_exists('label', $metadata) ?
$this->translatableStringHelper->localize($metadata['label']) : $workflow->getName();
$workflowsList[] = ['name' => $workflow->getName(), 'text' => $text];
}
return $workflowsList;
}
public function buildArrayPresentationForPlace(EntityWorkflow $entityWorkflow): array
{
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
$markingMetadata = $workflow->getMetadataStore()->getPlaceMetadata($entityWorkflow->getCurrentStep()->getCurrentStep());
$text = array_key_exists('label', $markingMetadata) ?
$this->translatableStringHelper->localize($markingMetadata['label']) : $entityWorkflow->getCurrentStep()->getCurrentStep();
return ['name' => $entityWorkflow->getCurrentStep()->getCurrentStep(), 'text' => $text];
}
public function buildArrayPresentationForWorkflow(WorkflowInterface $workflow): array
{
$metadata = $workflow->getMetadataStore()->getWorkflowMetadata();
$text = array_key_exists('label', $metadata) ?
$this->translatableStringHelper->localize($metadata['label']) : $workflow->getName();
return ['name' => $workflow->getName(), 'text' => $text];
}
}

View File

@@ -0,0 +1,45 @@
<?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\Notification;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Notification\NotificationHandlerInterface;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\MainBundle\Workflow\EntityWorkflowManager;
class WorkflowNotificationHandler implements NotificationHandlerInterface
{
private EntityWorkflowManager $entityWorkflowManager;
private EntityWorkflowRepository $entityWorkflowRepository;
public function getTemplate(Notification $notification, array $options = []): string
{
return '@ChillMain/Workflow/_notification_include.html.twig';
}
public function getTemplateData(Notification $notification, array $options = []): array
{
$entityWorkflow = $this->entityWorkflowRepository->find($notification->getRelatedEntityId());
return [
'entity_workflow' => $entityWorkflow,
'handler' => $this->entityWorkflowManager->getHandler($entityWorkflow),
];
}
public function supports(Notification $notification, array $options = []): bool
{
return $notification->getRelatedEntityClass() === EntityWorkflow::class;
}
}

View File

@@ -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;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
use Symfony\Component\Workflow\WorkflowInterface;
class RelatedEntityWorkflowSupportsStrategy implements WorkflowSupportStrategyInterface
{
public function supports(WorkflowInterface $workflow, $subject): bool
{
if (!$subject instanceof EntityWorkflow) {
return false;
}
foreach ($workflow->getMetadataStore()->getWorkflowMetadata()['related_entity']
as $relatedEntityClass) {
if ($subject->getRelatedEntityClass() === $relatedEntityClass) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,29 @@
<?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\Templating;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class WorkflowTwigExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction(
'chill_entity_workflow_list',
[WorkflowTwigExtensionRuntime::class, 'listWorkflows'],
['needs_environment' => true, 'is_safe' => ['html']]
),
];
}
}

View File

@@ -0,0 +1,79 @@
<?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\Templating;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\MainBundle\Workflow\EntityWorkflowManager;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Workflow\Registry;
use Twig\Environment;
use Twig\Extension\RuntimeExtensionInterface;
class WorkflowTwigExtensionRuntime implements RuntimeExtensionInterface
{
private EntityWorkflowManager $entityWorkflowManager;
private MetadataExtractor $metadataExtractor;
private NormalizerInterface $normalizer;
private Registry $registry;
private EntityWorkflowRepository $repository;
public function __construct(
EntityWorkflowManager $entityWorkflowManager,
Registry $registry,
EntityWorkflowRepository $repository,
MetadataExtractor $metadataExtractor,
NormalizerInterface $normalizer
) {
$this->entityWorkflowManager = $entityWorkflowManager;
$this->registry = $registry;
$this->repository = $repository;
$this->metadataExtractor = $metadataExtractor;
$this->normalizer = $normalizer;
}
public function listWorkflows(Environment $environment, string $relatedEntityClass, int $relatedEntityId, array $options = []): string
{
$blankEntityWorkflow = new EntityWorkflow();
$blankEntityWorkflow
->setRelatedEntityId($relatedEntityId)
->setRelatedEntityClass($relatedEntityClass);
$workflowsList = $this->metadataExtractor->availableWorkflowFor($relatedEntityClass, $relatedEntityId);
// get the related entity already created
$entityWorkflows = [];
foreach ($entityWorkflowsNaked = $this->repository->findBy(
['relatedEntityClass' => $relatedEntityClass, 'relatedEntityId' => $relatedEntityId]
) as $entityWorkflow) {
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
$entityWorkflows[] = [
'entity_workflow' => $entityWorkflow,
'workflow' => $this->metadataExtractor->buildArrayPresentationForWorkflow($workflow),
'handler' => $this->entityWorkflowManager->getHandler($entityWorkflow),
];
}
return $environment->render('@ChillMain/Workflow/_extension_list_workflow_for.html.twig', [
'entity_workflows_json' => $this->normalizer->normalize($entityWorkflowsNaked, 'json', ['groups' => 'read']),
'entity_workflows' => $entityWorkflows,
'blank_workflow' => $blankEntityWorkflow,
'workflows_availables' => $workflowsList,
]);
}
}

View File

@@ -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];
}
}

View File

@@ -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();
}
}
}