Completed
Push — 3.x ( 839353...0c5d0b )
by Grégoire
02:14
created

FormContractor::fixFieldDescription()   B

Complexity

Conditions 6
Paths 15

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 27
rs 8.439
cc 6
eloc 13
nc 15
nop 2
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
     * @param \Symfony\Component\Form\FormFactoryInterface $formFactory
26
     */
27
    public function __construct(FormFactoryInterface $formFactory)
28
    {
29
        $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...
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
36
    {
37
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
38
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
39
40
            // set the default field mapping
41
            if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
42
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
43
            }
44
45
            // set the default association mapping
46
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
47
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
48
            }
49
        }
50
51
        if (!$fieldDescription->getType()) {
52
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
53
        }
54
55
        $fieldDescription->setAdmin($admin);
56
        $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
57
58
        if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE, ClassMetadataInfo::MANY))) {
59
            $admin->attachAdminClass($fieldDescription);
60
        }
61
    }
62
63
    /**
64
     * @return \Symfony\Component\Form\FormFactoryInterface
65
     */
66
    public function getFormFactory()
67
    {
68
        return $this->formFactory;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getFormBuilder($name, array $options = array())
75
    {
76
        return $this->getFormFactory()->createNamedBuilder($name, 'form', null, $options);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
83
    {
84
        $options = array();
85
        $options['sonata_field_description'] = $fieldDescription;
86
87
        if ($type == 'sonata_type_model' || $type == 'sonata_type_model_list') {
88
            if ($fieldDescription->getOption('edit') == 'list') {
89
                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');
90
            }
91
92
            $options['class'] = $fieldDescription->getTargetEntity();
93
            $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
94
        } elseif ($type == 'sonata_type_admin') {
95
            if (!$fieldDescription->getAssociationAdmin()) {
96
                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()));
97
            }
98
99
            $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
100
            $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
101
        } elseif ($type == 'sonata_type_collection') {
102
            if (!$fieldDescription->getAssociationAdmin()) {
103
                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()));
104
            }
105
106
            $options['type'] = 'sonata_type_admin';
107
            $options['modifiable'] = true;
108
            $options['type_options'] = array(
109
            'sonata_field_description' => $fieldDescription,
110
            'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
111
        );
112
        }
113
114
        return $options;
115
    }
116
}
117