src/Controller/ConnectionController.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Connection;
  4. use App\Form\ConnectionType;
  5. use App\Service\CrmApi\ClientFacade;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use RetailCrm\Api\Factory\SimpleClientFactory;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Throwable;
  17. /**
  18.  * Class ConnectionController
  19.  *
  20.  * @package App\Controller
  21.  */
  22. class ConnectionController extends AbstractController
  23. {
  24.     /**
  25.      * @var EntityManagerInterface $manager
  26.      */
  27.     private EntityManagerInterface $manager;
  28.     /**
  29.      * @var ClientFacade
  30.      */
  31.     private ClientFacade $clientFacade;
  32.     /**
  33.      * ConnectionController constructor.
  34.      *
  35.      * @param EntityManagerInterface $manager
  36.      * @param ClientFacade $clientFacade
  37.      */
  38.     public function __construct(
  39.         EntityManagerInterface $manager,
  40.         ClientFacade           $clientFacade
  41.     ) {
  42.         $this->clientFacade $clientFacade;
  43.         $this->manager $manager;
  44.     }
  45.     /**
  46.      * @Route("/", name="connect", methods={"GET", "POST"})
  47.      * @param Request $request
  48.      *
  49.      * @return RedirectResponse|Response
  50.      * @throws Throwable
  51.      */
  52.     public function connect(Request $request): RedirectResponse|Response
  53.     {
  54.         /* @var Connection $connection */
  55.         $connection $this->getUser();
  56.         if ($connection) {
  57.             return $this->redirectToRoute('connection_settings');
  58.         }
  59.         $account $request->get('account');
  60.         $connection = new Connection();
  61.         $form $this->createFormBuilder($connection)
  62.             ->setAction($this->generateUrl('connect'))
  63.             ->setMethod('POST')
  64.             ->add('apiUrl'TextType::class, ['label' => false'data' => $account ?? ''])
  65.             ->add('apiKey'TextType::class, ['label' => false])
  66.             ->add('clientId'HiddenType::class, ['data' => hash('ripemd160', (string)time())])
  67.             ->getForm()
  68.             ->handleRequest($request);
  69.         if ($form->isSubmitted() && $form->isValid()) {
  70.             $searchExist $this->manager->getRepository(Connection::class)->findOneBy(
  71.                 ['apiUrl' => $connection->getApiUrl(), 'apiKey' => $connection->getApiKey()]
  72.             );
  73.             if (null === $searchExist) {
  74.                 // сейчас может быть только одно подключение
  75.                 $searchExist $this->manager->getRepository(Connection::class)->findOneBy([]);
  76.                 if ($searchExist !== null) {
  77.                     return new Response('Wrong credentials'400);
  78.                 }
  79.                 $client SimpleClientFactory::createClient($connection->getApiUrl(), $connection->getApiKey());
  80.                 $this->clientFacade->setApiClient($client);
  81.                 $resp $this->clientFacade->editConfiguration($request->getHttpHost(), $connection->getClientId());
  82.                 if ($resp['error'] == true) {
  83.                     return new Response($resp['message'], 400);
  84.                 }
  85.                 $this->manager->persist($connection);
  86.                 $this->manager->flush();
  87.                 $request->getSession()->set('clientId'$connection->getClientId());
  88.             } else {
  89.                 $request->getSession()->set('clientId'$searchExist->getClientId());
  90.             }
  91.             return $this->redirectToRoute('connection_settings');
  92.         }
  93.         return $this->render(
  94.             'index.html.twig',
  95.             [
  96.                 'form' => $form->createView(),
  97.                 'account' => $account
  98.             ]
  99.         );
  100.     }
  101.     /**
  102.      * @Route("/settings", name="connection_settings", methods={"GET", "POST"})
  103.      *
  104.      * @return Response
  105.      */
  106.     public function connectionSettings(): Response
  107.     {
  108.         /* @var Connection $connection */
  109.         $connection $this->getUser();
  110.         if (is_null($connection)) {
  111.             return $this->redirectToRoute('connect');
  112.         }
  113.         $render = [];
  114.         $render['formConnection'] = $this->createForm(
  115.             ConnectionType::class,
  116.             $connection,
  117.             [
  118.                 'action' => $this->generateUrl('save_connection'),
  119.             ]
  120.         )->createView();
  121.         return $this->render('settings.html.twig'$render);
  122.     }
  123.     /**
  124.      * @Route("/settings/save/connection", name="save_connection", methods={"POST"})
  125.      * @param Request $request
  126.      * @param TranslatorInterface $translator
  127.      *
  128.      * @return Response
  129.      */
  130.     public function saveConnection(Request $requestTranslatorInterface $translator): Response
  131.     {
  132.         /* @var Connection $connection */
  133.         $connection $this->getUser();
  134.         if (is_null($connection)) {
  135.             return new Response('Authentication is not confirmed'Response::HTTP_FORBIDDEN);
  136.         }
  137.         $form $request->get('connection');
  138.         /** @phpstan-ignore-next-line */
  139.         $connection->setApiUrl($form['apiUrl']);
  140.         /** @phpstan-ignore-next-line */
  141.         $connection->setApiKey($form['apiKey']);
  142.         $this->manager->persist($connection);
  143.         $this->manager->flush();
  144.         return new Response($translator->trans('Connection updated'), Response::HTTP_OK);
  145.     }
  146. }