/** * @param User $user * @param PageEleveur $commitingPageEleveur * @return PageEleveur * @throws HistoryException */ public function commit(User $user, PageEleveur $commitingPageEleveur) { /** @var PageEleveurCommit $clientHead */ $clientHead = $this->pageEleveurCommitRepository->find($commitingPageEleveur->getHead()); /** @var PageEleveurBranch $pageEleveurBranch */ $pageEleveurBranch = $this->pageEleveurBranchRepository->find($commitingPageEleveur->getId()); if (!$pageEleveurBranch || !$clientHead) { throw new HistoryException(HistoryException::BRANCHE_INCONNUE); } if ($pageEleveurBranch->getOwner()->getId() !== $user->getId()) { throw new HistoryException(HistoryException::DROIT_REFUSE); } if ($clientHead->getId() !== $pageEleveurBranch->getCommit()->getId()) { throw new HistoryException(HistoryException::NON_FAST_FORWARD); } $managedActialites = []; if ($commitingPageEleveur->getActualites() !== null) { foreach ($commitingPageEleveur->getActualites() as $actualite) { if ($actualite->hashCode() !== $actualite->getId()) { // l'actualité a été modifiée, on doit persister la nouvelle version $this->doctrine->persist($actualite); } else { // L'actualité n'a pas changé $actualite = $this->doctrine->merge($actualite); } array_push($managedActialites, $actualite); } } $newCommit = new PageEleveurCommit($clientHead, $commitingPageEleveur->getNom(), $commitingPageEleveur->getDescription(), $commitingPageEleveur->getEspeces(), $commitingPageEleveur->getRaces(), $commitingPageEleveur->getLieu(), $commitingPageEleveur->getAnimaux() !== null ? array_map(function (PageAnimal $pageAnimal) use($user) { if ($pageAnimal->getOwner()->getId() !== $user->getId()) { throw new HistoryException(HistoryException::DROIT_REFUSE); } return $this->pageAnimalBranchRepository->find($pageAnimal->getId()); }, $commitingPageEleveur->getAnimaux()) : [], $managedActialites); $this->doctrine->persist($newCommit); $pageEleveurBranch->setCommit($newCommit); $this->doctrine->flush([$newCommit, $pageEleveurBranch]); $pageEleveur = $this->fromBranch($this->findAtCommit($commitingPageEleveur->getId(), $newCommit->getId())); return $pageEleveur; }