Exemplo n.º 1
0
 /**
  * @param PageEleveurBranch|null $branch
  * @return PageEleveur
  */
 private function fromBranch(PageEleveurBranch $branch = null)
 {
     $pageEleveur = new PageEleveur();
     $pageEleveur->setId($branch->getId());
     $pageEleveur->setOwner($branch->getOwner());
     $pageEleveur->setSlug($branch->getSlug());
     $pageEleveur->setHead($branch->getCommit()->getId());
     $pageEleveur->setNom($branch->getCommit()->getNom());
     $pageEleveur->setDescription($branch->getCommit()->getDescription());
     $pageEleveur->setEspeces($branch->getCommit()->getEspeces());
     $pageEleveur->setRaces($branch->getCommit()->getRaces());
     $pageEleveur->setLieu($branch->getCommit()->getLieu());
     /*
      * Le tri permet de retourner une PE dans le bon ordre même si
      * le commit a été fait dans un mauvais ordre.
      */
     $arrayCollection = $branch->getCommit()->getAnimaux()->toArray();
     usort($arrayCollection, function (PageAnimalBranch $a, PageAnimalBranch $b) {
         return $b->getCreatedAt()->getTimestamp() - $a->getCreatedAt()->getTimestamp();
     });
     $animaux = [];
     /** @var PageAnimalBranch $pageAnimalBranch */
     foreach ($arrayCollection as $pageAnimalBranch) {
         $animaux[] = PageAnimalService::fromBranch($pageAnimalBranch);
     }
     $pageEleveur->setAnimaux($animaux);
     $actualites = $branch->getCommit()->getActualites()->toArray();
     usort($actualites, function (Actualite $a, Actualite $b) {
         return $b->getDate()->getTimestamp() - $a->getDate()->getTimestamp();
     });
     $pageEleveur->setActualites($actualites);
     return $pageEleveur;
 }
Exemplo n.º 2
0
 /**
  * @expectedException \AppBundle\Service\ValidationException
  * @expectedExceptionCode \AppBundle\Service\ValidationException::EMPTY_DATE_NAISSANCE
  */
 public function testCommit_dateNaissance_empty()
 {
     // Simulation d'une page animal en base de données
     $user = new User();
     $pageAnimalBranch = new PageAnimalBranch();
     $pageAnimalBranch->setOwner($user);
     $commit = new PageAnimalCommit(null, 'rodolf', $this->timeService->now(), null, PageAnimal::DISPONIBLE, PageAnimal::MALE, null);
     $commit->setId(1);
     $pageAnimalBranch->setCommit($commit);
     $this->pageAnimalBranchRepository->method('find')->willReturn($pageAnimalBranch);
     $this->pageAnimalCommitRepository->method('find')->with($commit->getId())->willReturn($commit);
     // Commit la page avec un nom vide
     $pageAnimal = $this->pageAnimalService->find($pageAnimalBranch->getId());
     $pageAnimal->setDateNaissance(null);
     $this->pageAnimalService->commit($user, $pageAnimal);
 }
Exemplo n.º 3
0
 /**
  * @Route("/animal/{pageAnimalId}", name="commitPageAnimal_route")
  * @Method("POST")
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function commitAction($pageAnimalId, Request $request)
 {
     /** @var PageAnimal $pageEleveur */
     $pageAnimal = $this->serializer->deserialize($request->getContent(), PageAnimal::class, 'json');
     /** @var AnonymousToken $token */
     $token = $this->tokenStorage->getToken();
     /** @var User $user */
     $user = $token->getUser();
     if ($user === 'anon.') {
         return new RedirectResponse($this->router->generate('fos_user_security_login'));
     }
     try {
         $this->pageAnimalService->commit($user, $pageAnimal);
         return new Response($this->serializer->serialize($pageAnimal, 'json'));
     } catch (HistoryException $e) {
         return $this->createHistoryErrorResponse($e, $user, $pageAnimal);
     } catch (ValidationException $e) {
         return $this->createValidationErrorResponse($e, $user, $pageAnimal);
     }
 }