Completed
Pull Request — master (#596)
by
unknown
06:21
created

ShowBuilder::fixFieldDescription()   D

Complexity

Conditions 10
Paths 55

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 42
rs 4.8196
cc 10
eloc 22
nc 55
nop 2

How to fix   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\DoctrineORMAdminBundle\Builder;
13
14
use Sonata\AdminBundle\Admin\AdminInterface;
15
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
16
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
17
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
18
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
19
20
class ShowBuilder implements ShowBuilderInterface
21
{
22
    /**
23
     * @var TypeGuesserInterface
24
     */
25
    protected $guesser;
26
27
    /**
28
     * @var string[]
29
     */
30
    protected $templates;
31
32
    /**
33
     * @param TypeGuesserInterface $guesser
34
     * @param string[]             $templates
35
     */
36
    public function __construct(TypeGuesserInterface $guesser, array $templates)
37
    {
38
        $this->guesser = $guesser;
39
        $this->templates = $templates;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getBaseList(array $options = array())
46
    {
47
        return new FieldDescriptionCollection();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
54
    {
55
        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...
56
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
57
            $fieldDescription->setType($guessType->getType());
58
        } else {
59
            $fieldDescription->setType($type);
60
        }
61
62
        $this->fixFieldDescription($admin, $fieldDescription);
63
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
64
65
        $list->add($fieldDescription);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
72
    {
73
        $fieldDescription->setAdmin($admin);
74
75
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
76
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
77
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
78
79
            // set the default field mapping
80
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
81
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
82
            }
83
84
            // set the default association mapping
85
            if (isset($metadata->associationMappings[$lastPropertyName])) {
86
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
87
            }
88
        }
89
90
        if (!$fieldDescription->getType()) {
91
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
92
        }
93
94
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
95
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
96
97
        if (!$fieldDescription->getTemplate()) {
98
            $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
99
100
            if (!$fieldDescription->getTemplate()) {
101
                if ($fieldDescription->describesSingleValuedAssociation()) {
102
                    $fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:show_single.html.twig');
103
                } elseif ($fieldDescription->describesCollectionValuedAssociation()) {
104
                    $fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:show_collection.html.twig');
105
                }
106
            }
107
        }
108
109
        if ($fieldDescription->describesAssociation()) {
110
            $admin->attachAdminClass($fieldDescription);
111
        }
112
    }
113
114
    /**
115
     * @param string $type
116
     *
117
     * @return string
118
     */
119
    private function getTemplate($type)
120
    {
121
        if (!isset($this->templates[$type])) {
122
            return;
123
        }
124
125
        return $this->templates[$type];
126
    }
127
}
128