HoneypotType::getParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the EoHoneypotBundle package.
5
 *
6
 * (c) Eymen Gunay <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eo\HoneypotBundle\Form\Type;
13
14
use Eo\HoneypotBundle\Events;
15
use Eo\HoneypotBundle\Event\BirdInCageEvent;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Eo\HoneypotBundle\Manager\HoneypotManager;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\Form\FormError;
20
use Symfony\Component\Form\FormEvent;
21
use Symfony\Component\Form\FormEvents;
22
use Symfony\Component\Form\AbstractType;
23
use Symfony\Component\Form\FormBuilderInterface;
24
use Symfony\Component\HttpFoundation\RequestStack;
25
use Symfony\Component\OptionsResolver\OptionsResolver;
26
27
class HoneypotType extends AbstractType
28
{
29
    /**
30
     * @var Symfony\Component\HttpFoundation\RequestStack
31
     */
32
    protected $requestStack;
33
34
    /**
35
     * @var Eo\HoneypotBundle\Manager\HoneypotManager
36
     */
37
    protected $honeypotManager;
38
39
    /**
40
     * @var Symfony\Component\EventDispatcher\EventDispatcherInterface
41
     */
42
    protected $eventDispatcher;
43
44
    /**
45
     * Class constructor
46
     *
47
     * @param Symfony\Component\HttpFoundation\RequestStack $requestStack
48
     * @param Eo\HoneypotBundle\Manager\HoneypotManager $honeypotManager
49
     * @param Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
50
     */
51
    public function __construct(RequestStack $requestStack, HoneypotManager $honeypotManager, EventDispatcherInterface $eventDispatcher)
52
    {
53
        $this->requestStack = $requestStack;
0 ignored issues
show
Documentation Bug introduced by
It seems like $requestStack of type object<Symfony\Component...oundation\RequestStack> is incompatible with the declared type object<Eo\HoneypotBundle...oundation\RequestStack> of property $requestStack.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        $this->honeypotManager = $honeypotManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $honeypotManager of type object<Eo\HoneypotBundle\Manager\HoneypotManager> is incompatible with the declared type object<Eo\HoneypotBundle...anager\HoneypotManager> of property $honeypotManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
        $this->eventDispatcher = $eventDispatcher;
0 ignored issues
show
Documentation Bug introduced by
It seems like $eventDispatcher of type object<Symfony\Component...entDispatcherInterface> is incompatible with the declared type object<Eo\HoneypotBundle...entDispatcherInterface> of property $eventDispatcher.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function buildForm(FormBuilderInterface $builder, array $options)
62
    {
63
        // Closure $this support was removed temporarily from PHP 5.3
64
        // and re-introduced with 5.4. This small hack is here for 5.3 compability.
65
        // https://wiki.php.net/rfc/closures/removal-of-this
66
        // http://php.net/manual/en/migration54.new-features.php
67
        $request = $this->requestStack->getCurrentRequest();
68
        $honeypotManager = $this->honeypotManager;
69
        $eventDispatcher = $this->eventDispatcher;
70
71
        $builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) use ($request, $honeypotManager, $eventDispatcher, $options) {
72
            $data = $event->getData();
73
            $form = $event->getForm();
74
75
            if (!$data) {
76
                return;
77
            }
78
79
            // Create new prey
80
            $prey = $honeypotManager->createNew($request->getClientIp());
81
82
            // Dispatch bird.in.cage event
83
            $eventDispatcher->dispatch(Events::BIRD_IN_CAGE, new BirdInCageEvent($prey));
84
85
            // Save prey
86
            $honeypotManager->save($prey);
87
88
            if ($options['causesError']) {
89
                $form->getParent()->addError(new FormError('Form is invalid.'));
90
            }
91
        });
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function configureOptions(OptionsResolver $resolver)
98
    {
99
        $resolver->setDefaults(array(
100
            'required'    => false,
101
            'mapped'      => false,
102
            'data'        => '',
103
            'causesError' => true,
104
            'attr'        => array(
105
                // autocomplete="off" does not work in some cases, random strings always do
106
                'autocomplete' => 'nope',
107
                // Make the field unfocusable for keyboard users
108
                'tabindex' => -1,
109
                // Hide the field from assistive technology like screen readers
110
                'aria-hidden' => 'true',
111
                // Fake `display:none` css behaviour to hide input
112
                // as some bots may also check inputs visibility
113
                'style' => 'position: fixed; left: -100%; top: -100%;'
114
            )
115
        ));
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getParent()
122
    {
123
        return 'Symfony\Component\Form\Extension\Core\Type\TextType';
124
    }
125
}
126