<?php
/**
* @author Antony Tkachenko <at@canaryknight.ru>
*/
namespace App\Controller;
use App\Event\UtmSubscriber;
use App\Form\Dto\BlogFeedback;
use App\Form\Dto\Registration;
use App\Exception\ValidationException;
use App\Form\Dto\RegistrationConsultation;
use App\Form\Dto\RegistrationInfo;
use App\Form\Dto\WalletDemo;
use App\Form\FormHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/form", name="form_", methods={"POST"}, defaults={"_format": "json"})
*/
class FormController extends AbstractController
{
private $gmbBaseHost;
/** @var FormHandler */
private $formHandler;
public function __construct(
string $gmbBaseHost,
FormHandler $formHandler
) {
$this->gmbBaseHost = $gmbBaseHost;
$this->formHandler = $formHandler;
}
private function getData(Request $request)
{
$data = $request->request->all();
if (empty($data)) {
try {
$data = $request->toArray();
} catch (\Throwable $e) {
$data = [];
}
}
return $data;
}
/**
* @Route("/registration", name="registration")
*/
public function registration(Request $request)
{
try {
$dto = $this->formHandler->handle(Registration::class, $this->getData($request), $request->attributes->get(UtmSubscriber::UTM_COOKIE_NAME));
} catch (ValidationException $e) {
return $this->json([
'success' => false,
'error' => 'Исправьте ошибки',
'validation' => $e->getErrorList(),
]);
} catch (\Throwable $e) {
return $this->json([
'success' => false,
'error' => $e->getMessage() . 'Неизвестная ошибка',
]);
}
/** @var $dto Registration::class */
return $this->json([
'success' => true,
'data' => [
'host' => "https://{$dto->org}.{$this->gmbBaseHost}",
'login' => 'admin',
'password' => $dto->password,
],
]);
}
/**
* @Route("/registration/info", name="registration_info")
*/
public function registrationInfo(Request $request)
{
try {
$this->formHandler->handle(RegistrationInfo::class, $this->getData($request), $request->attributes->get(UtmSubscriber::UTM_COOKIE_NAME));
} catch (ValidationException $e) {
return $this->json([
'success' => false,
'error' => 'Исправьте ошибки',
'validation' => $e->getErrorList(),
]);
} catch (\Throwable $e) {
return $this->json([
'success' => false,
'error' => 'Неизвестная ошибка',
]);
}
return $this->json([
'success' => true,
]);
}
/**
* @Route("/registration/consultation", name="registration_consultation")
*/
public function registrationConsultation(Request $request)
{
try {
$this->formHandler->handle(RegistrationConsultation::class, $this->getData($request), $request->attributes->get(UtmSubscriber::UTM_COOKIE_NAME));
} catch (ValidationException $e) {
return $this->json([
'success' => false,
'error' => 'Исправьте ошибки',
'validation' => $e->getErrorList(),
]);
} catch (\Throwable $e) {
return $this->json([
'success' => false,
'error' => 'Неизвестная ошибка',
]);
}
return $this->json([
'success' => true,
]);
}
/**
* @Route("/wallet/demo", name="wallet_demo")
*/
public function walletDemo(Request $request): JsonResponse
{
try {
$this->formHandler->handle(WalletDemo::class, $this->getData($request), $request->attributes->get(UtmSubscriber::UTM_COOKIE_NAME));
} catch (ValidationException $e) {
return $this->json([
'success' => false,
'error' => 'Ошибка',
'validation' => $e->getErrorList(),
]);
} catch (\Throwable $e) {
return $this->json([
'success' => false,
'error' => 'Неизвестная ошибка',
]);
}
return $this->json([
'success' => true,
]);
}
/**
* @Route("/blog-feedback", name="blog_feedback")
*/
public function blogFeedback(Request $request)
{
try {
$this->formHandler->handle(BlogFeedback::class, $this->getData($request), $request->attributes->get(UtmSubscriber::UTM_COOKIE_NAME));
} catch (ValidationException $e) {
return $this->json([
'success' => false,
'error' => 'Ошибка',
'validation' => $e->getErrorList(),
]);
} catch (\Throwable $e) {
return $this->json([
'success' => false,
'error' => 'Неизвестная ошибка',
]);
}
return $this->json([
'success' => true,
]);
}
}