ShowBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\DoctrinePHPCRAdminBundle\Builder;
15
16
use Doctrine\ODM\PHPCR\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
    /**
26
     * @var TypeGuesserInterface
27
     */
28
    protected $guesser;
29
30
    /**
31
     * @var array
32
     */
33
    protected $templates;
34
35
    /**
36
     * @param array $templates Indexed by field type
37
     */
38
    public function __construct(TypeGuesserInterface $guesser, array $templates)
39
    {
40
        $this->guesser = $guesser;
41
        $this->templates = $templates;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getBaseList(array $options = [])
48
    {
49
        return new FieldDescriptionCollection();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin): void
56
    {
57
        if (null === $type) {
58
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
59
            $fieldDescription->setType($guessType->getType());
60
        } else {
61
            $fieldDescription->setType($type);
62
        }
63
64
        $this->fixFieldDescription($admin, $fieldDescription);
65
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
66
67
        $list->add($fieldDescription);
68
    }
69
70
    /**
71
     * The method defines the correct default settings for the provided FieldDescription.
72
     *
73
     * {@inheritdoc}
74
     *
75
     * @throws \RuntimeException if the $fieldDescription does not have a type
76
     */
77
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void
78
    {
79
        $fieldDescription->setAdmin($admin);
80
81
        $metadata = null;
82
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
83
            /** @var ClassMetadata $metadata */
84
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
85
86
            // set the default field mapping
87
            if (isset($metadata->mappings[$fieldDescription->getName()])) {
88
                $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
89
            }
90
91
            // set the default association mapping
92
            if ($metadata->hasAssociation($fieldDescription->getName())) {
93
                $fieldDescription->setAssociationMapping($metadata->getAssociation($fieldDescription->getName()));
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()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldDescription->getTemplate() of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
105
            $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
106
107
            if (ClassMetadata::MANY_TO_ONE === $fieldDescription->getMappingType()) {
108
                $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/show_many_to_one.html.twig');
109
            }
110
111
            if (ClassMetadata::MANY_TO_MANY === $fieldDescription->getMappingType()) {
112
                $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/show_many_to_many.html.twig');
113
            }
114
        }
115
116
        $mappingTypes = [
117
            ClassMetadata::MANY_TO_ONE,
118
            ClassMetadata::MANY_TO_MANY,
119
            'children',
120
            'child',
121
            'parent',
122
            'referrers',
123
        ];
124
125
        if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && \in_array($fieldDescription->getMappingType(), $mappingTypes, true)) {
126
            $admin->attachAdminClass($fieldDescription);
127
        }
128
    }
129
130
    /**
131
     * @param string $type
132
     *
133
     * @return string|null The template if found
134
     */
135
    private function getTemplate($type)
136
    {
137
        if (!isset($this->templates[$type])) {
138
            return;
139
        }
140
141
        return $this->templates[$type];
142
    }
143
}
144