src/Form/AskRegistrationType.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Model\AskRegistration;
  4. use App\Service\User\FunctionsHelper;
  5. use Oz\ApiNvl\Provider\UserProvider;
  6. use Oz\Toolboxe\Form\Type\RecaptchaType;
  7. use Psr\Cache\InvalidArgumentException;
  8. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  9. use Symfony\Component\Form\AbstractType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  11. use Symfony\Component\Form\FormBuilderInterface;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. use Symfony\Component\Form\Extension\Core\Type\TextType;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Symfony\Contracts\Cache\ItemInterface;
  16. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  18. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  19. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  20. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  21. /**
  22.  *
  23.  */
  24. class AskRegistrationType extends AbstractType
  25. {
  26.     /**
  27.      * @var UserProvider
  28.      */
  29.     private $userProvider;
  30.     /**
  31.      * @var FilesystemAdapter
  32.      */
  33.     private $cache;
  34.     /**
  35.      * @param UserProvider $userProvider
  36.      */
  37.     public function __construct(UserProvider $userProvider)
  38.     {
  39.         $this->userProvider $userProvider;
  40.         $this->cache        = new FilesystemAdapter();
  41.     }
  42.     /**
  43.      * @param FormBuilderInterface $builder
  44.      * @param array                $options
  45.      *
  46.      * @return void
  47.      * @throws ClientExceptionInterface
  48.      * @throws DecodingExceptionInterface
  49.      * @throws RedirectionExceptionInterface
  50.      * @throws ServerExceptionInterface
  51.      * @throws TransportExceptionInterface
  52.      */
  53.     public function buildForm(FormBuilderInterface $builder, array $options): void
  54.     {
  55.         $builder
  56.             ->add('email'TextType::class, [
  57.                 'label'    => 'Email',
  58.                 'required' => false,
  59.                 'attr'     => [
  60.                     'placeholder' => 'louis.durand@monsite.fr',
  61.                 ],
  62.             ])
  63.             ->add('civility'ChoiceType::class, [
  64.                 'label'    => 'Civilité',
  65.                 'required' => true,
  66.                 'choices'  => [
  67.                     'Monsieur' => 1,
  68.                     'Madame'   => 0,
  69.                 ],
  70.             ])
  71.             ->add('lastname'TextType::class, [
  72.                 'label'    => 'Nom',
  73.                 'required' => false,
  74.                 'attr'     => [
  75.                     'placeholder' => 'Durand',
  76.                 ],
  77.             ])
  78.             ->add('firstname'TextType::class, [
  79.                 'label'    => 'Prénom',
  80.                 'required' => false,
  81.                 'attr'     => [
  82.                     'placeholder' => 'Louis',
  83.                 ],
  84.             ])
  85.             ->add('phone'TextType::class, [
  86.                 'label'    => 'Téléphone',
  87.                 'required' => false,
  88.                 'attr'     => [
  89.                     'placeholder' => '0545234589',
  90.                 ],
  91.             ])
  92. //            ->add('fonction', TextType::class, [
  93. //                'label'    => 'Fonction',
  94. //                'required' => false,
  95. //                'attr'     => [
  96. //                    'placeholder' => 'Responsable des ventes',
  97. //                ],
  98. //            ])
  99.             ->add('fonction'ChoiceType::class, [
  100.                 'label'       => 'Fonction',
  101.                 'required'    => false,
  102.                 'placeholder' => 'Choisir une fonction',
  103.                 'choices'     => $this->getFunctions(),
  104.             ])
  105.             ->add('company'TextType::class, [
  106.                 'label'    => 'Société',
  107.                 'required' => false,
  108.                 'attr'     => [
  109.                     'placeholder' => 'Toyota',
  110.                 ],
  111.             ])
  112. //            ->add('adresse', TextareaType::class, [
  113. //                'label'    => 'Adresse',
  114. //                'required' => false,
  115. //                'attr'     => [
  116. //                    'placeholder' => "17 rue du champ de foire\nSaint germain",
  117. //                ],
  118. //            ])
  119.             ->add('siret'TextType::class, [
  120.                 'label'    => 'N° Siret',
  121.                 'required' => false,
  122.                 'attr'     => [
  123.                     'placeholder' => '230457856',
  124.                 ],
  125.             ])
  126.             ->add('recatpchaValue'RecaptchaType::class);
  127.     }
  128.     /**
  129.      * @return array
  130.      * @throws InvalidArgumentException
  131.      */
  132.     private function getFunctions(): array
  133.     {
  134.         return $this->cache->get('user_functions', function (ItemInterface $item) {
  135.             $item->expiresAfter(3600);
  136.             $choices  = [];
  137.             $response $this->userProvider->getFonctions();
  138.             if (200 !== $response->getStatusCode()) {
  139.                 return $choices;
  140.             }
  141.             foreach ($response->toArray() as $rawFunction) {
  142.                 $choices[$rawFunction['caption']] = $rawFunction['caption'];
  143.             }
  144.             asort($choices);
  145.             return $choices;
  146.         });
  147.     }
  148.     /**
  149.      * @param OptionsResolver $resolver
  150.      *
  151.      * @return void
  152.      */
  153.     public function configureOptions(OptionsResolver $resolver): void
  154.     {
  155.         $resolver->setDefaults([
  156.             'data_class' => AskRegistration::class,
  157.         ]);
  158.     }
  159. }