fix errors in user getter/setters for mainScope and userJob

This commit is contained in:
Mathieu Jaumotte 2023-09-20 17:24:18 +02:00
parent 024790128a
commit 6228cc5ede

View File

@ -264,7 +264,8 @@ class User implements UserInterface, \Stringable
public function getMainScope(?DateTimeImmutable $at = null): ?Scope public function getMainScope(?DateTimeImmutable $at = null): ?Scope
{ {
$at ??= new DateTimeImmutable('today'); $at ??= new DateTimeImmutable('now');
$criteria = new Criteria(); $criteria = new Criteria();
$expr = Criteria::expr(); $expr = Criteria::expr();
@ -307,7 +308,8 @@ class User implements UserInterface, \Stringable
public function getUserJob(?DateTimeImmutable $at = null): ?UserJob public function getUserJob(?DateTimeImmutable $at = null): ?UserJob
{ {
$at ??= new DateTimeImmutable('today'); $at ??= new DateTimeImmutable('now');
$criteria = new Criteria(); $criteria = new Criteria();
$expr = Criteria::expr(); $expr = Criteria::expr();
@ -322,6 +324,7 @@ class User implements UserInterface, \Stringable
); );
$jobs = $this->jobHistories->matching($criteria); $jobs = $this->jobHistories->matching($criteria);
if ($jobs->count() > 0) { if ($jobs->count() > 0) {
return $jobs->first()->getJob(); return $jobs->first()->getJob();
} }
@ -502,19 +505,31 @@ class User implements UserInterface, \Stringable
return $this; return $this;
} }
public function setMainScope(UserScopeHistory $mainScope): User public function setMainScope(?Scope $mainScope): User
{ {
if (!$this->scopeHistories->contains($mainScope)) { $currentScopeUnchanged = array_filter(
$this->scopeHistories[] = $mainScope; $this->scopeHistories->toArray(),
$mainScope->setUser($this); fn($row) => $row->getEndDate() === null && $row->getScope() === $mainScope
);
if (count($currentScopeUnchanged) > 0) {
return $this;
} }
// ensure continuity of histories $newScope = new UserScopeHistory();
$newScope
->setStartDate(new DateTimeImmutable('now'))
->setScope($mainScope)
->setUser($this);
$this->scopeHistories[] = $newScope;
$criteria = new Criteria(); $criteria = new Criteria();
$criteria->orderBy(['startDate' => Criteria::ASC, 'id' => Criteria::ASC]); $criteria->orderBy(['startDate' => Criteria::ASC, 'id' => Criteria::ASC]);
/** @var Iterator $scopes */ /** @var Iterator $scopes */
$scopes = $this->getMainScope()->matching($criteria)->getIterator(); $scopes = $this->scopeHistories->matching($criteria)->getIterator();
$scopes->rewind(); $scopes->rewind();
do { do {
@ -555,18 +570,31 @@ class User implements UserInterface, \Stringable
return $this; return $this;
} }
public function setUserJob(UserJobHistory $userJob): User public function setUserJob(?UserJob $userJob): User
{ {
if (!$this->jobHistories->contains($userJob)) { $currentJobUnchanged = array_filter(
$this->jobHistories[] = $userJob; $this->jobHistories->toArray(),
$userJob->setUser($this); fn($row) => $row->getEndDate() === null && $row->getJob() === $userJob
);
if (count($currentJobUnchanged) > 0) {
return $this;
} }
$newJob = new UserJobHistory();
$newJob
->setStartDate(new DateTimeImmutable('now'))
->setJob($userJob)
->setUser($this);
$this->jobHistories[] = $newJob;
$criteria = new Criteria(); $criteria = new Criteria();
$criteria->orderBy(['startDate' => Criteria::ASC, 'id' => Criteria::ASC]); $criteria->orderBy(['startDate' => Criteria::ASC, 'id' => Criteria::ASC]);
/** @var Iterator $jobs */ /** @var Iterator $jobs */
$jobs = $this->getUserJob()->matching($criteria)->getIterator(); $jobs = $this->jobHistories->matching($criteria)->getIterator();
$jobs->rewind(); $jobs->rewind();
do { do {