Completed
Push — 3.x ( 67f846...7bb2af )
by Grégoire
04:58
created

FormContractor::getDefaultOptions()   D

Complexity

Conditions 14
Paths 10

Size

Total Lines 85
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 4.9516
c 0
b 0
f 0
cc 14
eloc 56
nc 10
nop 2

How to fix   Long Method    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 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\DoctrineMongoDBAdminBundle\Builder;
13
14
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
17
use Sonata\AdminBundle\Builder\FormContractorInterface;
18
use Sonata\AdminBundle\Form\Type\AdminType;
19
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
20
use Sonata\AdminBundle\Form\Type\ModelHiddenType;
21
use Sonata\AdminBundle\Form\Type\ModelListType;
22
use Sonata\AdminBundle\Form\Type\ModelType;
23
use Sonata\AdminBundle\Form\Type\ModelTypeList;
24
use Sonata\CoreBundle\Form\Type\CollectionType;
25
use Symfony\Component\Form\Extension\Core\Type\FormType;
26
use Symfony\Component\Form\FormFactoryInterface;
27
28
class FormContractor implements FormContractorInterface
29
{
30
    /**
31
     * @deprecated since version 3.x, to be removed in 4.0
32
     *
33
     * @var FormFactoryInterface
34
     */
35
    protected $fieldFactory;
36
37
    /**
38
     * @var FormFactoryInterface
39
     */
40
    private $formFactory;
41
42
    public function __construct(FormFactoryInterface $formFactory)
43
    {
44
        $this->formFactory = $formFactory;
45
    }
46
47
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
48
    {
49
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
50
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
51
52
            // set the default field mapping
53
            if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
54
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
55
            }
56
57
            // set the default association mapping
58
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
59
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
60
            }
61
        }
62
63
        if (!$fieldDescription->getType()) {
64
            throw new \RuntimeException(sprintf(
65
                'Please define a type for field `%s` in `%s`',
66
                $fieldDescription->getName(),
67
                get_class($admin)
68
            ));
69
        }
70
71
        $fieldDescription->setAdmin($admin);
72
        $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
73
74
        if (in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE, ClassMetadataInfo::MANY])) {
75
            $admin->attachAdminClass($fieldDescription);
76
        }
77
    }
78
79
    /**
80
     * @return FormFactoryInterface
81
     */
82
    public function getFormFactory()
83
    {
84
        return $this->formFactory;
85
    }
86
87
    public function getFormBuilder($name, array $options = [])
88
    {
89
        return $this->getFormFactory()->createNamedBuilder($name, FormType::class, null, $options);
90
    }
91
92
    public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
93
    {
94
        $options = [];
95
        $options['sonata_field_description'] = $fieldDescription;
96
97
        // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony 2.8
98
        if (in_array($type, [
99
            'sonata_type_model',
100
            'sonata_type_model_list',
101
            'sonata_type_model_hidden',
102
            'sonata_type_model_autocomplete',
103
        ], true) || $this->checkFormClass($type, [
104
            ModelType::class,
105
            ModelTypeList::class,
106
            ModelListType::class,
107
            ModelHiddenType::class,
108
            ModelAutocompleteType::class,
109
        ])) {
110
            if ('list' === $fieldDescription->getOption('edit')) {
111
                throw new \LogicException(
112
                    'The ``Sonata\AdminBundle\Form\Type\ModelType`` type does not accept an ``edit`` option anymore,'
113
                    .' please review the UPGRADE-2.1.md file from the SonataAdminBundle'
114
                );
115
            }
116
117
            $options['class'] = $fieldDescription->getTargetEntity();
118
            $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
119
120
            // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony 2.8
121
            if ('sonata_type_model_autocomplete' === $type || $this->checkFormClass($type, [ModelAutocompleteType::class])) {
122
                if (!$fieldDescription->getAssociationAdmin()) {
123
                    throw new \RuntimeException(sprintf(
124
                        'The current field `%s` is not linked to an admin.'
125
                        .' Please create one for the target entity: `%s`',
126
                        $fieldDescription->getName(),
127
                        $fieldDescription->getTargetEntity()
128
                    ));
129
                }
130
            }
131
            // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony 2.8
132
        } elseif ('sonata_type_admin' === $type || $this->checkFormClass($type, [AdminType::class])) {
133
            if (!$fieldDescription->getAssociationAdmin()) {
134
                throw new \RuntimeException(sprintf(
135
                    'The current field `%s` is not linked to an admin.'
136
                    .' Please create one for the target entity : `%s`',
137
                    $fieldDescription->getName(),
138
                    $fieldDescription->getTargetEntity()
139
                ));
140
            }
141
142
            if (!in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE, ClassMetadataInfo::MANY])) {
143
                throw new \RuntimeException(sprintf(
144
                    'You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or  Many-To-One.'
145
                    .' Maybe you want `sonata_type_collection` instead?',
146
                    $fieldDescription->getName()
147
                ));
148
            }
149
150
            // set sensitive default value to have a component working fine out of the box
151
            $options['btn_add'] = false;
152
            $options['delete'] = false;
153
154
            $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
155
            $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
156
        // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony 2.8
157
        } elseif ('sonata_type_collection' === $type || $this->checkFormClass($type, [CollectionType::class])) {
158
            if (!$fieldDescription->getAssociationAdmin()) {
159
                throw new \RuntimeException(sprintf(
160
                    'The current field `%s` is not linked to an admin.'
161
                    .' Please create one for the target entity : `%s`',
162
                    $fieldDescription->getName(),
163
                    $fieldDescription->getTargetEntity()
164
                ));
165
            }
166
167
            $options['type'] = AdminType::class;
168
            $options['modifiable'] = true;
169
            $options['type_options'] = [
170
                'sonata_field_description' => $fieldDescription,
171
                'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
172
            ];
173
        }
174
175
        return $options;
176
    }
177
178
    /**
179
     * @param string $type
180
     * @param array  $classes
181
     *
182
     * @return array
183
     */
184
    private function checkFormClass($type, $classes)
185
    {
186
        return array_filter($classes, function ($subclass) use ($type) {
187
            return is_a($type, $subclass, true);
188
        });
189
    }
190
}
191