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

ShowBuilder::fixFieldDescription()   C

Complexity

Conditions 12
Paths 175

Size

Total Lines 52
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 52
rs 5.4871
cc 12
eloc 27
nc 175
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\ShowBuilderInterface;
19
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
20
21
class ShowBuilder implements ShowBuilderInterface
22
{
23
    protected $guesser;
24
25
    protected $templates;
26
27
    /**
28
     * @param \Sonata\AdminBundle\Guesser\TypeGuesserInterface $guesser
29
     * @param array                                            $templates
30
     */
31
    public function __construct(TypeGuesserInterface $guesser, array $templates)
32
    {
33
        $this->guesser = $guesser;
34
        $this->templates = $templates;
35
    }
36
37
    /**
38
     * @param array $options
39
     *
40
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionCollection
41
     */
42
    public function getBaseList(array $options = array())
43
    {
44
        return new FieldDescriptionCollection();
45
    }
46
47
    /**
48
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
49
     * @param string|null                                          $type
50
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface  $fieldDescription
51
     * @param \Sonata\AdminBundle\Admin\AdminInterface             $admin
52
     *
53
     * @return mixed
54
     */
55
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
56
    {
57
        if ($type == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $type of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
58
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
59
            $fieldDescription->setType($guessType->getType());
60
        } else {
61
            $fieldDescription->setType($type);
62
        }
63
64
        $this->fixFieldDescription($admin, $fieldDescription);
65
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
66
67
        $list->add($fieldDescription);
68
    }
69
70
    /**
71
     * The method defines the correct default settings for the provided FieldDescription.
72
     *
73
     * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
74
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
75
     */
76
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
77
    {
78
        $fieldDescription->setAdmin($admin);
79
80
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
81
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
82
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
83
84
            // set the default field mapping
85
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
86
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
87
            }
88
89
                // set the default association mapping
90
            if (isset($metadata->associationMappings[$lastPropertyName])) {
91
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
92
            }
93
        }
94
95
        if (!$fieldDescription->getType()) {
96
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
97
        }
98
99
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
100
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
101
102
        if (!$fieldDescription->getTemplate()) {
103
            if ($fieldDescription->getType() == 'id') {
104
                $fieldDescription->setType('string');
105
            }
106
107
            if ($fieldDescription->getType() == 'int') {
108
                $fieldDescription->setType('integer');
109
            }
110
111
            $template = $this->getTemplate($fieldDescription->getType());
112
113
            if ($template === null) {
114
                if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE) {
115
                    $template = 'SonataDoctrineMongoDBAdminBundle:CRUD:show_mongo_one.html.twig';
116
                } elseif ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY) {
117
                    $template = 'SonataDoctrineMongoDBAdminBundle:CRUD:show_mongo_many.html.twig';
118
                }
119
            }
120
121
            $fieldDescription->setTemplate($template);
122
        }
123
124
        if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE, ClassMetadataInfo::MANY))) {
125
            $admin->attachAdminClass($fieldDescription);
126
        }
127
    }
128
129
    /**
130
     * @param string $type
131
     *
132
     * @return string
133
     */
134
    private function getTemplate($type)
135
    {
136
        if (!isset($this->templates[$type])) {
137
            return;
138
        }
139
140
        return $this->templates[$type];
141
    }
142
}
143