Completed
Push — 2.x ( 359f6e )
by Sullivan
16:25 queued 14:10
created

ShowBuilder::fixFieldDescription()   C

Complexity

Conditions 15
Paths 180

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 59
rs 5.7872
c 1
b 0
f 0
cc 15
eloc 36
nc 180
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 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\DoctrineORMAdminBundle\Builder;
13
14
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
17
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
18
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
19
20
use Doctrine\ORM\Mapping\ClassMetadataInfo;
21
22
class ShowBuilder implements ShowBuilderInterface
23
{
24
    protected $guesser;
25
26
    protected $templates;
27
28
    /**
29
     * @param \Sonata\AdminBundle\Guesser\TypeGuesserInterface $guesser
30
     * @param array                                            $templates
31
     */
32
    public function __construct(TypeGuesserInterface $guesser, array $templates)
33
    {
34
        $this->guesser   = $guesser;
35
        $this->templates = $templates;
36
    }
37
38
    /**
39
     * @param array $options
40
     *
41
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionCollection
42
     */
43
    public function getBaseList(array $options = array())
44
    {
45
        return new FieldDescriptionCollection;
46
    }
47
48
    /**
49
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
50
     * @param string|null                                          $type
51
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface  $fieldDescription
52
     * @param \Sonata\AdminBundle\Admin\AdminInterface             $admin
53
     *
54
     * @return mixed
55
     */
56
    public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
57
    {
58
        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...
59
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
60
            $fieldDescription->setType($guessType->getType());
61
        } else {
62
            $fieldDescription->setType($type);
63
        }
64
65
        $this->fixFieldDescription($admin, $fieldDescription);
66
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
67
68
        $list->add($fieldDescription);
69
    }
70
71
    /**
72
     * @param string $type
73
     *
74
     * @return string
75
     */
76
    private function getTemplate($type)
77
    {
78
        if (!isset($this->templates[$type])) {
79
            return null;
80
        }
81
82
        return $this->templates[$type];
83
    }
84
85
    /**
86
     * The method defines the correct default settings for the provided FieldDescription
87
     *
88
     * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
89
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
90
     *
91
     * @return void
92
     */
93
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
94
    {
95
        $fieldDescription->setAdmin($admin);
96
97
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
98
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
99
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
100
101
            // set the default field mapping
102
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
103
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
104
            }
105
106
            // set the default association mapping
107
            if (isset($metadata->associationMappings[$lastPropertyName])) {
108
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
109
            }
110
        }
111
112
        if (!$fieldDescription->getType()) {
113
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
114
        }
115
116
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
117
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
118
119
        if (!$fieldDescription->getTemplate()) {
120
121
            $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
122
123
            if (!$fieldDescription->getTemplate()) {
124
125
                switch($fieldDescription->getMappingType()) {
126
                    case ClassMetadataInfo::MANY_TO_ONE:
127
                        $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_one.html.twig');
128
                        break;
129
                    case ClassMetadataInfo::ONE_TO_ONE:
130
                        $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_one.html.twig');
131
                        break;
132
                    case ClassMetadataInfo::ONE_TO_MANY:
133
                        $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_many.html.twig');
134
                        break;
135
                    case ClassMetadataInfo::MANY_TO_MANY:
136
                        $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_many.html.twig');
137
                        break;
138
                }
139
140
            }
141
        }
142
143
        switch($fieldDescription->getMappingType()) {
144
            case ClassMetadataInfo::MANY_TO_ONE:
145
            case ClassMetadataInfo::ONE_TO_ONE:
146
            case ClassMetadataInfo::ONE_TO_MANY:
147
            case ClassMetadataInfo::MANY_TO_MANY:
148
                $admin->attachAdminClass($fieldDescription);
149
                break;
150
        }
151
    }
152
}
153