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