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 ( 220e03...c6ffc1 )
by Christian
01:21
created

HoneypotFormExtension::createViewOptions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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