diff --git a/src/Bundle/ChillMainBundle/Entity/User.php b/src/Bundle/ChillMainBundle/Entity/User.php index 8eab3bb11..414ae47aa 100644 --- a/src/Bundle/ChillMainBundle/Entity/User.php +++ b/src/Bundle/ChillMainBundle/Entity/User.php @@ -264,7 +264,8 @@ class User implements UserInterface, \Stringable public function getMainScope(?DateTimeImmutable $at = null): ?Scope { - $at ??= new DateTimeImmutable('today'); + $at ??= new DateTimeImmutable('now'); + $criteria = new Criteria(); $expr = Criteria::expr(); @@ -307,7 +308,8 @@ class User implements UserInterface, \Stringable public function getUserJob(?DateTimeImmutable $at = null): ?UserJob { - $at ??= new DateTimeImmutable('today'); + $at ??= new DateTimeImmutable('now'); + $criteria = new Criteria(); $expr = Criteria::expr(); @@ -322,6 +324,7 @@ class User implements UserInterface, \Stringable ); $jobs = $this->jobHistories->matching($criteria); + if ($jobs->count() > 0) { return $jobs->first()->getJob(); } @@ -502,19 +505,31 @@ class User implements UserInterface, \Stringable return $this; } - public function setMainScope(UserScopeHistory $mainScope): User + public function setMainScope(?Scope $mainScope): User { - if (!$this->scopeHistories->contains($mainScope)) { - $this->scopeHistories[] = $mainScope; - $mainScope->setUser($this); + $currentScopeUnchanged = array_filter( + $this->scopeHistories->toArray(), + 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->orderBy(['startDate' => Criteria::ASC, 'id' => Criteria::ASC]); /** @var Iterator $scopes */ - $scopes = $this->getMainScope()->matching($criteria)->getIterator(); + $scopes = $this->scopeHistories->matching($criteria)->getIterator(); $scopes->rewind(); do { @@ -555,18 +570,31 @@ class User implements UserInterface, \Stringable return $this; } - public function setUserJob(UserJobHistory $userJob): User + public function setUserJob(?UserJob $userJob): User { - if (!$this->jobHistories->contains($userJob)) { - $this->jobHistories[] = $userJob; - $userJob->setUser($this); + $currentJobUnchanged = array_filter( + $this->jobHistories->toArray(), + 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->orderBy(['startDate' => Criteria::ASC, 'id' => Criteria::ASC]); /** @var Iterator $jobs */ - $jobs = $this->getUserJob()->matching($criteria)->getIterator(); + $jobs = $this->jobHistories->matching($criteria)->getIterator(); $jobs->rewind(); do {