Test Failed
Push — release/4.x ( 1432ca...131f10 )
by Erik
05:53
created

UrlType   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A buildForm() 0 14 4
A getBlockPrefix() 0 3 1
A configureOptions() 0 11 1
A finishView() 0 5 1
A getParent() 0 3 1
A getName() 0 3 1
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\UrlBundle\Type;
7
8
use Symfony\Component\Form\AbstractType;
9
use Symfony\Component\Form\Extension\Core\Type\TextType;
10
use Symfony\Component\Form\FormBuilderInterface;
11
use Symfony\Component\Form\FormInterface;
12
use Symfony\Component\Form\FormView;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
use Zicht\Bundle\UrlBundle\Aliasing\Aliasing;
15
use Zicht\Bundle\UrlBundle\Form\DataTransformer\TextTransformer;
16
17
/**
18
 * Type for choosing an URL
19
 */
20
class UrlType extends AbstractType
21
{
22
    /**
23
     * @var Aliasing
24
     */
25
    private $aliasing;
26
27
    /**
28
     * UrlType constructor.
29
     *
30
     * @param Aliasing $aliasing
31
     */
32
    public function __construct(Aliasing $aliasing)
33
    {
34
        $this->aliasing = $aliasing;
35
    }
36
37
    /**
38
     * @{inheritDoc}
39
     */
40
    public function getName()
41
    {
42
        return 'zicht_url';
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function getBlockPrefix()
49
    {
50
        return 'zicht_url';
51
    }
52
53
    /**
54
     * @{inheritDoc}
55
     */
56
    public function configureOptions(OptionsResolver $resolver)
57
    {
58
        parent::configureOptions($resolver);
59
60
        $resolver
61
            ->setDefaults(
62
                array(
63
                    'with_edit_button'      => true,
64
                    'no_transform_public'   => false,
65
                    'no_transform_internal' => false,
66
                    'url_suggest'           => '/admin/url/suggest',
67
                )
68
            );
69
    }
70
71
    /**
72
     * @{inheritDoc}
73
     */
74
    public function finishView(FormView $view, FormInterface $form, array $options)
75
    {
76
        parent::finishView($view, $form, $options);
77
        $view->vars['url_suggest'] = $options['url_suggest'];
78
        $view->vars['with_edit_button'] = $options['with_edit_button'];
79
    }
80
81
    /**
82
     * @{inheritDoc}
83
     */
84
    public function getParent()
85
    {
86
        return TextType::class;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function buildForm(FormBuilderInterface $builder, array $options)
93
    {
94
        $mode = TextTransformer::MODE_TO_INTERNAL|TextTransformer::MODE_TO_PUBLIC;
95
96
        if ($options['no_transform_public']) {
97
            $mode ^= TextTransformer::MODE_TO_PUBLIC;
98
        }
99
100
        if ($options['no_transform_internal']) {
101
            $mode ^= TextTransformer::MODE_TO_INTERNAL;
102
        }
103
104
        if ($mode > 0) {
105
            $builder->addModelTransformer(new TextTransformer($this->aliasing, $mode));
106
        }
107
    }
108
}
109