Completed
Push — master ( a33308...36ba79 )
by Maximilian
01:58
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
/*
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\DoctrinePHPCRAdminBundle\Builder;
13
14
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
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 array
30
     */
31
    protected $templates;
32
33
    /**
34
     * @param TypeGuesserInterface $guesser
35
     * @param array                $templates Indexed by field type
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
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
     * The method defines the correct default settings for the provided FieldDescription.
71
     *
72
     * {@inheritdoc}
73
     *
74
     * @throws \RuntimeException if the $fieldDescription does not have a type
75
     */
76
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
77
    {
78
        $fieldDescription->setAdmin($admin);
79
80
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
81
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
82
83
            // set the default field mapping
84
            if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
85
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
86
            }
87
88
            // set the default association mapping
89
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
90
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
91
            }
92
        }
93
94
        if (!$fieldDescription->getType()) {
95
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
96
        }
97
98
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
99
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
100
101
        if (!$fieldDescription->getTemplate()) {
102
            $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
103
104
            if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_ONE) {
105
                $fieldDescription->setTemplate('SonataDoctrinePhpcrAdminBundle:CRUD:show_phpcr_many_to_one.html.twig');
106
            }
107
108
            if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_MANY) {
109
                $fieldDescription->setTemplate('SonataDoctrinePhpcrAdminBundle:CRUD:show_phpcr_many_to_many.html.twig');
110
            }
111
        }
112
113
        switch ($fieldDescription->getMappingType()) {
114
            case ClassMetadata::MANY_TO_ONE:
115
            case ClassMetadata::MANY_TO_MANY:
116
                $admin->attachAdminClass($fieldDescription);
117
                break;
118
        }
119
    }
120
121
    /**
122
     * @param string $type
123
     *
124
     * @return string|null The template if found
125
     */
126
    private function getTemplate($type)
127
    {
128
        if (!isset($this->templates[$type])) {
129
            return;
130
        }
131
132
        return $this->templates[$type];
133
    }
134
}
135