Completed
Push — 3.x ( 36fa90...980c10 )
by Christian
15s
created

FormContractor   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 141
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B fixFieldDescription() 0 27 6
A getFormFactory() 0 4 1
A getFormBuilder() 0 4 1
C getDefaultOptions() 0 47 11
A checkFormType() 0 4 1
A checkFormClass() 0 6 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\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 Symfony\Component\Form\FormFactoryInterface;
19
20
class FormContractor implements FormContractorInterface
21
{
22
    protected $fieldFactory;
23
24
    /**
25
     * @var FormFactoryInterface
26
     */
27
    private $formFactory;
28
29
    /**
30
     * @param \Symfony\Component\Form\FormFactoryInterface $formFactory
31
     */
32
    public function __construct(FormFactoryInterface $formFactory)
33
    {
34
        $this->formFactory = $formFactory;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
41
    {
42
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
43
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
44
45
            // set the default field mapping
46
            if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
47
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
48
            }
49
50
            // set the default association mapping
51
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
52
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
53
            }
54
        }
55
56
        if (!$fieldDescription->getType()) {
57
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
58
        }
59
60
        $fieldDescription->setAdmin($admin);
61
        $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
62
63
        if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE, ClassMetadataInfo::MANY))) {
64
            $admin->attachAdminClass($fieldDescription);
65
        }
66
    }
67
68
    /**
69
     * @return \Symfony\Component\Form\FormFactoryInterface
70
     */
71
    public function getFormFactory()
72
    {
73
        return $this->formFactory;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getFormBuilder($name, array $options = array())
80
    {
81
        return $this->getFormFactory()->createNamedBuilder($name, 'form', null, $options);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
88
    {
89
        $options = array();
90
        $options['sonata_field_description'] = $fieldDescription;
91
92
        // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony <2.8
93
        if ($this->checkFormType($type, array(
94
            'sonata_type_model',
95
            'sonata_type_model_list',
96
        )) || $this->checkFormClass($type, array(
97
            'Sonata\AdminBundle\Form\Type\ModelType',
98
            'Sonata\AdminBundle\Form\Type\ModelListType',
99
        ))) {
100
            if ($fieldDescription->getOption('edit') == 'list') {
101
                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');
102
            }
103
104
            $options['class'] = $fieldDescription->getTargetEntity();
105
            $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
106
            // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony <2.8
107
        } elseif ($this->checkFormType($type, array('sonata_type_admin')) || $this->checkFormClass($type, array('Sonata\AdminBundle\Form\Type\AdminType'))) {
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
            $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
113
            $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
114
            // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony <2.8
115
        } elseif ($this->checkFormType($type, array('sonata_type_collection')) || $this->checkFormClass($type, array('Sonata\CoreBundle\Form\Type\CollectionType'))) {
116
            if (!$fieldDescription->getAssociationAdmin()) {
117
                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()));
118
            }
119
120
            // NEXT_MAJOR: Use only FQCN when dropping support for Symfony <2.8
121
            $options['type'] = 'sonata_type_collection' === $type ?
122
                'sonata_type_admin' :
123
                'Sonata\AdminBundle\Form\Type\AdminType'
124
            ;
125
            $options['modifiable'] = true;
126
            $options['type_options'] = array(
127
                'sonata_field_description' => $fieldDescription,
128
                'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
129
            );
130
        }
131
132
        return $options;
133
    }
134
135
    /**
136
     * NEXT_MAJOR: See next major comments above, this method should be removed when dropping support for Symfony <2.8.
137
     *
138
     * @param string $type
139
     * @param array  $types
140
     *
141
     * @return bool
142
     */
143
    private function checkFormType($type, $types)
144
    {
145
        return in_array($type, $types, true);
146
    }
147
148
    /**
149
     * @param string $type
150
     * @param array  $classes
151
     *
152
     * @return array
153
     */
154
    private function checkFormClass($type, $classes)
155
    {
156
        return array_filter($classes, function ($subclass) use ($type) {
157
            return is_a($type, $subclass, true);
158
        });
159
    }
160
}
161