mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
create api for social issue consistency
This commit is contained in:
@@ -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;
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user