<?php
namespace MLDev\AccountBundle\Controller;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use MLDev\AccountBundle\Entity\Account;
use MLDev\AccountBundle\Form\AccountFormType;
use MLDev\AccountBundle\Repository\AccountRepository;
use MLDev\BaseBundle\Controller\FrontController;
use MLDev\BaseBundle\Service\PageManager;
use MLDev\OrderBundle\Entity\Order;
use MLDev\OrderBundle\Entity\Payment;
use MLDev\OrderBundle\Entity\PaymentInfo;
use MLDev\OrderBundle\Service\PaymentProvider;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* Class AccountController
* @package MLDev\AccountBundle\Controller
*/
class AccountController extends FrontController
{
const LOGIN_REDIRECT_ROUTE = 'mldev-front-account';
const LOGOUT_REDIRECT_ROUTE = 'mldev-front-home';
/**
* @var PageManager
*/
private $pageManager;
/**
* @var AccountRepository
*/
private $accountRepository;
private $direct_api_widget_id;
private $recaptcha_key;
public function __construct(
PageManager $pageManager,
AccountRepository $accountRepository,
ParameterBagInterface $params
) {
$this->pageManager = $pageManager;
$this->accountRepository = $accountRepository;
$this->direct_api_widget_id = $params->get('direct_api_widget_id');
$this->recaptcha_key = $params->get('recaptcha_key');
}
/**
* @Route("/account", name="mldev-front-account")
* @IsGranted("ROLE_USER_ACCOUNT")
*/
public function account(Request $request): Response
{
$entity = $this->pageManager->createVirtualEntity('Личный кабинет', '/account');
/** @var Account $account */
$account = $this->getUser();
$form = $this->createForm(AccountFormType::class, $account);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$this->accountRepository->save($account);
return $this->redirectToRoute('mldev-front-account', ['accountInfo' => 'saved']);
}
$this->accountRepository->refresh($account);
}
return $this->renderCustomTemplate($entity, '@MLDevAccount/account/account.html.twig', [
'user' => $account,
'accountInfo' => $form->createView(),
]);
}
/**
* @Route("/account/orders", name="mldev-front-account-orders")
* @IsGranted("ROLE_USER_ACCOUNT")
*/
public function accountOrders(Request $request, PaymentProvider $paymentProvider): Response
{
$entity = $this->pageManager->createVirtualEntity('Мои заказы', '/account/orders');
$orderId = (int)$request->query->get('order', null);
$methodId = (int)$request->query->get('method', null);
$isChange = (bool)$request->query->get('change', false);
/** @var ArrayCollection $userOrders */
$userOrders = $this->getUser()->getAvailableOrders();
$availablePaymentMethods = new ArrayCollection(
$paymentProvider->getAvailablePaymentMethods(null)
);
if ($isChange === true) {
$criteria = new Criteria(
Criteria::expr()->eq('id', $orderId)
);
if (($userOrders = $userOrders->matching($criteria)) && $userOrders->count()) {
/** @var Order $order */
$order = $userOrders->first();
if ($order->getPayment() instanceof PaymentInfo) {
if (!$order->getPayment()->getType() instanceof Payment\Yookassa || !$order->getPayment()->getType(
) instanceof Payment\Tinkoff) {
$criteria = new Criteria(
Criteria::expr()->eq('id', $methodId)
);
if (($method = $availablePaymentMethods->matching($criteria)) && $method->count()) {
$order->getPayment()->setType($method->first());
$this->accountRepository->save($order);
}
}
}
}
return $this->redirectToRoute('mldev-front-account-orders', ['order' => $orderId]);
}
return $this->renderCustomTemplate($entity, '@MLDevAccount/account/account-orders.html.twig', [
'user' => $this->getUser(),
'availablePaymentMethods' => $availablePaymentMethods->toArray(),
]);
}
/**
* @Route("/account/login", name="mldev-front-account-login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute(self::LOGIN_REDIRECT_ROUTE);
}
// dd($this->findOneBy(['phoneNumber' => $phoneNumber]));
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$entity = $this->pageManager->createVirtualEntity('Личный кабинет', '/account/login');
return $this->renderCustomTemplate($entity, '@MLDevAccount/account/login.html.twig', [
'error' => $error,
'last_username' => $lastUsername,
'widgetId' => $this->direct_api_widget_id,
'captchaSiteKey' => $this->recaptcha_key
]);
}
/**
* @Route("/account/logout", name="mldev-front-account-logout")
* @IsGranted("ROLE_USER_ACCOUNT")
*/
public function logout(Request $request, TokenStorageInterface $tokenStorage): RedirectResponse
{
if ($request->getSession()->invalidate()) {
$tokenStorage->setToken(null);
}
return $this->redirectToRoute(self::LOGOUT_REDIRECT_ROUTE);
}
}