bundles/mldev-account-bundle/src/Controller/AccountController.php line 63

Open in your IDE?
  1. <?php
  2. namespace MLDev\AccountBundle\Controller;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Criteria;
  5. use MLDev\AccountBundle\Entity\Account;
  6. use MLDev\AccountBundle\Form\AccountFormType;
  7. use MLDev\AccountBundle\Repository\AccountRepository;
  8. use MLDev\BaseBundle\Controller\FrontController;
  9. use MLDev\BaseBundle\Service\PageManager;
  10. use MLDev\OrderBundle\Entity\Order;
  11. use MLDev\OrderBundle\Entity\Payment;
  12. use MLDev\OrderBundle\Entity\PaymentInfo;
  13. use MLDev\OrderBundle\Service\PaymentProvider;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  22. /**
  23.  * Class AccountController
  24.  * @package MLDev\AccountBundle\Controller
  25.  */
  26. class AccountController extends FrontController
  27. {
  28.     const LOGIN_REDIRECT_ROUTE 'mldev-front-account';
  29.     const LOGOUT_REDIRECT_ROUTE 'mldev-front-home';
  30.     /**
  31.      * @var PageManager
  32.      */
  33.     private $pageManager;
  34.     /**
  35.      * @var AccountRepository
  36.      */
  37.     private $accountRepository;
  38.     private $direct_api_widget_id;
  39.     private $recaptcha_key;
  40.     public function __construct(
  41.         PageManager $pageManager,
  42.         AccountRepository $accountRepository,
  43.         ParameterBagInterface $params
  44.     ) {
  45.         $this->pageManager $pageManager;
  46.         $this->accountRepository $accountRepository;
  47.         $this->direct_api_widget_id $params->get('direct_api_widget_id');
  48.         $this->recaptcha_key $params->get('recaptcha_key');
  49.     }
  50.     /**
  51.      * @Route("/account", name="mldev-front-account")
  52.      * @IsGranted("ROLE_USER_ACCOUNT")
  53.      */
  54.     public function account(Request $request): Response
  55.     {
  56.         $entity $this->pageManager->createVirtualEntity('Личный кабинет''/account');
  57.         /** @var Account $account */
  58.         $account $this->getUser();
  59.         $form $this->createForm(AccountFormType::class, $account);
  60.         $form->handleRequest($request);
  61.         if ($form->isSubmitted()) {
  62.             if ($form->isValid()) {
  63.                 $this->accountRepository->save($account);
  64.                 return $this->redirectToRoute('mldev-front-account', ['accountInfo' => 'saved']);
  65.             }
  66.             $this->accountRepository->refresh($account);
  67.         }
  68.         return $this->renderCustomTemplate($entity'@MLDevAccount/account/account.html.twig', [
  69.             'user' => $account,
  70.             'accountInfo' => $form->createView(),
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/account/orders", name="mldev-front-account-orders")
  75.      * @IsGranted("ROLE_USER_ACCOUNT")
  76.      */
  77.     public function accountOrders(Request $requestPaymentProvider $paymentProvider): Response
  78.     {
  79.         $entity $this->pageManager->createVirtualEntity('Мои заказы''/account/orders');
  80.         $orderId = (int)$request->query->get('order'null);
  81.         $methodId = (int)$request->query->get('method'null);
  82.         $isChange = (bool)$request->query->get('change'false);
  83.         /** @var ArrayCollection $userOrders */
  84.         $userOrders $this->getUser()->getAvailableOrders();
  85.         $availablePaymentMethods = new ArrayCollection(
  86.             $paymentProvider->getAvailablePaymentMethods(null)
  87.         );
  88.         if ($isChange === true) {
  89.             $criteria = new Criteria(
  90.                 Criteria::expr()->eq('id'$orderId)
  91.             );
  92.             if (($userOrders $userOrders->matching($criteria)) && $userOrders->count()) {
  93.                 /** @var Order $order */
  94.                 $order $userOrders->first();
  95.                 if ($order->getPayment() instanceof PaymentInfo) {
  96.                     if (!$order->getPayment()->getType() instanceof Payment\Yookassa || !$order->getPayment()->getType(
  97.                             ) instanceof Payment\Tinkoff) {
  98.                         $criteria = new Criteria(
  99.                             Criteria::expr()->eq('id'$methodId)
  100.                         );
  101.                         if (($method $availablePaymentMethods->matching($criteria)) && $method->count()) {
  102.                             $order->getPayment()->setType($method->first());
  103.                             $this->accountRepository->save($order);
  104.                         }
  105.                     }
  106.                 }
  107.             }
  108.             return $this->redirectToRoute('mldev-front-account-orders', ['order' => $orderId]);
  109.         }
  110.         return $this->renderCustomTemplate($entity'@MLDevAccount/account/account-orders.html.twig', [
  111.             'user' => $this->getUser(),
  112.             'availablePaymentMethods' => $availablePaymentMethods->toArray(),
  113.         ]);
  114.     }
  115.     /**
  116.      * @Route("/account/login", name="mldev-front-account-login")
  117.      */
  118.     public function login(AuthenticationUtils $authenticationUtils): Response
  119.     {
  120.         if ($this->getUser()) {
  121.             return $this->redirectToRoute(self::LOGIN_REDIRECT_ROUTE);
  122.         }
  123.         //   dd($this->findOneBy(['phoneNumber' => $phoneNumber]));
  124.         // get the login error if there is one
  125.         $error $authenticationUtils->getLastAuthenticationError();
  126.         // last username entered by the user
  127.         $lastUsername $authenticationUtils->getLastUsername();
  128.         $entity $this->pageManager->createVirtualEntity('Личный кабинет''/account/login');
  129.         return $this->renderCustomTemplate($entity'@MLDevAccount/account/login.html.twig', [
  130.             'error' => $error,
  131.             'last_username' => $lastUsername,
  132.             'widgetId' => $this->direct_api_widget_id,
  133.             'captchaSiteKey' => $this->recaptcha_key
  134.         ]);
  135.     }
  136.     /**
  137.      * @Route("/account/logout", name="mldev-front-account-logout")
  138.      * @IsGranted("ROLE_USER_ACCOUNT")
  139.      */
  140.     public function logout(Request $requestTokenStorageInterface $tokenStorage): RedirectResponse
  141.     {
  142.         if ($request->getSession()->invalidate()) {
  143.             $tokenStorage->setToken(null);
  144.         }
  145.         return $this->redirectToRoute(self::LOGOUT_REDIRECT_ROUTE);
  146.     }
  147. }