ShowBuilder   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 110
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getBaseList() 0 4 1
A addField() 0 14 2
C fixFieldDescription() 0 52 12
A getTemplate() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Builder;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
19
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
20
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
23
class ShowBuilder implements ShowBuilderInterface
24
{
25
    protected $guesser;
26
27
    protected $templates;
28
29
    public function __construct(TypeGuesserInterface $guesser, array $templates)
30
    {
31
        $this->guesser = $guesser;
32
        $this->templates = $templates;
33
    }
34
35
    /**
36
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionCollection
37
     */
38
    public function getBaseList(array $options = [])
39
    {
40
        return new FieldDescriptionCollection();
41
    }
42
43
    /**
44
     * @param string|null $type
45
     *
46
     * @return mixed
47
     */
48
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
49
    {
50
        if (null === $type) {
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
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
59
60
        $list->add($fieldDescription);
61
    }
62
63
    /**
64
     * The method defines the correct default settings for the provided FieldDescription.
65
     */
66
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void
67
    {
68
        $fieldDescription->setAdmin($admin);
69
70
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
71
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
72
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
73
74
            // set the default field mapping
75
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
76
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
77
            }
78
79
            // set the default association mapping
80
            if (isset($metadata->associationMappings[$lastPropertyName])) {
81
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
82
            }
83
        }
84
85
        if (!$fieldDescription->getType()) {
86
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin)));
87
        }
88
89
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
90
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
91
92
        if (!$fieldDescription->getTemplate()) {
93
            if ('id' === $fieldDescription->getType()) {
94
                $fieldDescription->setType('string');
95
            }
96
97
            if ('int' === $fieldDescription->getType()) {
98
                $fieldDescription->setType('integer');
99
            }
100
101
            $template = $this->getTemplate($fieldDescription->getType());
102
103
            if (null === $template) {
104
                if (ClassMetadata::ONE === $fieldDescription->getMappingType()) {
105
                    $template = '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig';
106
                } elseif (ClassMetadata::MANY === $fieldDescription->getMappingType()) {
107
                    $template = '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig';
108
                }
109
            }
110
111
            $fieldDescription->setTemplate($template);
112
        }
113
114
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
115
            $admin->attachAdminClass($fieldDescription);
116
        }
117
    }
118
119
    /**
120
     * @param string $type
121
     *
122
     * @return string
123
     */
124
    private function getTemplate($type)
125
    {
126
        if (!isset($this->templates[$type])) {
127
            return;
128
        }
129
130
        return $this->templates[$type];
131
    }
132
}
133