/** * @param User $user * @param PageAnimal $pageAnimal * @throws HistoryException * @throws ValidationException */ public function commit(User $user, PageAnimal $pageAnimal) { /** @var PageAnimalBranch $pageAnimalBranch */ $pageAnimalBranch = $this->pageAnimalBranchRepository->find($pageAnimal->getId()); if ($pageAnimalBranch == null) { throw new HistoryException(HistoryException::BRANCHE_INCONNUE); } if ($user->getId() !== $pageAnimalBranch->getOwner()->getId()) { throw new HistoryException(HistoryException::DROIT_REFUSE); } /** @var PageAnimalCommit $clientHead */ $clientHead = $this->pageAnimalCommitRepository->find($pageAnimal->getHead()); if ($clientHead->getId() !== $pageAnimalBranch->getCommit()->getId()) { throw new HistoryException(HistoryException::NON_FAST_FORWARD); } if (empty($pageAnimal->getNom())) { throw new ValidationException(ValidationException::EMPTY_NOM); } if (empty($pageAnimal->getDateNaissance())) { throw new ValidationException(ValidationException::EMPTY_DATE_NAISSANCE); } $commit = new PageAnimalCommit($clientHead, $pageAnimal->getNom(), $pageAnimal->getDateNaissance(), $pageAnimal->getDescription(), $pageAnimal->getStatut(), $pageAnimal->getSexe(), $pageAnimal->getPhotos()); $this->doctrine->persist($commit); $pageAnimalBranch->setCommit($commit); $this->doctrine->flush([$commit, $pageAnimalBranch]); $pageAnimal->setHead($commit->getId()); }
/** * @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; }