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
Pull Request — master (#14)
by
unknown
12:52
created

FormFactory::addHelpMessageProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Linio\DynamicFormBundle\Form;
4
5
use Linio\DynamicFormBundle\DataProvider;
6
use Linio\DynamicFormBundle\HelpMessageProvider;
7
use Linio\DynamicFormBundle\Exception\NonExistentFormException;
8
use Linio\DynamicFormBundle\Exception\NotExistentDataProviderException;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\Form\FormBuilderInterface;
11
use Symfony\Component\Form\FormFactory as SymfonyFormFactory;
12
use Symfony\Component\Form\FormInterface;
13
14
class FormFactory
15
{
16
    /**
17
     * @var SymfonyFormFactory
18
     */
19
    protected $formFactory;
20
21
    /**
22
     * @var array
23
     */
24
    protected $configuration;
25
26
    /**
27
     * @var DataProvider[]
28
     */
29
    protected $dataProviders = [];
30
31
    /**
32
     * @var array
33
     */
34
    protected $eventSubscribers = [];
35
36
    /**
37
     * @var HelpMessageProvider[]
38
     */
39
    protected $helpMessageProviders = [];
40
41
    /**
42
     * @param SymfonyFormFactory $formFactory
43
     */
44
    public function setFormFactory(SymfonyFormFactory $formFactory)
45
    {
46
        $this->formFactory = $formFactory;
47
    }
48
49
    /**
50
     * @param array $configuration
51
     */
52
    public function setConfiguration(array $configuration)
53
    {
54
        $this->configuration = $configuration;
55
    }
56
57
    /**
58
     * @param string       $alias
59
     * @param DataProvider $dataProvider
60
     */
61
    public function addDataProvider($alias, DataProvider $dataProvider)
62
    {
63
        $this->dataProviders[$alias] = $dataProvider;
64
    }
65
66
    /**
67
     * @param string $alias
68
     * @param HelpMessageProvider $helpMessageProvider
69
     */
70
    public function addHelpMessageProvider($alias, HelpMessageProvider $helpMessageProvider)
71
    {
72
        $this->helpMessageProviders[$alias] = $helpMessageProvider;
73
    }
74
75
    /**
76
     * @param string                   $formName
77
     * @param EventSubscriberInterface $eventSubscriber
78
     */
79
    public function addEventSubscriber($formName, EventSubscriberInterface $eventSubscriber)
80
    {
81
        if (!isset($this->eventSubscribers[$formName])) {
82
            $this->eventSubscribers[$formName] = [];
83
        }
84
85
        $this->eventSubscribers[$formName][] = $eventSubscriber;
86
    }
87
88
    /**
89
     * @param string $key     The key of the Form in the form configuration
90
     * @param array  $data
91
     * @param array  $options
92
     * @param string $name    An name for the form. If empty, the key will be used
93
     *
94
     * @return FormInterface
95
     */
96
    public function createForm($key, $data = [], $options = [], $name = null)
97
    {
98
        return $this->createBuilder($key, $data, $options, $name)->getForm();
99
    }
100
101
    /**
102
     * This method generates a form based on the configuration file.
103
     *
104
     * @param string $key     The key of the Form in the form configuration
105
     * @param array  $data
106
     * @param array  $options
107
     * @param string $name    An name for the form. If empty, the key will be used
108
     *
109
     * @return FormBuilderInterface
110
     *
111
     * @throws NonExistentFormException
112
     */
113
    public function createBuilder($key, $data = [], $options = [], $name = null)
114
    {
115
        if (!isset($this->configuration[$key])) {
116
            throw new NonExistentFormException(sprintf('The form "%s" was not found.', $key));
117
        }
118
119
        $formBuilder = $this->formFactory->createNamedBuilder($name ?: $key, 'form', $data, $options);
120
121
        if (isset($this->eventSubscribers[$key])) {
122
            foreach ($this->eventSubscribers[$key] as $eventSubscriber) {
123
                $formBuilder->addEventSubscriber($eventSubscriber);
124
            }
125
        }
126
127
        foreach ($this->configuration[$key] as $key => $fieldConfiguration) {
128
            if (!$fieldConfiguration['enabled']) {
129
                continue;
130
            }
131
132
            $fieldOptions = isset($fieldConfiguration['options']) ? $fieldConfiguration['options'] : [];
133
134
            if (isset($fieldConfiguration['data_provider'])) {
135
                $fieldOptions['choices'] = $this->loadDataProvider($fieldConfiguration['data_provider'])->getData();
136
            }
137
138
            if (isset($fieldConfiguration['help_message_provider'])) {
139
                $fieldOptions['help'] = $this->loadHelpMessageProvider($fieldConfiguration['help_message_provider'])->getHelpMessage($fieldOptions['help']);
140
            }
141
142
            if (isset($fieldConfiguration['validation'])) {
143
                $constraints = [];
144
145
                foreach ($fieldConfiguration['validation'] as $validatorName => $options) {
146
                    $constraints[] = new $validatorName($options);
147
                }
148
149
                $fieldOptions['constraints'] = $constraints;
150
            }
151
152
            $field = $formBuilder->create($key, $fieldConfiguration['type'], $fieldOptions);
153
154
            if (isset($fieldConfiguration['transformer'])) {
155
                $transformerConfiguration = $fieldConfiguration['transformer'];
156
                $transformer = new $transformerConfiguration['class']();
157
158
                if (isset($transformerConfiguration['calls'])) {
159
                    foreach ($transformerConfiguration['calls'] as $call) {
160
                        call_user_func([$transformer, $call[0]], $call[1]);
161
                    }
162
                }
163
164
                $field->addModelTransformer($transformer);
165
            }
166
167
            $formBuilder->add($field);
168
        }
169
170
        return $formBuilder;
171
    }
172
173
    /**
174
     * @param string $alias
175
     *
176
     * @return DataProvider
177
     *
178
     * @throws NotExistentDataProviderException
179
     */
180
    public function loadDataProvider($alias)
181
    {
182
        if (!isset($this->dataProviders[$alias])) {
183
            throw new NotExistentDataProviderException();
184
        }
185
186
        return $this->dataProviders[$alias];
187
    }
188
189
    /**
190
     * @param string $alias
191
     *
192
     * @return HelpMessageProvider
193
     *
194
     * @throws NotExistentDataProviderException
195
     */
196
    public function loadHelpMessageProvider($alias)
197
    {
198
        if (!isset($this->helpMessageProviders[$alias])) {
199
            throw new NotExistentDataProviderException();
200
        }
201
202
        return $this->helpMessageProviders[$alias];
203
    }
204
205
    /**
206
     * @param string $name
207
     *
208
     * @return array
209
     *
210
     * @throws NonExistentFormException
211
     */
212
    public function getConfiguration($name = null)
213
    {
214
        if ($name === null) {
215
            return $this->configuration;
216
        }
217
218
        if (!$this->has($name)) {
219
            throw new NonExistentFormException();
220
        }
221
222
        return $this->configuration[$name];
223
    }
224
225
    /**
226
     * Checks if a given form exists.
227
     *
228
     * @param string $name
229
     *
230
     * @return bool
231
     */
232
    public function has($name)
233
    {
234
        return isset($this->configuration[$name]);
235
    }
236
}
237