GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 46f5a7...8e2719 )
by Christian
01:21
created

HoneypotFormExtension::getExtendedTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[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 Core23\AntiSpamBundle\Form\Extension;
13
14
use Core23\AntiSpamBundle\Form\EventListener\AntiSpamHoneypotListener;
15
use Symfony\Component\Form\AbstractTypeExtension;
16
use Symfony\Component\Form\Extension\Core\Type\FormType;
17
use Symfony\Component\Form\Extension\Core\Type\TextType;
18
use Symfony\Component\Form\FormBuilderInterface;
19
use Symfony\Component\Form\FormFactoryInterface;
20
use Symfony\Component\Form\FormInterface;
21
use Symfony\Component\Form\FormView;
22
use Symfony\Component\OptionsResolver\OptionsResolver;
23
use Symfony\Component\Translation\TranslatorInterface;
24
25
final class HoneypotFormExtension extends AbstractTypeExtension
26
{
27
    /**
28
     * @var TranslatorInterface
29
     */
30
    private $translator;
31
32
    /**
33
     * @var array<string, mixed>
34
     */
35
    private $defaults;
36
37
    /**
38
     * @param TranslatorInterface $translator
39
     * @param array               $defaults
40
     */
41
    public function __construct(TranslatorInterface $translator, array $defaults)
42
    {
43
        $this->translator        = $translator;
44
        $this->defaults          = $defaults;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function buildForm(FormBuilderInterface $builder, array $options): void
51
    {
52
        if (!$options['antispam_honeypot']) {
53
            return;
54
        }
55
56
        $builder
57
            ->setAttribute('antispam_honeypot_factory', $builder->getFormFactory())
58
            ->addEventSubscriber(new AntiSpamHoneypotListener($this->translator, $options['core23_antispam_field']));
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function finishView(FormView $view, FormInterface $form, array $options): void
65
    {
66
        if ($view->parent || !$options['antispam_honeypot'] || !$options['compound']) {
67
            return;
68
        }
69
70
        if ($form->has($options['core23_antispam_field'])) {
71
            throw new \RuntimeException(sprintf('Honeypot field "%s" is already used.', $options['core23_antispam_field']));
72
        }
73
74
        $formOptions = [
75
            'mapped'   => false,
76
            'label'    => false,
77
            'required' => false,
78
        ];
79
80
        if (null === $options['antispam_honeypot_class']) {
81
            $formOptions['attr'] = [
82
                'style' => 'display:none',
83
            ];
84
        } else {
85
            $formOptions['attr'] = [
86
                'class' => $options['antispam_honeypot_class'],
87
            ];
88
        }
89
90
        $factory       = $form->getConfig()->getAttribute('antispam_honeypot_factory');
91
92
        if (!$factory instanceof FormFactoryInterface) {
93
            throw new \RuntimeException('Invalid form factory to create a honeyput.');
94
        }
95
96
        $formView = $factory
97
            ->createNamed($options['honeypot_field'], TextType::class, null, $formOptions)
98
            ->createView($view);
99
100
        $view->children[$options['honeypot_field']] = $formView;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function configureOptions(OptionsResolver $resolver): void
107
    {
108
        $resolver
109
            ->setDefaults([
110
                'antispam_honeypot'        => $this->defaults['global'],
111
                'antispam_honeypot_class'  => $this->defaults['class'],
112
                'antispam_honeypot_field'  => $this->defaults['field'],
113
            ])
114
            ->setAllowedTypes('antispam_honeypot', 'bool')
115
            ->setAllowedTypes('antispam_honeypot_class', ['string', 'null'])
116
            ->setAllowedTypes('antispam_honeypot_field', 'string');
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public static function getExtendedTypes(): iterable
123
    {
124
        return [
125
            FormType::class,
126
            ];
127
    }
128
}
129