Completed
Push — master ( bae860...fc1986 )
by Grégoire
11s
created

src/Builder/ShowBuilder.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\ClassMetadataInfo;
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
    /**
30
     * @param \Sonata\AdminBundle\Guesser\TypeGuesserInterface $guesser
31
     * @param array                                            $templates
32
     */
33
    public function __construct(TypeGuesserInterface $guesser, array $templates)
34
    {
35
        $this->guesser = $guesser;
36
        $this->templates = $templates;
37
    }
38
39
    /**
40
     * @param array $options
41
     *
42
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionCollection
43
     */
44
    public function getBaseList(array $options = [])
45
    {
46
        return new FieldDescriptionCollection();
47
    }
48
49
    /**
50
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
51
     * @param string|null                                          $type
52
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface  $fieldDescription
53
     * @param \Sonata\AdminBundle\Admin\AdminInterface             $admin
54
     *
55
     * @return mixed
56
     */
57
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
58
    {
59
        if (null == $type) {
0 ignored issues
show
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...
60
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
61
            $fieldDescription->setType($guessType->getType());
62
        } else {
63
            $fieldDescription->setType($type);
64
        }
65
66
        $this->fixFieldDescription($admin, $fieldDescription);
67
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
68
69
        $list->add($fieldDescription);
70
    }
71
72
    /**
73
     * The method defines the correct default settings for the provided FieldDescription.
74
     *
75
     * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
76
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
77
     */
78
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void
79
    {
80
        $fieldDescription->setAdmin($admin);
81
82
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
83
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
84
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
85
86
            // set the default field mapping
87
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
88
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
89
            }
90
91
            // set the default association mapping
92
            if (isset($metadata->associationMappings[$lastPropertyName])) {
93
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
94
            }
95
        }
96
97
        if (!$fieldDescription->getType()) {
98
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin)));
99
        }
100
101
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
102
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
103
104
        if (!$fieldDescription->getTemplate()) {
105
            if ('id' == $fieldDescription->getType()) {
106
                $fieldDescription->setType('string');
107
            }
108
109
            if ('int' == $fieldDescription->getType()) {
110
                $fieldDescription->setType('integer');
111
            }
112
113
            $template = $this->getTemplate($fieldDescription->getType());
114
115
            if (null === $template) {
116
                if (ClassMetadataInfo::ONE == $fieldDescription->getMappingType()) {
117
                    $template = '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig';
118
                } elseif (ClassMetadataInfo::MANY == $fieldDescription->getMappingType()) {
119
                    $template = '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig';
120
                }
121
            }
122
123
            $fieldDescription->setTemplate($template);
124
        }
125
126
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE, ClassMetadataInfo::MANY])) {
127
            $admin->attachAdminClass($fieldDescription);
128
        }
129
    }
130
131
    /**
132
     * @param string $type
133
     *
134
     * @return string
135
     */
136
    private function getTemplate($type)
137
    {
138
        if (!isset($this->templates[$type])) {
139
            return;
140
        }
141
142
        return $this->templates[$type];
143
    }
144
}
145