Completed
Pull Request — 2.x (#544)
by Oskar
03:30 queued 01:43
created

src/Builder/ShowBuilder.php (1 issue)

Labels
Severity

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\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 TypeGuesserInterface $guesser
37
     * @param array                $templates Indexed by field type
38
     */
39
    public function __construct(TypeGuesserInterface $guesser, array $templates)
40
    {
41
        $this->guesser = $guesser;
42
        $this->templates = $templates;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getBaseList(array $options = [])
49
    {
50
        return new FieldDescriptionCollection();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
57
    {
58
        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...
59
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
60
            $fieldDescription->setType($guessType->getType());
61
        } else {
62
            $fieldDescription->setType($type);
63
        }
64
65
        $this->fixFieldDescription($admin, $fieldDescription);
66
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
67
68
        $list->add($fieldDescription);
69
    }
70
71
    /**
72
     * The method defines the correct default settings for the provided FieldDescription.
73
     *
74
     * {@inheritdoc}
75
     *
76
     * @throws \RuntimeException if the $fieldDescription does not have a type
77
     */
78
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
79
    {
80
        $fieldDescription->setAdmin($admin);
81
82
        $metadata = null;
83
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
84
            /** @var ClassMetadata $metadata */
85
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
86
87
            // set the default field mapping
88
            if (isset($metadata->mappings[$fieldDescription->getName()])) {
89
                $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
90
            }
91
92
            // set the default association mapping
93
            if ($metadata->hasAssociation($fieldDescription->getName())) {
94
                $fieldDescription->setAssociationMapping($metadata->getAssociation($fieldDescription->getName()));
95
            }
96
        }
97
98
        if (!$fieldDescription->getType()) {
99
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin)));
100
        }
101
102
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
103
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
104
105
        if (!$fieldDescription->getTemplate()) {
106
            $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
107
108
            if (ClassMetadata::MANY_TO_ONE == $fieldDescription->getMappingType()) {
109
                $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/show_many_to_one.html.twig');
110
            }
111
112
            if (ClassMetadata::MANY_TO_MANY == $fieldDescription->getMappingType()) {
113
                $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/show_many_to_many.html.twig');
114
            }
115
        }
116
117
        $mappingTypes = [
118
            ClassMetadata::MANY_TO_ONE,
119
            ClassMetadata::MANY_TO_MANY,
120
            'children',
121
            'child',
122
            'parent',
123
            'referrers',
124
        ];
125
126
        if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && \in_array($fieldDescription->getMappingType(), $mappingTypes, true)) {
127
            $admin->attachAdminClass($fieldDescription);
128
        }
129
    }
130
131
    /**
132
     * @param string $type
133
     *
134
     * @return string|null The template if found
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