create api for social issue consistency

This commit is contained in:
2021-08-21 23:18:55 +02:00
parent 2a3f869882
commit 09e5cc1545
6 changed files with 341 additions and 1 deletions

View File

@@ -0,0 +1,28 @@
<?php
namespace Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency;
use Doctrine\Common\Collections\Collection;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
/**
* This interface must be implemented on entities which:
*
* * have both social issue and is linked to an AccompanyingPeriod
* * when the social issues in the entity should be added to the accompanying period
*
* A doctrine listener will list social issues which are associated to the entity, but
* not on the AccompanyingPeriod, and push them back to the accompanying period.
*/
interface AccompanyingPeriodLinkedWithSocialIssuesEntityInterface
{
public function getAccompanyingPeriod(): AccompanyingPeriod;
/**
* @return Collection|SocialIssue[]
*/
public function getSocialIssues(): Collection;
public function removeSocialIssue(SocialIssue $issue): self;
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Doctrine\ORM\Event\LifecycleEventArgs;
/**
* This service listens for preUpdate and prePersist events on some entities
* and ensure consistency of SocialIssue with an associated AccompanyingPeriod.
*
* The entity must implements interface Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodLinkedWithSocialIssuesEntityInterface.
*
* This subscriber is not called automatically: for performance reasons, this
* apply only on entities which are configured. See https://symfony.com/doc/4.4/doctrine/events.html#doctrine-entity-listeners
*/
final class AccompanyingPeriodSocialIssueConsistencyEntityListener
{
public function prePersist(AccompanyingPeriodLinkedWithSocialIssuesEntityInterface $entity, LifecycleEventArgs $eventArgs)
{
$this->ensureConsistencyEntity($entity);
}
public function preUpdate(AccompanyingPeriodLinkedWithSocialIssuesEntityInterface $entity, LifecycleEventArgs $eventArgs)
{
$this->ensureConsistencyEntity($entity);
}
public function prePersistAccompanyingPeriod(AccompanyingPeriod $period, LifecycleEventArgs $eventArgs)
{
$this->ensureConsistencyAccompanyingPeriod($period);
}
public function preUpdateAccompanyingPeriod(AccompanyingPeriod $period, LifecycleEventArgs $eventArgs)
{
$this->ensureConsistencyAccompanyingPeriod($period);
}
private function ensureConsistencyEntity(AccompanyingPeriodLinkedWithSocialIssuesEntityInterface $entity): void
{
// remove issues parents on the entity itself
$ancestors = SocialIssue::findAncestorSocialIssues($entity->getSocialIssues());
foreach ($ancestors as $ancestor) {
$entity->removeSocialIssue($ancestor);
}
$period = $entity->getAccompanyingPeriod();
foreach ($entity->getSocialIssues() as $issue) {
// the entity itself test if the social issue is already associated, or not
$period->addSocialIssue($issue);
}
$this->ensureConsistencyAccompanyingPeriod($period);
}
private function ensureConsistencyAccompanyingPeriod(AccompanyingPeriod $period): void
{
$ancestors = SocialIssue::findAncestorSocialIssues($period->getSocialIssues());
foreach ($ancestors as $ancestor) {
$period->removeSocialIssue($ancestor);
}
}
}