first impl of event (WIP)

This commit is contained in:
Julien Fastré 2022-02-11 10:31:17 +01:00
parent f2221565c5
commit 441704dc29
5 changed files with 330 additions and 6 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,144 @@
<?php
namespace Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\MainBundle\Doctrine\Model\TrackCreationTrait;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @ORM\Table("chill_person_accompanying_period_location_history")
*/
class AccompanyingPeriodLocationHistory
{
use TrackCreationTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\ManyToOne(targetEntity=AccompanyingPeriod::class)
*/
private AccompanyingPeriod $period;
/**
* @ORM\ManyToOne(targetEntity=Person::class)
*/
private ?Person $personLocation;
/**
* @ORM\ManyToOne(targetEntity=Address::class)
*/
private ?Address $addressLocation;
/**
* @ORM\Column(type="date_immutable")
*/
private ?\DateTimeImmutable $startDate;
/**
* @ORM\Column(type="date_immutable")
*/
private ?\DateTimeImmutable $endDate;
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->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;
}
}

View File

@ -0,0 +1,119 @@
<?php
namespace Chill\PersonBundle\Event\Person;
use Chill\MainBundle\Entity\Address;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Person;
use Symfony\Contracts\EventDispatcher\Event;
class PersonAddressMoveEvent extends Event
{
public const PERSON_MOVE_POST = 'chill_person.person_move_post';
private Person $person;
private ?Address $previousAddress;
private ?Address $nextAddress;
private ?HouseholdMember $previousMembership;
private ?HouseholdMember $nextMembership;
public function __construct(
Person $person
) {
$this->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;
}
}

View File

@ -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()) {

View File

@ -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);
}
}