<?php
namespace App\Controller;
use App\Entity\Faq;
use App\Entity\User;
use App\Service\GmailService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use App\Service\UserService;
use App\Service\MailService;
class PageController extends AbstractController
{
private $userService;
public function __construct(UserService $userService)
{
$this->userService = $userService;
}
/**
* @Route("/kontakt", name="contact")
*/
public function contact(): Response
{
return $this->render('page/contact.html.twig', [
'controller_name' => 'PageController',
]);
}
/**
* @Route("/faq", name="faq")
*/
public function faq(ManagerRegistry $doctrine): Response
{
$faq = $doctrine->getRepository(Faq::class)->findAllArray();
$faqCategory = $doctrine->getRepository(Faq::class)->countByCategory();
foreach ($faqCategory as $item) {
$category = $item['category'];
unset($item['category']);
$faqCategoryCount[$category] = $item['count'];
}
return $this->render('page/faq.html.twig', [
'faq' => $faq,
'category' => $faqCategoryCount
]);
}
/**
* @Route("/przypomnij_haslo", name="forgot_password")
*/
public function forgotPassword(): Response
{
return $this->render('page/forgotPassword.html.twig', [
'controller_name' => 'PageController',
]);
}
/**
* @Route("/recovery_password", name="recovery_password")
*/
public function recoveryPassword(GmailService $mailer, Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $entityManager): JsonResponse
{
$json = $request->getContent();
$data = json_decode($json, true);
$user = $doctrine->getRepository(User::class)->findOneByEmail($data['email']);
if ($user instanceof User) {
$mailService = new MailService();
$newPassword = $this->userService->generatePassword();
$user->setPassword($passwordHasher->hashPassword(
$user,
$newPassword)
);
$entityManager->persist($user);
$mailer->sendTwigEmail(
$data['email'],
'Nowe Hasło',
'reset_password',
['password' => $newPassword]
);
$entityManager->flush();
return new JsonResponse(['success' => true], 200);
}
return new JsonResponse(['success' => false], 400);
}
}