Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Form/EventListener/URLChooserFormSubscriber.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\Form\EventListener;
4
5
use Kunstmaan\NodeBundle\Form\Type\URLChooserType;
6
use Kunstmaan\NodeBundle\Validation\URLValidator;
7
use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrl;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\Form\Extension\Core\Type\TextType;
10
use Symfony\Component\Form\FormEvent;
11
use Symfony\Component\Form\FormEvents;
12
use Symfony\Component\Validator\Constraints\Email;
13
use Symfony\Component\Validator\Constraints\Url;
14
15
class URLChooserFormSubscriber implements EventSubscriberInterface
16
{
17
    use URLValidator;
18
19
    public static function getSubscribedEvents()
20
    {
21
        return [
22
            FormEvents::POST_SET_DATA => 'postSetData',
23
            FormEvents::PRE_SUBMIT => 'preSubmit',
24
        ];
25
    }
26
27
    /**
28
     * When opening the form for the first time, check the type of URL and set the according fields.
29
     *
30
     * @param FormEvent $event
31
     */
32
    public function postSetData(FormEvent $event)
33
    {
34
        $this->formModifier($event);
35
    }
36
37
    public function preSubmit(FormEvent $event)
38
    {
39
        $this->formModifier($event);
40
    }
41
42
    private function formModifier(FormEvent $event)
43
    {
44
        $form = $event->getForm();
45
        $data = $form->getData();
46
47
        $constraints = [];
48
        $attributes['class'] = 'js-change-urlchooser';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
49
50
        if (!empty($data) && $form->has('link_type')) {
51
            // Check if e-mail address
52
            if ($this->isEmailAddress($data)) {
53
                $form->get('link_type')->setData(URLChooserType::EMAIL);
54
                $constraints[] = new Email();
55
            } // Check if internal link
56
            elseif ($this->isInternalLink($data) || $this->isInternalMediaLink($data)) {
57
                $form->get('link_type')->setData(URLChooserType::INTERNAL);
58
                $attributes['choose_url'] = true;
59
            } // Else, it's an external link
60
            else {
61
                $form->get('link_type')->setData(URLChooserType::EXTERNAL);
62
                $constraints[] = new ValidExternalUrl();
63
            }
64
        } else {
65
            $choices = $form->get('link_type')->getConfig()->getOption('choices');
66
            $firstOption = array_shift($choices);
67
68 View Code Duplication
            switch ($firstOption) {
69
                case URLChooserType::INTERNAL:
70
                    $attributes['choose_url'] = true;
71
72
                    break;
73
                case URLChooserType::EXTERNAL:
74
                    $attributes['placeholder'] = 'https://';
75
                    $constraints[] = new ValidExternalUrl();
76
77
                    break;
78
                case URLChooserType::EMAIL:
79
                    $constraints[] = new Email();
80
81
                    break;
82
            }
83
84
            $form->get('link_type')->setData($firstOption);
85
        }
86
87
        $form->add(
88
            'link_url',
89
            TextType::class,
90
            [
91
                'label' => 'URL',
92
                'required' => true,
93
                'attr' => $attributes,
94
                'constraints' => $constraints,
95
                'error_bubbling' => true,
96
            ]
97
        );
98
    }
99
}
100