Completed
Push — 2.x ( 359f6e )
by Sullivan
16:25 queued 14:10
created

FormContractor::getDefaultOptions()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 50
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 0
loc 50
rs 5.7647
c 2
b 0
f 2
cc 10
eloc 27
nc 10
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Sonata 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\DoctrineORMAdminBundle\Builder;
13
14
use Sonata\AdminBundle\Admin\AdminInterface;
15
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
16
use Sonata\AdminBundle\Builder\FormContractorInterface;
17
18
use Symfony\Component\Form\FormFactoryInterface;
19
20
use Doctrine\ORM\Mapping\ClassMetadataInfo;
21
22
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription;
23
24
class FormContractor implements FormContractorInterface
25
{
26
    protected $fieldFactory;
27
28
    /**
29
     * @param \Symfony\Component\Form\FormFactoryInterface $formFactory
30
     */
31
    public function __construct(FormFactoryInterface $formFactory)
32
    {
33
        $this->formFactory = $formFactory;
0 ignored issues
show
Bug introduced by
The property formFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
40
    {
41
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
42
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
43
44
            // set the default field mapping
45
            if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
46
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
47
            }
48
49
            // set the default association mapping
50
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
51
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
52
            }
53
        }
54
55
        if (!$fieldDescription->getType()) {
56
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
57
        }
58
59
        $fieldDescription->setAdmin($admin);
60
        $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
61
62
        if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY, ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE ))) {
63
            $admin->attachAdminClass($fieldDescription);
64
        }
65
    }
66
67
    /**
68
     * @return \Symfony\Component\Form\FormFactoryInterface
69
     */
70
    public function getFormFactory()
71
    {
72
        return $this->formFactory;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getFormBuilder($name, array $options = array())
79
    {
80
        return $this->getFormFactory()->createNamedBuilder($name, 'form', null, $options);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
87
    {
88
        $options                             = array();
89
        $options['sonata_field_description'] = $fieldDescription;
90
91
        if (in_array($type, array('sonata_type_model', 'sonata_type_model_list', 'sonata_type_model_hidden', 'sonata_type_model_autocomplete'))) {
92
93
            if ($fieldDescription->getOption('edit') == 'list') {
94
                throw new \LogicException('The ``sonata_type_model`` type does not accept an ``edit`` option anymore, please review the UPGRADE-2.1.md file from the SonataAdminBundle');
95
            }
96
97
            $options['class']         = $fieldDescription->getTargetEntity();
98
            $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
99
100
            if ($type == 'sonata_type_model_autocomplete') {
101
                if (!$fieldDescription->getAssociationAdmin()) {
102
                    throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity: `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity()));
103
                }
104
            }
105
106
        } elseif ($type == 'sonata_type_admin') {
107
108
            if (!$fieldDescription->getAssociationAdmin()) {
109
                throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity()));
110
            }
111
112
            if (!in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE ))) {
113
                throw new \RuntimeException(sprintf('You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or  Many-To-One. Maybe you want `sonata_model_list` instead?', $fieldDescription->getName()));
114
            }
115
116
            $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
117
            $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
118
119
        } elseif ($type == 'sonata_type_collection') {
120
121
            if (!$fieldDescription->getAssociationAdmin()) {
122
                throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity()));
123
            }
124
125
            $options['type']         = 'sonata_type_admin';
126
            $options['modifiable']   = true;
127
            $options['type_options'] = array(
128
                'sonata_field_description' => $fieldDescription,
129
                'data_class'               => $fieldDescription->getAssociationAdmin()->getClass()
130
            );
131
132
        }
133
134
        return $options;
135
    }
136
}
137