Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

TreeModelType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 95
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaults() 0 4 1
A buildForm() 0 7 1
A buildView() 0 7 1
A setDefaultOptions() 0 4 1
B configureOptions() 0 26 1
A getName() 0 4 1
A getBlockPrefix() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Form\Type;
13
14
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
15
use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
16
use Symfony\Component\Form\AbstractType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\Form\FormView;
20
use Symfony\Component\OptionsResolver\Options;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
23
24
class TreeModelType extends AbstractType
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $defaults = array();
30
31
    /**
32
     * @param array $defaults
33
     */
34
    public function setDefaults(array $defaults)
35
    {
36
        $this->defaults = $defaults;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function buildForm(FormBuilderInterface $builder, array $options)
43
    {
44
        $builder->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true);
45
        $builder->setAttribute('root_node', $options['root_node']);
46
        $builder->setAttribute('select_root_node', $options['select_root_node']);
47
        $builder->setAttribute('repository_name', $options['repository_name']);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function buildView(FormView $view, FormInterface $form, array $options)
54
    {
55
        $view->vars['root_node'] = $form->getConfig()->getAttribute('root_node');
56
        $view->vars['select_root_node'] = $form->getConfig()->getAttribute('select_root_node');
57
        $view->vars['repository_name'] = $form->getConfig()->getAttribute('repository_name');
58
        $view->vars['routing_defaults'] = $this->defaults;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @todo Remove when Symfony <2.8 is no longer supported
65
     */
66
    public function setDefaultOptions(OptionsResolverInterface $resolver)
67
    {
68
        $this->configureOptions($resolver);
0 ignored issues
show
Documentation introduced by
$resolver is of type object<Symfony\Component...tionsResolverInterface>, but the function expects a object<Symfony\Component...solver\OptionsResolver>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function configureOptions(OptionsResolver $resolver)
75
    {
76
        $resolver->setDefaults(array(
77
            'template' => 'doctrine_phpcr_odm_tree',
78
            'compound' => false,
79
            'model_manager' => null,
80
            'class' => null,
81
            'property' => null,
82
            'query' => null,
83
            'choices' => null,
84
            'root_node' => '/',
85
            'select_root_node' => false,
86
            'parent' => 'choice',
87
            'repository_name' => 'default',
88
            'preferred_choices' => array(),
89
            'choice_list' => function (Options $options, $previousValue) {
0 ignored issues
show
Unused Code introduced by
The parameter $previousValue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
                return new ModelChoiceList(
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList has been deprecated with message: Since 3.x, to be removed in 4.0. Use Sonata\AdminBundle\ModelChoiceLoader instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
91
                    $options['model_manager'],
92
                    $options['class'],
93
                    $options['property'],
94
                    $options['query'],
95
                    $options['choices']
96
                );
97
            },
98
        ));
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     *
104
     * @todo Remove when Symfony <2.8 is no longer supported
105
     */
106
    public function getName()
107
    {
108
        return $this->getBlockPrefix();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function getBlockPrefix()
115
    {
116
        return 'doctrine_phpcr_odm_tree';
117
    }
118
}
119