normalizer for entityworkflow

This commit is contained in:
Julien Fastré 2022-01-28 17:09:00 +01:00
parent da22532587
commit fdafe7c82b
4 changed files with 134 additions and 5 deletions

View File

@ -51,7 +51,6 @@ class EntityWorkflow implements TrackCreationInterface, TrackUpdateInterface
* @ORM\Id * @ORM\Id
* @ORM\GeneratedValue * @ORM\GeneratedValue
* @ORM\Column(type="integer") * @ORM\Column(type="integer")
* @Serializer\Groups({"read"})
*/ */
private ?int $id = null; private ?int $id = null;

View File

@ -0,0 +1,69 @@
<?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\Serializer\Normalizer;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class EntitWorkflowStepNormalizer implements NormalizerAwareInterface, NormalizerInterface
{
use NormalizerAwareTrait;
private MetadataExtractor $metadataExtractor;
public function __construct(MetadataExtractor $metadataExtractor)
{
$this->metadataExtractor = $metadataExtractor;
}
/**
* @param EntityWorkflowStep $object
*/
public function normalize($object, ?string $format = null, array $context = []): array
{
$data = [
'type' => 'entity_workflow_step',
'id' => $object->getId(),
'comment' => $object->getComment(),
'currentStep' => $this->metadataExtractor->buildArrayPresentationForPlace($object->getEntityWorkflow(), $object),
'finalizeAfter' => $object->isFinalizeAfter(),
'isFreezed' => false,
'isFinalized' => false,
'previousId' => null,
'nextId' => null,
'by' => null,
'at' => null,
];
if (null !== $previous = $object->getPrevious()) {
$data['previousId'] = $previous->getId();
$data['isFreezed'] = $previous->isFreezeAfter();
$data['isFinalized'] = $previous->isFreezeAfter();
$data['by'] = $previous->getTransitionBy();
$data['at'] = $previous->getTransitionAt();
}
if (null !== $next = $object->getNext()) {
$data['nextId'] = $next->getId();
}
return $data;
}
public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof EntityWorkflowStep && 'json' === $format;
}
}

View File

@ -0,0 +1,59 @@
<?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\Serializer\Normalizer;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Workflow\Registry;
class EntityWorkflowNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private MetadataExtractor $metadataExtractor;
private Registry $registry;
public function __construct(MetadataExtractor $metadataExtractor, Registry $registry)
{
$this->metadataExtractor = $metadataExtractor;
$this->registry = $registry;
}
/**
* @param EntityWorkflow $object
*
* @return array
*/
public function normalize($object, ?string $format = null, array $context = [])
{
$workflow = $this->registry->get($object, $object->getWorkflowName());
return [
'type' => 'entity_workflow',
'id' => $object->getId(),
'relatedEntityClass' => $object->getRelatedEntityClass(),
'relatedEntityId' => $object->getRelatedEntityId(),
'workflow' => $this->metadataExtractor->buildArrayPresentationForWorkflow($workflow),
'current_step' => $this->metadataExtractor->buildArrayPresentationForPlace($object),
'steps' => $this->normalizer->normalize($object->getStepsChained(), $format, $context),
];
}
public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof EntityWorkflow && 'json' === $format;
}
}

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Workflow\Helper; namespace Chill\MainBundle\Workflow\Helper;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow; use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Symfony\Component\Workflow\Registry; use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\WorkflowInterface; use Symfony\Component\Workflow\WorkflowInterface;
@ -51,16 +52,17 @@ class MetadataExtractor
return $workflowsList; return $workflowsList;
} }
public function buildArrayPresentationForPlace(EntityWorkflow $entityWorkflow): array public function buildArrayPresentationForPlace(EntityWorkflow $entityWorkflow, ?EntityWorkflowStep $step = null): array
{ {
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName()); $workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
$step ??= $entityWorkflow->getCurrentStep();
$markingMetadata = $workflow->getMetadataStore()->getPlaceMetadata($entityWorkflow->getCurrentStep()->getCurrentStep()); $markingMetadata = $workflow->getMetadataStore()->getPlaceMetadata($step->getCurrentStep());
$text = array_key_exists('label', $markingMetadata) ? $text = array_key_exists('label', $markingMetadata) ?
$this->translatableStringHelper->localize($markingMetadata['label']) : $entityWorkflow->getCurrentStep()->getCurrentStep(); $this->translatableStringHelper->localize($markingMetadata['label']) : $step->getCurrentStep();
return ['name' => $entityWorkflow->getCurrentStep()->getCurrentStep(), 'text' => $text]; return ['name' => $step->getCurrentStep(), 'text' => $text];
} }
public function buildArrayPresentationForWorkflow(WorkflowInterface $workflow): array public function buildArrayPresentationForWorkflow(WorkflowInterface $workflow): array