Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\Form\Extension\Core\Type\TextType;
9
use Symfony\Component\Form\FormEvent;
10
use Symfony\Component\Form\FormEvents;
11
12
class URLChooserFormSubscriber implements EventSubscriberInterface
13
{
14
    use URLValidator;
15
16
    public static function getSubscribedEvents()
17
    {
18
        return [
19
            FormEvents::POST_SET_DATA => 'postSetData',
20
        ];
21
    }
22
23
    /**
24
     * When opening the form for the first time, check the type of URL and set the according fields.
25
     *
26
     * @param FormEvent $event
27
     */
28
    public function postSetData(FormEvent $event)
29
    {
30
        $form = $event->getForm();
31
        $data = $form->getData();
32
33
        $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...
34
35
        if (!empty($data) && $form->has('link_type')) {
36
            // Check if e-mail address
37
            if ($this->isEmailAddress($data)) {
38
                $form->get('link_type')->setData(URLChooserType::EMAIL);
39
            } // Check if internal link
40
            elseif ($this->isInternalLink($data) || $this->isInternalMediaLink($data)) {
41
                $form->get('link_type')->setData(URLChooserType::INTERNAL);
42
                $attributes['choose_url'] = true;
43
            } // Else, it's an external link
44
            else {
45
                $form->get('link_type')->setData(URLChooserType::EXTERNAL);
46
            }
47
        } else {
48
            $choices = $form->get('link_type')->getConfig()->getOption('choices');
49
            $firstOption = array_shift($choices);
50
51 View Code Duplication
            switch ($firstOption) {
52
                case URLChooserType::INTERNAL:
53
                    $attributes['choose_url'] = true;
54
55
                    break;
56
                case URLChooserType::EXTERNAL:
57
                    $attributes['placeholder'] = 'https://';
58
59
                    break;
60
            }
61
62
            $form->get('link_type')->setData($firstOption);
63
        }
64
65
        $form->add(
66
            'link_url',
67
            TextType::class,
68
            [
69
                'label' => 'URL',
70
                'required' => true,
71
                'attr' => $attributes,
72
            ]
73
        );
74
    }
75
}
76