<?php
namespace App\Controller;
use App\Entity\Admin;
use App\Enum\Site\Domain;
use App\Form\Type\LoginType;
use App\Security\FormAuthenticator;
use App\Service\Ads\AdsService;
use App\Service\Image\ImageService;
use App\Service\Mail\MailService;
use App\Service\SendLog\SendLogService;
use App\Service\User\UserService;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Security;
class MainController extends AbstractController
{
public function index(Request $request, Security $security, UserService $userService, ImageService $imageService)
{
if (!$security->getUser()) {
return $this->redirectToRoute('login');
}
/** @var Admin $admin */
$admin = $security->getUser()->getAssociatedUser();
if ($admin->getDomain() && $admin->getDomain() != $request->getLocale()) {
return $this->redirectToRoute('main', ['_locale' => $admin->getDomain()]);
}
$params = $userService->getDomainStatParams($request->getLocale());
return $this->render('main.html.twig', $params);
}
public function login(Request $request, Security $security, MailService $mailService)
{
if ($security->getUser()) {
return $this->redirectToRoute('main');
}
$code = $request->get('code', '');
if (in_array($code, ['code', 'tapak'])) {
$codeTxt = rand(100000, 999999);
$request->getSession()->set(FormAuthenticator::CODE_CACHE_KEY . $request->getClientIp(), $codeTxt);
if ($code == 'code') {
$email = 'jevgeniya.yan@gmail.com';
} else {
/** @var Admin $admin */
$admin = $this->getDoctrine()->getManager()->getRepository(Admin::class)->findOneBy(['username' => $code]);
if (!$admin || !$admin instanceof Admin) {
throw new \Exception('Error');
}
$email = $admin->getEmail();
$code = 'code';
}
$mailService->sendMail($email, 'Auth code', $codeTxt, Domain::GRIBU_LV);
}
$admin = new Admin();
$loginForm = $this->createForm(LoginType::class, $admin);
$error = $request->getSession()->get(Security::AUTHENTICATION_ERROR);
if ($error) {
$request->getSession()->set(Security::AUTHENTICATION_ERROR, null);
}
return $this->render('login.html.twig', [
'login_form' => $loginForm->createView(),
'error' => $error,
'code' => $code,
]);
}
public function logout()
{
//
}
public function lastLog(int $id, Security $security, SendLogService $sendLogService, Request $request)
{
if (!$security->getUser()) {
return $this->redirectToRoute('login');
}
if ($id) {
return $this->render('last_log.html.twig', [
'log' => $sendLogService->get($id)
]);
}
return $this->render('last_logs.html.twig', [
'logs' => $sendLogService->getAll($request->getLocale()),
]);
}
/*public function testAction(MailService $mailService, AdsService $adsService)
{
$ads = $adsService->get(323781);
$mailService->priorityEndMessage($ads->getEmail(), $ads);
return null;
}*/
}