AdhesionController.php 4.43 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6
<?php

namespace App\Controller;

use App\Entity\Adherent;
use App\Entity\Payment;
7
use App\Entity\Usergroup;
Julien Jorry committed
8
use App\Form\Type\AdhererFormType;
9 10
use App\Security\LoginAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
11
use Gamez\Symfony\Component\Serializer\Normalizer\UuidNormalizer;
Julien Jorry committed
12 13 14
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
15 16
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
17
use Symfony\Component\Serializer\Encoder\JsonEncoder;
Julien Jorry committed
18
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
19 20
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
Julien Jorry committed
21 22 23

class AdhesionController extends AbstractController
{
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    private $em;
    private $authenticator;
    private $guardHandler;

    public function __construct(
        EntityManagerInterface $em,
        RouterInterface $router,
        LoginAuthenticator $authenticator,
        GuardAuthenticatorHandler $guardHandler)
    {
        $this->em = $em;
        $this->router = $router;
        $this->authenticator = $authenticator;
        $this->guardHandler = $guardHandler;
    }

Julien Jorry committed
40 41 42 43 44
    /**
     * @Route("/adherer", name="adherer")
     */
    public function adhererAction(Request $request)
    {
Damien Moulard committed
45 46 47 48
        if ($this->getParameter('tav_env') == true) {
            return $this->redirect($this->router->generate('index'));
        }

Julien Jorry committed
49 50 51 52 53 54 55
        $adherent = new Adherent();

        $form = $this->createForm(AdhererFormType::class, $adherent);
        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            if ($form->isValid()) {
56
                // CB PAIEMENT USE PAYZEN
57
                if ($form->has('save') && $form->get('save')->isClicked()) {
58
                    $adherentNew = $form->getData();
Julien Jorry committed
59

60 61 62 63
                    // Serialize form data in json to store with payment object and persist when payment is valid
                    $encoders = [new JsonEncoder()];
                    $normalizers = [new UuidNormalizer(), new ObjectNormalizer()];
                    $serializer = new Serializer($normalizers, $encoders);
64

65 66 67 68 69 70 71 72 73 74 75 76
                    $data = $serializer->normalize(
                        $adherentNew,
                        null,
                        [AbstractNormalizer::ATTRIBUTES => ['user' => ['username', 'email', 'firstname', 'lastname', 'plainPassword'],
                            'groupe' => ['id' => 'uuid'],
                            'geoloc' => ['adresse',
                                'cpostal',
                                'ville', ], ]]
                    );
                    $jsondata = $serializer->serialize($data, 'json');

                    // Redirect to payment page
77
                    return $this->forward('App\Controller\PaymentController::preparePaymentAction', [
78 79 80 81 82
                        'form' => $form,
                        'type' => Payment::TYPE_ADHESION,
                        'extra_data' => $jsondata,
                    ]);
                // HELLOASSO PAIEMENT
83
                } elseif ($form->has('saveHelloAsso') && $form->get('saveHelloAsso')->isClicked()) {
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
                    $adherentNew = $form->getData();
                    $usergroup = $this->em->getRepository(Usergroup::class)->findOneByName('Adherent');
                    $adherentNew->getUser()->addPossiblegroup($usergroup);
                    $adherentNew->getUser()->addGroup($usergroup);
                    $adherentNew->getUser()->setAdherent($adherentNew);
                    $adherentNew->getUser()->setEnabled(true);

                    $this->em->persist($adherentNew);
                    $this->em->flush();

                    // Connect new user
                    $this->guardHandler->authenticateUserAndHandleSuccess(
                        $adherentNew->getUser(),
                        $request,
                        $this->authenticator,
                        'main'
                    );
Julien Jorry committed
101

102 103
                    return $this->redirect($this->router->generate('index') . '?showmlcadhesionmodal=1');
                }
Julien Jorry committed
104 105 106
            } else {
                $this->addFlash(
                    'error',
Julien Jorry committed
107
                    'Veuillez vérifier les informations du formulaire !'
Julien Jorry committed
108 109 110 111 112 113 114 115 116
                );
            }
        }

        return $this->render('@kohinos/adherent/adherer.html.twig', [
            'form' => $form->createView(),
        ]);
    }
}