Completed
Push — 3.x-dev-kit ( 4fe8f1 )
by
unknown
11:30 queued 08:33
created

ShowBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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 Doctrine\ORM\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
    /**
24
     * @var TypeGuesserInterface
25
     */
26
    protected $guesser;
27
28
    /**
29
     * @var string[]
30
     */
31
    protected $templates;
32
33
    /**
34
     * @param TypeGuesserInterface $guesser
35
     * @param string[]             $templates
36
     */
37
    public function __construct(TypeGuesserInterface $guesser, array $templates)
38
    {
39
        $this->guesser = $guesser;
40
        $this->templates = $templates;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getBaseList(array $options = array())
47
    {
48
        return new FieldDescriptionCollection();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
55
    {
56
        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...
57
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
58
            $fieldDescription->setType($guessType->getType());
59
        } else {
60
            $fieldDescription->setType($type);
61
        }
62
63
        $this->fixFieldDescription($admin, $fieldDescription);
64
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
65
66
        $list->add($fieldDescription);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
73
    {
74
        $fieldDescription->setAdmin($admin);
75
76
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
77
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
78
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
79
80
            // set the default field mapping
81
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
82
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
83
            }
84
85
            // set the default association mapping
86
            if (isset($metadata->associationMappings[$lastPropertyName])) {
87
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
88
            }
89
        }
90
91
        if (!$fieldDescription->getType()) {
92
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
93
        }
94
95
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
96
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
97
98
        if (!$fieldDescription->getTemplate()) {
99
            $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
100
101
            if (!$fieldDescription->getTemplate()) {
102
                switch ($fieldDescription->getMappingType()) {
103
                    case ClassMetadataInfo::MANY_TO_ONE:
104
                        $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_one.html.twig');
105
                        break;
106
                    case ClassMetadataInfo::ONE_TO_ONE:
107
                        $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_one.html.twig');
108
                        break;
109
                    case ClassMetadataInfo::ONE_TO_MANY:
110
                        $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_many.html.twig');
111
                        break;
112
                    case ClassMetadataInfo::MANY_TO_MANY:
113
                        $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_many.html.twig');
114
                        break;
115
                }
116
            }
117
        }
118
119
        switch ($fieldDescription->getMappingType()) {
120
            case ClassMetadataInfo::MANY_TO_ONE:
121
            case ClassMetadataInfo::ONE_TO_ONE:
122
            case ClassMetadataInfo::ONE_TO_MANY:
123
            case ClassMetadataInfo::MANY_TO_MANY:
124
                $admin->attachAdminClass($fieldDescription);
125
                break;
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