/** * @Route("/animal/{pageAnimalId}", name="getPageAnimal_route") * @Method("GET") * @return \Symfony\Component\HttpFoundation\Response */ public function getAction($pageAnimalId) { $pageAnimal = $this->pageAnimalService->find($pageAnimalId); if (!$pageAnimal) { throw new NotFoundHttpException(null, null); } /** @var AnonymousToken $token */ $token = $this->tokenStorage->getToken(); /** @var User $user */ $user = $token->getUser(); $isOwner = $user !== 'anon.' && $pageAnimal->getOwner()->getId() === $user->getId(); $pageEleveur = $this->pageEleveurService->findByPageAnimal($pageAnimal); return $this->templating->renderResponse('base.html.twig', [TwigNodeTemplateTreeSection::TEMPLATE_TREE_BRANCH => 'editable/page-animal', 'pageAnimal' => $pageAnimal, 'isEditable' => $isOwner, 'pageEleveur' => $pageEleveur]); }
/** * @Route("/", name="teaser_route") * @param Request $request * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response * @throws \Exception */ public function teaserAction(Request $request) { /** @var AnonymousToken $token */ $token = $this->tokenStorage->getToken(); /** @var User $user */ $user = $token->getUser(); if ($user == 'anon.') { return $this->templating->renderResponse('base.html.twig', [TwigNodeTemplateTreeSection::TEMPLATE_TREE_BRANCH => 'home/teaser']); } $pageEleveur = $this->pageEleveurService->findByOwner($user); $form = $this->formFactory->createNamedBuilder('creation-page-eleveur')->add('nom', TextType::class, ['label' => 'Nom de l\'élevage'])->add('save', SubmitType::class, array('label' => 'Créer ma page éleveur'))->getForm(); $form->handleRequest($request); if (!$form->isSubmitted() && $pageEleveur) { // un eleveur ne peux pas créer une 2eme page eleveur return $this->templating->renderResponse('base.html.twig', [TwigNodeTemplateTreeSection::TEMPLATE_TREE_BRANCH => 'home/eleveur', 'pageEleveur' => $pageEleveur]); } if ($form->isSubmitted() && $form->isValid()) { // traitement du formulaire de creation de page eleveur $nom = $form->getData()['nom']; try { $slug = $this->pageEleveurService->create($nom, $user)->getSlug(); return new RedirectResponse($this->router->generate('getPageEleveur_route', ['pageEleveurSlug' => $slug])); } catch (HistoryException $e) { $this->logger->error($e->getMessage(), ['exception' => $e, 'user' => $user, 'nom' => $nom]); switch ($e->getCode()) { case HistoryException::NOM_INVALIDE: return new Response('Le nom "' . $nom . '" n\'est pas valide.', Response::HTTP_NOT_ACCEPTABLE); case HistoryException::SLUG_DEJA_EXISTANT: return new Response('Une page éleveur du même nom existe déjà.', Response::HTTP_CONFLICT); case HistoryException::DEJA_OWNER: return new Response('Vous avez déjà une page éleveur.', Response::HTTP_BAD_REQUEST); } } } // home d'un user connecté mais qui n'a pas de page eleveur return $this->templating->renderResponse('base.html.twig', [TwigNodeTemplateTreeSection::TEMPLATE_TREE_BRANCH => 'home/creation-page-eleveur', 'creationPageEleveur' => $form->createView()]); }
/** * @Route("/commit-page-eleveur", name="commitPageEleveur_route") * @Method("POST") * @param Request $request * @return Response * @throws HistoryException */ public function commitAction(Request $request) { /** @var PageEleveur $pageEleveur */ $pageEleveur = $this->serializer->deserialize($request->getContent(), PageEleveur::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 { $pageEleveur = $this->pageEleveurService->commit($user, $pageEleveur); return new Response(self::jsonPageEleveur($pageEleveur)); } catch (HistoryException $e) { return $this->createErrorResponse($e, $user, $pageEleveur); } }
public function testOrdre_Actualite() { // Mock d'une page eleveur en base de données $user = new User(); $user->setId(1); $pageEleveurBranch = new PageEleveurBranch(); $pageEleveurBranch->setId(1); $pageEleveurBranch->setOwner($user); $this->pageEleveurBranchRepository->method('find')->withAnyParameters()->willReturn($pageEleveurBranch); $commit1 = $this->newCommit(1); $pageEleveurBranch->setCommit($commit1); $this->pageEleveurCommitRepository->method('find')->withAnyParameters()->willReturn($commit1); //Simulation d'une requete de commit $pageEleveur = new PageEleveur(); $pageEleveur->setId($pageEleveurBranch->getId()); $pageEleveur->setHead($commit1->getId()); // Les actu sont commités dans le mauvais ordre $pageEleveur->setActualites([new Actualite('actu 1', new \DateTime()), new Actualite('actu 2', (new \DateTime())->add(new \DateInterval('P2D')))]); $pageEleveur = $this->pageEleveurService->commit($user, $pageEleveur); // commit() doit retourner les actu dans le bon ordre $this->assertEquals('actu 2', $pageEleveur->getActualites()[0]->getContenu()); $this->assertEquals('actu 1', $pageEleveur->getActualites()[1]->getContenu()); }