vendor/eo/honeypot-bundle/Form/Type/HoneypotType.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the EoHoneypotBundle package.
  4.  *
  5.  * (c) Eymen Gunay <eymen@egunay.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Eo\HoneypotBundle\Form\Type;
  11. use Eo\HoneypotBundle\Events;
  12. use Eo\HoneypotBundle\Event\BirdInCageEvent;
  13. use Doctrine\Common\Persistence\ObjectManager;
  14. use Eo\HoneypotBundle\Manager\HoneypotManager;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\Form\FormError;
  17. use Symfony\Component\Form\FormEvent;
  18. use Symfony\Component\Form\FormEvents;
  19. use Symfony\Component\Form\AbstractType;
  20. use Symfony\Component\Form\FormBuilderInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Symfony\Component\OptionsResolver\OptionsResolver;
  23. class HoneypotType extends AbstractType
  24. {
  25.     /**
  26.      * @var Symfony\Component\HttpFoundation\RequestStack
  27.      */
  28.     protected $requestStack;
  29.     /**
  30.      * @var Eo\HoneypotBundle\Manager\HoneypotManager
  31.      */
  32.     protected $honeypotManager;
  33.     /**
  34.      * @var Symfony\Component\EventDispatcher\EventDispatcherInterface
  35.      */
  36.     protected $eventDispatcher;
  37.     /**
  38.      * Class constructor
  39.      *
  40.      * @param Symfony\Component\HttpFoundation\RequestStack $requestStack
  41.      * @param Eo\HoneypotBundle\Manager\HoneypotManager $honeypotManager
  42.      * @param Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
  43.      */
  44.     public function __construct(RequestStack $requestStackHoneypotManager $honeypotManagerEventDispatcherInterface $eventDispatcher)
  45.     {
  46.         $this->requestStack $requestStack;
  47.         $this->honeypotManager $honeypotManager;
  48.         $this->eventDispatcher $eventDispatcher;
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function buildForm(FormBuilderInterface $builder, array $options)
  54.     {
  55.         // Closure $this support was removed temporarily from PHP 5.3
  56.         // and re-introduced with 5.4. This small hack is here for 5.3 compability.
  57.         // https://wiki.php.net/rfc/closures/removal-of-this
  58.         // http://php.net/manual/en/migration54.new-features.php
  59.         $request $this->requestStack->getCurrentRequest();
  60.         $honeypotManager $this->honeypotManager;
  61.         $eventDispatcher $this->eventDispatcher;
  62.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) use ($request$honeypotManager$eventDispatcher$options) {
  63.             $data $event->getData();
  64.             $form $event->getForm();
  65.             if (!$data) {
  66.                 return;
  67.             }
  68.             // Create new prey
  69.             $prey $honeypotManager->createNew($request->getClientIp());
  70.             // Dispatch bird.in.cage event
  71.             $eventDispatcher->dispatch(new BirdInCageEvent($prey), Events::BIRD_IN_CAGE);
  72.             // Save prey
  73.             $honeypotManager->save($prey);
  74.             if ($options['causesError']) {
  75.                 $form->getParent()->addError(new FormError('Form is invalid.'));
  76.             }
  77.         });
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function configureOptions(OptionsResolver $resolver)
  83.     {
  84.         $resolver->setDefaults(array(
  85.             'required'    => false,
  86.             'mapped'      => false,
  87.             'data'        => '',
  88.             'causesError' => true,
  89.             'attr'        => array(
  90.                 // autocomplete="off" does not work in some cases, random strings always do
  91.                 'autocomplete' => 'nope',
  92.                 // Make the field unfocusable for keyboard users
  93.                 'tabindex' => -1,
  94.                 // Hide the field from assistive technology like screen readers
  95.                 'aria-hidden' => 'true',
  96.                 // Fake `display:none` css behaviour to hide input
  97.                 // as some bots may also check inputs visibility
  98.                 'style' => 'position: fixed; left: -100%; top: -100%;'
  99.             )
  100.         ));
  101.     }
  102.     /**
  103.      * {@inheritdoc}
  104.      */
  105.     public function getParent()
  106.     {
  107.         return 'Symfony\Component\Form\Extension\Core\Type\TextType';
  108.     }
  109. }