createLyhendiForm(); $lastResult = $this->getRepository()->findOneBy([], ['id' => 'DESC']); return $this->renderForm('index.html.twig', ['form' => $form, 'last' => $lastResult]); } private function createLyhendiForm(): FormInterface { return $this->createForm(LyhendiType::class); } private function getRepository(): UrlRepository { /** @noinspection PhpIncompatibleReturnTypeInspection */ return $this->getEntityManager()->getRepository(Url::class); } private function getEntityManager(): EntityManagerInterface { /** @noinspection PhpIncompatibleReturnTypeInspection */ return $this->managerRegistry->getManagerForClass(Url::class); } /** * @Route("/", methods={"POST"}) */ public function insert(Request $request): Response { $form = $this->createForm(LyhendiType::class); $form->handleRequest($request); if (!$form->isSubmitted()) { return $this->getRedirectToHome(); } if ($form->isValid()) { /** @var Url $url */ $url = $form->getData(); $manager = $this->getEntityManager(); $manager->persist($url); $manager->flush(); $publicString = $url->getCustomString() ?? $this->hasher->encode($url->getId()); return $this->redirectToRoute('app_lyhendi_get', ['string' => $publicString]); } return $this->renderForm('index.html.twig', ['form' => $form, 'last' => null]); } private function getRedirectToHome(): RedirectResponse { return $this->redirectToRoute('app_lyhendi_index'); } /** * @Route("/{string}", methods={"GET"}) */ public function get(string $string): Response { $url = $this->getRepository()->findOneByAny($string); if ($url === null) { return $this->getRedirectToHome(); } return new Response($url->getLongUrl()); } }