From 441704dc29e6518a8d8ffefca39aebe2d6e130b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 11 Feb 2022 10:31:17 +0100 Subject: [PATCH] first impl of event (WIP) --- .../Entity/AccompanyingPeriod.php | 36 ++++- .../AccompanyingPeriodLocationHistory.php | 144 ++++++++++++++++++ .../Event/Person/PersonAddressMoveEvent.php | 119 +++++++++++++++ .../Household/MembersEditor.php | 23 ++- .../Household/MembersEditorFactory.php | 14 +- 5 files changed, 330 insertions(+), 6 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodLocationHistory.php create mode 100644 src/Bundle/ChillPersonBundle/Event/Person/PersonAddressMoveEvent.php diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 1e7444af6..00a5ca580 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -21,6 +21,7 @@ use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\UserJob; +use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodLocationHistory; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork; use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment; @@ -113,6 +114,12 @@ class AccompanyingPeriod implements */ public const STEP_DRAFT = 'DRAFT'; + /** + * @ORM\OneToMany(targetEntity=AccompanyingPeriodLocationHistory::class, + * mappedBy="period") + */ + private Collection $locationHistories; + /** * @ORM\ManyToOne( * targetEntity=Address::class @@ -384,6 +391,7 @@ class AccompanyingPeriod implements $this->works = new ArrayCollection(); $this->resources = new ArrayCollection(); $this->userHistories = new ArrayCollection(); + $this->locationHistories = new ArrayCollection(); } /** @@ -1036,7 +1044,9 @@ class AccompanyingPeriod implements */ public function setAddressLocation(?Address $addressLocation = null): self { - $this->addressLocation = $addressLocation; + if ($this->addressLocation !== $addressLocation) { + $this->addressLocation = $addressLocation; + } return $this; } @@ -1259,4 +1269,28 @@ class AccompanyingPeriod implements return $this; } + + public function getLocationHistories(): Collection + { + return $this->locationHistories; + } + + public function addLocationHistory(AccompanyingPeriodLocationHistory $history): self + { + if (!$this->locationHistories->contains($history)) { + $this->locationHistories[] = $history; + $history->setPeriod($this); + } + + return $this; + } + + public function removeLocationHistory(AccompanyingPeriodLocationHistory $history): self + { + if ($this->locationHistories->removeElement($history)) { + $history->setPeriod(null); + } + + return $this; + } } diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodLocationHistory.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodLocationHistory.php new file mode 100644 index 000000000..4ceac647d --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodLocationHistory.php @@ -0,0 +1,144 @@ +id; + } + + /** + * @return Person|null + */ + public function getPersonLocation(): ?Person + { + return $this->personLocation; + } + + /** + * @param Person|null $personLocation + * @return AccompanyingPeriodLocationHistory + */ + public function setPersonLocation(?Person $personLocation): AccompanyingPeriodLocationHistory + { + $this->personLocation = $personLocation; + return $this; + } + + /** + * @return Address|null + */ + public function getAddressLocation(): ?Address + { + return $this->addressLocation; + } + + /** + * @param Address|null $addressLocation + * @return AccompanyingPeriodLocationHistory + */ + public function setAddressLocation(?Address $addressLocation): AccompanyingPeriodLocationHistory + { + $this->addressLocation = $addressLocation; + return $this; + } + + /** + * @return \DateTimeImmutable|null + */ + public function getStartDate(): ?\DateTimeImmutable + { + return $this->startDate; + } + + /** + * @param \DateTimeImmutable|null $startDate + * @return AccompanyingPeriodLocationHistory + */ + public function setStartDate(?\DateTimeImmutable $startDate): AccompanyingPeriodLocationHistory + { + $this->startDate = $startDate; + return $this; + } + + /** + * @return \DateTimeImmutable|null + */ + public function getEndDate(): ?\DateTimeImmutable + { + return $this->endDate; + } + + /** + * @param \DateTimeImmutable|null $endDate + * @return AccompanyingPeriodLocationHistory + */ + public function setEndDate(?\DateTimeImmutable $endDate): AccompanyingPeriodLocationHistory + { + $this->endDate = $endDate; + return $this; + } + + /** + * @return AccompanyingPeriod + */ + public function getPeriod(): AccompanyingPeriod + { + return $this->period; + } + + /** + * @internal use AccompanyingPeriod::addLocationHistory + */ + public function setPeriod(AccompanyingPeriod $period): AccompanyingPeriodLocationHistory + { + $this->period = $period; + return $this; + } +} diff --git a/src/Bundle/ChillPersonBundle/Event/Person/PersonAddressMoveEvent.php b/src/Bundle/ChillPersonBundle/Event/Person/PersonAddressMoveEvent.php new file mode 100644 index 000000000..afd3ad37d --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Event/Person/PersonAddressMoveEvent.php @@ -0,0 +1,119 @@ +person = $person; + } + + /** + * @param Address|null $previousAddress + * @return PersonAddressMoveEvent + */ + public function setPreviousAddress(?Address $previousAddress): PersonAddressMoveEvent + { + $this->previousAddress = $previousAddress; + return $this; + } + + + public function getPerson(): Person + { + return $this->person; + } + + public function getPreviousAddress(): ?Address + { + return $this->previousAddress; + } + + public function getPreviousHousehold(): ?Household + { + if (null !== $previousMembership = $this->getPreviousMembership()) { + return $previousMembership->getHousehold(); + } + + return null; + } + + public function getNextHousehold(): ?Household + { + if (NULL !== $nextMembership = $this->getNextMembership()) { + return $nextMembership->getHousehold(); + } + + return null; + } + + public function personChangeHousehold(): bool + { + return $this->getPreviousHousehold() !== $this->getNextHousehold(); + } + + public function personChangeAddress(): bool + { + return $this->getPreviousAddress() !== $this->getNextAddress(); + } + + /** + * @return HouseholdMember|null + */ + public function getPreviousMembership(): ?HouseholdMember + { + return $this->previousMembership; + } + + /** + * @param HouseholdMember|null $previousMembership + * @return PersonAddressMoveEvent + */ + public function setPreviousMembership(?HouseholdMember $previousMembership): PersonAddressMoveEvent + { + $this->previousMembership = $previousMembership; + return $this; + } + + public function getNextMembership(): ?HouseholdMember + { + return $this->nextMembership; + } + + public function setNextMembership(?HouseholdMember $nextMembership): PersonAddressMoveEvent + { + $this->nextMembership = $nextMembership; + return $this; + } + + public function getNextAddress(): ?Address + { + return $this->nextAddress; + } + + public function setNextAddress(?Address $nextAddress): PersonAddressMoveEvent + { + $this->nextAddress = $nextAddress; + return $this; + } +} diff --git a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php index adc97a4ac..d3a777471 100644 --- a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php +++ b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php @@ -15,13 +15,13 @@ use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\HouseholdMember; use Chill\PersonBundle\Entity\Household\Position; use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent; use DateTimeImmutable; use Doctrine\Common\Collections\Criteria; use LogicException; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\ConstraintViolationListInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; - use function in_array; use function spl_object_hash; @@ -41,12 +41,17 @@ class MembersEditor private array $persistables = []; + private array $events = []; + private ValidatorInterface $validator; - public function __construct(ValidatorInterface $validator, ?Household $household) + private EventDispatcherInterface $eventDispatcher; + + public function __construct(ValidatorInterface $validator, ?Household $household, EventDispatcherInterface $eventDispatcher) { $this->validator = $validator; $this->household = $household; + $this->eventDispatcher = $eventDispatcher; } public function addMovement(DateTimeImmutable $date, Person $person, Position $position, ?bool $holder = false, ?string $comment = null): self @@ -55,6 +60,8 @@ class MembersEditor throw new LogicException('You must define a household first'); } + $event = new PersonAddressMoveEvent($person); + $membership = (new HouseholdMember()) ->setStartDate($date) ->setPerson($person) @@ -62,6 +69,7 @@ class MembersEditor ->setHolder($holder) ->setComment($comment); $this->household->addMember($membership); + $event->setNextMembership($membership); if ($position->getShareHousehold()) { foreach ($person->getHouseholdParticipationsShareHousehold() as $participation) { @@ -74,6 +82,7 @@ class MembersEditor } if ($participation->getEndDate() === null || $participation->getEndDate() > $date) { + $event->setPreviousMembership($participation); $participation->setEndDate($date); $this->membershipsAffected[] = $participation; $this->oldMembershipsHashes[] = spl_object_hash($participation); @@ -92,6 +101,7 @@ class MembersEditor $this->membershipsAffected[] = $membership; $this->persistables[] = $membership; + $this->events[] = $event; return $this; } @@ -129,6 +139,8 @@ class MembersEditor ->matching($criteria); foreach ($participations as $participation) { + $this->events[] = $event = new PersonAddressMoveEvent(); + $event->setPreviousMembership($participation); $participation->setEndDate($date); $this->membershipsAffected[] = $participation; } @@ -136,6 +148,13 @@ class MembersEditor return $this; } + public function postMove(): void + { + foreach ($this->events as $event) { + $this->eventDispatcher->dispatch($event); + } + } + public function validate(): ConstraintViolationListInterface { if ($this->hasHousehold()) { diff --git a/src/Bundle/ChillPersonBundle/Household/MembersEditorFactory.php b/src/Bundle/ChillPersonBundle/Household/MembersEditorFactory.php index a26841064..366b1cb4c 100644 --- a/src/Bundle/ChillPersonBundle/Household/MembersEditorFactory.php +++ b/src/Bundle/ChillPersonBundle/Household/MembersEditorFactory.php @@ -12,17 +12,25 @@ declare(strict_types=1); namespace Chill\PersonBundle\Household; use Chill\PersonBundle\Entity\Household\Household; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; class MembersEditorFactory { - public function __construct(ValidatorInterface $validator) - { + private EventDispatcherInterface $eventDispatcher; + + private ValidatorInterface $validator; + + public function __construct( + EventDispatcherInterface $eventDispatcher, + ValidatorInterface $validator + ) { $this->validator = $validator; + $this->eventDispatcher = $eventDispatcher; } public function createEditor(?Household $household = null): MembersEditor { - return new MembersEditor($this->validator, $household); + return new MembersEditor($this->validator, $household, $this->eventDispatcher); } }