Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

src/Builder/FormContractor.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\DoctrinePHPCRAdminBundle\Builder;
13
14
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
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
    /**
23
     * @var FormFactoryInterface
24
     */
25
    protected $formFactory;
26
27
    /**
28
     * @param FormFactoryInterface $formFactory
29
     */
30
    public function __construct(FormFactoryInterface $formFactory)
31
    {
32
        $this->formFactory = $formFactory;
33
    }
34
35
    /**
36
     * The method defines the correct default settings for the provided FieldDescription.
37
     *
38
     * {@inheritdoc}
39
     *
40
     * @throws \RuntimeException if the $fieldDescription does not specify a type
41
     */
42
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
43
    {
44
        $metadata = null;
45
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
46
            /** @var \Doctrine\ODM\PHPCR\Mapping\ClassMetadata $metadata */
47
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
48
49
            // set the default field mapping
50
            if (isset($metadata->mappings[$fieldDescription->getName()])) {
51
                $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
52
            }
53
54
            // set the default association mapping
55
            if ($metadata->hasAssociation($fieldDescription->getName())) {
56
                $fieldDescription->setAssociationMapping($metadata->getAssociation($fieldDescription->getName()));
57
            }
58
        }
59
60
        if (!$fieldDescription->getType()) {
61
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
62
        }
63
64
        $fieldDescription->setAdmin($admin);
65
        $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
66
67
        $mappingTypes = array(
68
            ClassMetadata::MANY_TO_ONE,
69
            ClassMetadata::MANY_TO_MANY,
70
            'children',
71
            'child', 'parent',
72
            'referrers',
73
        );
74
75
        if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && in_array($fieldDescription->getMappingType(), $mappingTypes)) {
76
            $admin->attachAdminClass($fieldDescription);
77
        }
78
    }
79
80
    /**
81
     * @return FormFactoryInterface
82
     */
83
    public function getFormFactory()
84
    {
85
        return $this->formFactory;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getFormBuilder($name, array $options = array())
92
    {
93
        return $this->getFormFactory()->createNamedBuilder($name, 'form', null, $options);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     *
99
     * @throws \LogicException if a sonata_type_model field does not have a
100
     *                         target model configured
101
     */
102
    public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
103
    {
104
        $options = array();
105
        $options['sonata_field_description'] = $fieldDescription;
106
107
        switch ($type) {
108
            case 'Sonata\DoctrinePHPCRAdminBundle\Form\Type\TreeModelType':
109
            case 'doctrine_phpcr_odm_tree':
110
                $options['class'] = $fieldDescription->getTargetEntity();
111
                $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
112
113
                break;
114
            case 'Sonata\AdminBundle\Form\Type\Modeltype':
115
            case 'sonata_type_model':
116
            case 'Sonata\AdminBundle\Form\Type\ModelTypeList':
117
            case 'sonata_type_model_list':
118
                if (!$fieldDescription->getTargetEntity()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldDescription->getTargetEntity() of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
119
                    throw new \LogicException(sprintf(
120
                        'The field "%s" in class "%s" does not have a target model defined. Please specify the "targetDocument" attribute in the mapping for this class.',
121
                        $fieldDescription->getName(),
122
                        $fieldDescription->getAdmin()->getClass()
123
                    ));
124
                }
125
126
                $options['class'] = $fieldDescription->getTargetEntity();
127
                $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
128
129
                break;
130
            case 'Sonata\AdminBundle\Form\Type\AdminType':
131
            case 'sonata_type_admin':
132
                if (!$fieldDescription->getAssociationAdmin()) {
133
                    throw $this->getAssociationAdminException($fieldDescription);
134
                }
135
136
                $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
137
                $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
138
139
                break;
140
            case 'Sonata\CoreBundle\Form\Type\CollectionType':
141
            case 'sonata_type_collection':
142
                if (!$fieldDescription->getAssociationAdmin()) {
143
                    throw $this->getAssociationAdminException($fieldDescription);
144
                }
145
146
                $options['type'] = 'sonata_type_admin';
147
                $options['modifiable'] = true;
148
                $options['type_options'] = array(
149
                    'sonata_field_description' => $fieldDescription,
150
                    'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
151
                );
152
            break;
153
        }
154
155
        return $options;
156
    }
157
158
    /**
159
     * @param FieldDescriptionInterface $fieldDescription
160
     *
161
     * @return \LogicException
162
     */
163
    protected function getAssociationAdminException(FieldDescriptionInterface $fieldDescription)
164
    {
165
        $msg = sprintf('The current field `%s` is not linked to an admin. Please create one', $fieldDescription->getName());
166
        if (in_array($fieldDescription->getMappingType(), array(ClassMetadata::MANY_TO_ONE, ClassMetadata::MANY_TO_MANY, 'referrers'))) {
167
            if ($fieldDescription->getTargetEntity()) {
168
                $msg .= " for the target document: `{$fieldDescription->getTargetEntity()}`";
169
            }
170
            $msg .= ', specify the `targetDocument` in the Reference, or the `referringDocument` in the Referrers or use the option `admin_code` to link it.';
171
        } else {
172
            $msg .= ' and use the option `admin_code` to link it.';
173
        }
174
175
        return new \LogicException($msg);
176
    }
177
}
178