fix social action consistency

This commit is contained in:
2022-04-26 21:12:31 +02:00
parent db6c4f15f8
commit b2fb86111d
3 changed files with 93 additions and 31 deletions

View File

@@ -230,45 +230,39 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac
public function addSocialAction(SocialAction $socialAction): self
{
$descendants = $socialAction->getDescendantsWithThis();
$parent = $socialAction->getParent();
$parentKey = array_search($parent, $this->socialActions->toArray());
if (false !== $parentKey) {
$this->socialActions[$parentKey] = $socialAction;
// return $this;
if (!$this->socialActions->contains($socialAction)) {
$this->socialActions[] = $socialAction;
$this->ensureSocialActionConsistency();
}
if (count($descendants) > 0) {
foreach ($descendants as $d) {
$inCollection = $this->socialActions->contains($d);
if ($inCollection) {
return $this;
}
}
}
$this->socialActions[] = $socialAction;
return $this;
}
private function ensureSocialActionConsistency(): void
{
$ancestors = SocialAction::findAncestorSocialActions($this->getSocialActions());
foreach ($ancestors as $ancestor) {
$this->removeSocialAction($ancestor);
}
}
/**
* Add a social issue
*
* Note: the social issue consistency (the fact that only yougest social issues
* are kept) is processed by an entity listener:
* @see{\Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodSocialIssueConsistencyEntityListener}
*
* @param SocialIssue $socialIssue
* @return $this
*/
public function addSocialIssue(SocialIssue $socialIssue): self
{
$descendants = $socialIssue->getDescendantsWithThis();
if (count($descendants) > 0) {
foreach ($descendants as $d) {
$inCollection = $this->socialIssues->contains($d);
if ($inCollection) {
return $this;
}
}
if (!$this->socialIssues->contains($socialIssue)) {
$this->socialIssues[] = $socialIssue;
}
$this->socialIssues[] = $socialIssue;
return $this;
}