mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-26 08:35:00 +00:00
80 lines
2.3 KiB
PHP
80 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Chill\PersonBundle\Household;
|
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
|
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
|
use Chill\PersonBundle\Entity\Household\Position;
|
|
use Chill\PersonBundle\Entity\Household\Household;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
|
|
class MembersEditor
|
|
{
|
|
private ValidatorInterface $validator;
|
|
private Household $household;
|
|
|
|
private array $persistables = [];
|
|
private array $membershipsAffected = [];
|
|
|
|
public function __construct(ValidatorInterface $validator, Household $household)
|
|
{
|
|
$this->validation = $validator;
|
|
$this->household = $household;
|
|
}
|
|
|
|
public function addMovement(\DateTimeImmutable $date, Person $person, Position $position, ?bool $holder = false, ?string $comment = null): self
|
|
{
|
|
if (NULL === $this->household) {
|
|
throw new \LogicException("You must define a household first");
|
|
}
|
|
|
|
$membership = (new HouseholdMember())
|
|
->setStartDate($date)
|
|
->setPerson($person)
|
|
->setPosition($position)
|
|
->setHolder($holder)
|
|
->setHousehold($this->household)
|
|
->setComment($comment)
|
|
;
|
|
|
|
if ($position->getShareHousehold()) {
|
|
foreach ($person->getHouseholdParticipations() as $participation) {
|
|
if (FALSE === $participation->getShareHousehold()) {
|
|
continue;
|
|
}
|
|
|
|
if ($participation === $membership) {
|
|
continue;
|
|
}
|
|
|
|
if ($participation->getEndDate() === NULL || $participation->getEndDate() > $date) {
|
|
$participation->setEndDate($date);
|
|
$this->membershipsAffected[] = $participation;
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->membershipsAffected[] = $membership;
|
|
$this->persistables[] = $membership;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function validate(): ConstraintViolationListInterface
|
|
{
|
|
|
|
}
|
|
|
|
public function getPersistable(): array
|
|
{
|
|
return $this->persistables;
|
|
}
|
|
|
|
public function getHousehold(): Household
|
|
{
|
|
return $this->household;
|
|
}
|
|
}
|