You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.8 KiB
92 lines
2.8 KiB
2 years ago
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Controller;
|
||
|
|
||
|
use App\Entity\Url;
|
||
|
use App\Form\Type\LyhendiType;
|
||
|
use App\Hasher;
|
||
|
use App\Repository\UrlRepository;
|
||
|
use Doctrine\ORM\EntityManagerInterface;
|
||
|
use Doctrine\Persistence\ManagerRegistry;
|
||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||
|
use Symfony\Component\Form\FormInterface;
|
||
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
use Symfony\Component\Routing\Annotation\Route;
|
||
|
|
||
|
final class LyhendiController extends AbstractController
|
||
|
{
|
||
|
public function __construct(private ManagerRegistry $managerRegistry, private Hasher $hasher)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @Route("/", methods={"GET"})
|
||
|
*/
|
||
|
public function index(): Response
|
||
|
{
|
||
|
$form = $this->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());
|
||
|
}
|
||
|
}
|