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

ListBuilder::fixFieldDescription()   D

Complexity

Conditions 14
Paths 490

Size

Total Lines 63
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 63
rs 4.0413
cc 14
eloc 34
nc 490
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\FieldDescriptionCollection;
17
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
18
use Sonata\AdminBundle\Builder\ListBuilderInterface;
19
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
20
21
class ListBuilder implements ListBuilderInterface
22
{
23
    protected $guesser;
24
25
    protected $templates = array();
26
27
    /**
28
     * @param \Sonata\AdminBundle\Guesser\TypeGuesserInterface $guesser
29
     * @param array                                            $templates
30
     */
31
    public function __construct(TypeGuesserInterface $guesser, array $templates = array())
32
    {
33
        $this->guesser = $guesser;
34
        $this->templates = $templates;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getBaseList(array $options = array())
41
    {
42
        return new FieldDescriptionCollection();
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function buildField($type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
49
    {
50
        if ($type == null) {
51
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
52
            $fieldDescription->setType($guessType->getType());
53
        } else {
54
            $fieldDescription->setType($type);
55
        }
56
57
        $this->fixFieldDescription($admin, $fieldDescription);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
64
    {
65
        $this->buildField($type, $fieldDescription, $admin);
66
        $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription);
67
68
        $list->add($fieldDescription);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
75
    {
76
        if ($fieldDescription->getName() == '_action') {
77
            $this->buildActionFieldDescription($fieldDescription);
78
        }
79
80
        $fieldDescription->setAdmin($admin);
81
82
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
83
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
84
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
85
86
            // set the default field mapping
87
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
88
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
89
                if ($fieldDescription->getOption('sortable') !== false) {
90
                    $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', true));
91
                    $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings()));
92
                    $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping()));
93
                }
94
            }
95
96
            // set the default association mapping
97
            if (isset($metadata->associationMappings[$lastPropertyName])) {
98
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
99
            }
100
101
            $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC'));
102
        }
103
104
        if (!$fieldDescription->getType()) {
105
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
106
        }
107
108
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
109
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
110
111
        if (!$fieldDescription->getTemplate()) {
112
            if ($fieldDescription->getType() == 'id') {
113
                $fieldDescription->setType('string');
114
            }
115
116
            if ($fieldDescription->getType() == 'int') {
117
                $fieldDescription->setType('integer');
118
            }
119
120
            $template = $this->getTemplate($fieldDescription->getType());
121
122
            if ($template === null) {
123
                if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE) {
124
                    $template = 'SonataDoctrineMongoDBAdminBundle:CRUD:list_mongo_one.html.twig';
125
                } elseif ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY) {
126
                    $template = 'SonataDoctrineMongoDBAdminBundle:CRUD:list_mongo_many.html.twig';
127
                }
128
            }
129
130
            $fieldDescription->setTemplate($template);
131
        }
132
133
        if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE, ClassMetadataInfo::MANY))) {
134
            $admin->attachAdminClass($fieldDescription);
135
        }
136
    }
137
138
    /**
139
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
140
     *
141
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
142
     */
143
    public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription)
144
    {
145
        if (null === $fieldDescription->getTemplate()) {
146
            $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig');
147
        }
148
149
        if (null === $fieldDescription->getType()) {
150
            $fieldDescription->setType('action');
151
        }
152
153
        if (null === $fieldDescription->getOption('name')) {
154
            $fieldDescription->setOption('name', 'Action');
155
        }
156
157
        if (null === $fieldDescription->getOption('code')) {
158
            $fieldDescription->setOption('code', 'Action');
159
        }
160
161
        if (null !== $fieldDescription->getOption('actions')) {
162
            $actions = $fieldDescription->getOption('actions');
163
            foreach ($actions as $k => $action) {
164
                if (!isset($action['template'])) {
165
                    $actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k);
166
                }
167
            }
168
169
            $fieldDescription->setOption('actions', $actions);
170
        }
171
172
        return $fieldDescription;
173
    }
174
175
    /**
176
     * @param string $type
177
     *
178
     * @return string
179
     */
180
    private function getTemplate($type)
181
    {
182
        if (!isset($this->templates[$type])) {
183
            return;
184
        }
185
186
        return $this->templates[$type];
187
    }
188
}
189