cotisations = $cotisations; } public function computeCompany(CompanyMember $companyMember) { $cotis = $this->cotisations->obtenirListe(AFUP_PERSONNES_MORALES, $companyMember->getId()); $infos = $this->computeFromCotisationsAndReturnInfos($cotis); return $infos['years']; } public function computeCompanyAndReturnInfos(CompanyMember $companyMember) { $cotis = $this->cotisations->obtenirListe(AFUP_PERSONNES_MORALES, $companyMember->getId()); return $this->computeFromCotisationsAndReturnInfos($cotis); } public function compute(User $user) { $infos = $this->computeAndReturnInfos($user); return $infos['years']; } public function computeAndReturnInfos(User $user) { $cotis = $this->cotisations->obtenirListe(AFUP_PERSONNES_PHYSIQUES, $user->getId()); return $this->computeFromCotisationsAndReturnInfos($cotis); } private function computeFromCotisationsAndReturnInfos(array $cotisations) { $now = new \DateTime(); $diffs = []; $years = []; foreach ($cotisations as $coti) { $from = \DateTimeImmutable::createFromFormat('U', $coti['date_debut']); $to = \DateTimeImmutable::createFromFormat('U', $coti['date_fin']); $to = min($now, $to); $diffs[] = $from->diff($to); $years[] = $from->format('Y'); } $reference = new \DateTimeImmutable(); $lastest = clone $reference; foreach ($diffs as $dif) { $lastest = $lastest->add($dif); } $totalDiffs = $reference->diff($lastest); $firstYear = null; if (count($years)) { $firstYear = min($years); } return [ 'years' => $totalDiffs->y, 'first_year' => $firstYear, ]; } }