mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,28 +1,42 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Household;
|
||||
|
||||
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
||||
use Chill\PersonBundle\Entity\Household\Position;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
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;
|
||||
|
||||
class MembersEditor
|
||||
{
|
||||
private ValidatorInterface $validator;
|
||||
private ?Household $household = null;
|
||||
|
||||
private array $persistables = [];
|
||||
private array $membershipsAffected = [];
|
||||
private array $oldMembershipsHashes = [];
|
||||
public const VALIDATION_GROUP_AFFECTED = 'household_memberships';
|
||||
|
||||
public const VALIDATION_GROUP_CREATED = 'household_memberships_created';
|
||||
public const VALIDATION_GROUP_AFFECTED = 'household_memberships';
|
||||
|
||||
private ?Household $household = null;
|
||||
|
||||
private array $membershipsAffected = [];
|
||||
|
||||
private array $oldMembershipsHashes = [];
|
||||
|
||||
private array $persistables = [];
|
||||
|
||||
private ValidatorInterface $validator;
|
||||
|
||||
public function __construct(ValidatorInterface $validator, ?Household $household)
|
||||
{
|
||||
@@ -30,10 +44,10 @@ class MembersEditor
|
||||
$this->household = $household;
|
||||
}
|
||||
|
||||
public function addMovement(\DateTimeImmutable $date, Person $person, Position $position, ?bool $holder = false, ?string $comment = null): self
|
||||
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");
|
||||
if (null === $this->household) {
|
||||
throw new LogicException('You must define a household first');
|
||||
}
|
||||
|
||||
$membership = (new HouseholdMember())
|
||||
@@ -41,8 +55,7 @@ class MembersEditor
|
||||
->setPerson($person)
|
||||
->setPosition($position)
|
||||
->setHolder($holder)
|
||||
->setComment($comment)
|
||||
;
|
||||
->setComment($comment);
|
||||
$this->household->addMember($membership);
|
||||
|
||||
if ($position->getShareHousehold()) {
|
||||
@@ -55,10 +68,10 @@ class MembersEditor
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($participation->getEndDate() === NULL || $participation->getEndDate() > $date) {
|
||||
if ($participation->getEndDate() === null || $participation->getEndDate() > $date) {
|
||||
$participation->setEndDate($date);
|
||||
$this->membershipsAffected[] = $participation;
|
||||
$this->oldMembershipsHashes[] = \spl_object_hash($participation);
|
||||
$this->oldMembershipsHashes[] = spl_object_hash($participation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,8 +82,23 @@ class MembersEditor
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getHousehold(): ?Household
|
||||
{
|
||||
return $this->household;
|
||||
}
|
||||
|
||||
public function getPersistable(): array
|
||||
{
|
||||
return $this->persistables;
|
||||
}
|
||||
|
||||
public function hasHousehold(): bool
|
||||
{
|
||||
return null !== $this->household;
|
||||
}
|
||||
|
||||
public function leaveMovement(
|
||||
\DateTimeImmutable $date,
|
||||
DateTimeImmutable $date,
|
||||
Person $person
|
||||
): self {
|
||||
$criteria = new Criteria();
|
||||
@@ -84,8 +112,7 @@ class MembersEditor
|
||||
);
|
||||
|
||||
$participations = $person->getHouseholdParticipations()
|
||||
->matching($criteria)
|
||||
;
|
||||
->matching($criteria);
|
||||
|
||||
foreach ($participations as $participation) {
|
||||
$participation->setEndDate($date);
|
||||
@@ -95,41 +122,24 @@ class MembersEditor
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function validate(): ConstraintViolationListInterface
|
||||
{
|
||||
if ($this->hasHousehold()) {
|
||||
$list = $this->validator
|
||||
->validate($this->getHousehold(), null, [ self::VALIDATION_GROUP_AFFECTED ]);
|
||||
->validate($this->getHousehold(), null, [self::VALIDATION_GROUP_AFFECTED]);
|
||||
} else {
|
||||
$list = new ConstraintViolationList();
|
||||
}
|
||||
|
||||
foreach ($this->membershipsAffected as $m) {
|
||||
if (\in_array(\spl_object_hash($m), $this->oldMembershipsHashes)) {
|
||||
$list->addAll($this->validator->validate($m, null, [ self::VALIDATION_GROUP_AFFECTED ]));
|
||||
if (in_array(spl_object_hash($m), $this->oldMembershipsHashes)) {
|
||||
$list->addAll($this->validator->validate($m, null, [self::VALIDATION_GROUP_AFFECTED]));
|
||||
} else {
|
||||
$list->addAll($this->validator->validate($m, null, [ self::VALIDATION_GROUP_CREATED,
|
||||
self::VALIDATION_GROUP_AFFECTED ]));
|
||||
$list->addAll($this->validator->validate($m, null, [self::VALIDATION_GROUP_CREATED,
|
||||
self::VALIDATION_GROUP_AFFECTED, ]));
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getPersistable(): array
|
||||
{
|
||||
return $this->persistables;
|
||||
}
|
||||
|
||||
|
||||
public function getHousehold(): ?Household
|
||||
{
|
||||
return $this->household;
|
||||
}
|
||||
|
||||
public function hasHousehold(): bool
|
||||
{
|
||||
return $this->household !== null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user