EntityType::getName()   A
last analyzed

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 0
1
<?php
2
3
namespace DoS\ResourceBundle\Form\Type;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use DoS\ResourceBundle\Form\DataTransformer\IdentifierToObjectTransformer;
7
use Symfony\Component\Form\AbstractType;
8
use Symfony\Component\Form\Exception\LogicException;
9
use Symfony\Component\Form\FormBuilderInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\Form\FormView;
12
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
13
14
class EntityType extends AbstractType
15
{
16
    /**
17
     * Manager registry.
18
     *
19
     * @var ManagerRegistry
20
     */
21
    protected $manager;
22
23
    public function __construct(ManagerRegistry $manager)
24
    {
25
        $this->manager = $manager;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function buildForm(FormBuilderInterface $builder, array $options)
32
    {
33
        if (!$options['class']) {
34
            throw new LogicException('Option "class" must be set.');
35
        }
36
37
        $modelTransformer = new IdentifierToObjectTransformer(
38
            $this->manager->getRepository($options['class']),
39
            $options['identifier']
40
        );
41
42
        $builder
43
            ->addModelTransformer($modelTransformer)
44
        ;
45
    }
46
47
    public function buildView(FormView $view, FormInterface $form, array $options)
48
    {
49
        $view->vars['model'] = $form->getData();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function setDefaultOptions(OptionsResolverInterface $resolver)
56
    {
57
        $resolver->setDefaults(array(
58
                'identifier' => 'id',
59
                'empty_data' => null,
60
                'class' => null,
61
            ))
62
        ;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getParent()
69
    {
70
        return 'text';
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getName()
77
    {
78
        return 'dos_entity';
79
    }
80
}
81