Completed
Push — master ( a33308...36ba79 )
by Maximilian
01:58
created

ListBuilder::getTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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\ListBuilderInterface;
19
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
20
21
class ListBuilder implements ListBuilderInterface
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
36
     */
37
    public function __construct(TypeGuesserInterface $guesser, array $templates = array())
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 buildField($type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
55
    {
56
        if ($type == null) {
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
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
70
    {
71
        $this->buildField($type, $fieldDescription, $admin);
72
        $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription);
73
74
        $list->add($fieldDescription);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     *
80
     * @throws \RuntimeException if the $fieldDescription does not have a type
81
     */
82
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
83
    {
84
        if ($fieldDescription->getName() == '_action' || $fieldDescription->getType() === 'actions') {
85
            $this->buildActionFieldDescription($fieldDescription);
86
        }
87
88
        $fieldDescription->setAdmin($admin);
89
90
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
91
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
92
93
            // TODO sort on parent associations or node name
94
            $defaultSortable = true;
95
            if ($metadata->hasAssociation($fieldDescription->getName())
96
                || $metadata->nodename === $fieldDescription->getName()
97
            ) {
98
                $defaultSortable = false;
99
            }
100
101
            // TODO get and set parent association mappings, see
102
            // https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle/issues/106
103
            //$fieldDescription->setParentAssociationMappings($parentAssociationMappings);
104
105
            // set the default field mapping
106
            if (isset($metadata->mappings[$fieldDescription->getName()])) {
107
                $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
108
                if ($fieldDescription->getOption('sortable') !== false) {
109
                    $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', $defaultSortable));
110
                    $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings()));
111
                    $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping()));
112
                }
113
            }
114
115
            // set the default association mapping
116
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
117
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
118
            }
119
120
            $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC'));
121
        }
122
123
        if (!$fieldDescription->getType()) {
124
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
125
        }
126
127
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
128
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
129
130
        if (!$fieldDescription->getTemplate()) {
131
            $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
132
133
            if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_ONE) {
134
                $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_many_to_one.html.twig');
135
            }
136
137
            if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_MANY) {
138
                $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_many_to_many.html.twig');
139
            }
140
141
            if ($fieldDescription->getMappingType() == 'child' || $fieldDescription->getMappingType() == 'parent') {
142
                $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_one_to_one.html.twig');
143
            }
144
145
            if ($fieldDescription->getMappingType() == 'children' || $fieldDescription->getMappingType() == 'referrers') {
146
                $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_one_to_many.html.twig');
147
            }
148
        }
149
150
        $mappingTypes = array(
151
            ClassMetadata::MANY_TO_ONE,
152
            ClassMetadata::MANY_TO_MANY,
153
            'children',
154
            'child', 'parent',
155
            'referrers',
156
        );
157
158
        if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && in_array($fieldDescription->getMappingType(), $mappingTypes)) {
0 ignored issues
show
Bug introduced by
The variable $metadata does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
159
            $admin->attachAdminClass($fieldDescription);
160
        }
161
    }
162
163
    /**
164
     * @param FieldDescriptionInterface $fieldDescription
165
     *
166
     * @return FieldDescriptionInterface
167
     */
168
    public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription)
169
    {
170
        if (null === $fieldDescription->getTemplate()) {
171
            $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig');
172
        }
173
174
        if (null === $fieldDescription->getType()) {
175
            $fieldDescription->setType('actions');
176
        }
177
178
        if (null === $fieldDescription->getOption('name')) {
179
            $fieldDescription->setOption('name', 'Action');
180
        }
181
182
        if (null === $fieldDescription->getOption('code')) {
183
            $fieldDescription->setOption('code', 'Action');
184
        }
185
186
        if (null !== $fieldDescription->getOption('actions')) {
187
            $actions = $fieldDescription->getOption('actions');
188
            foreach ($actions as $k => $action) {
189
                if (!isset($action['template'])) {
190
                    $actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k);
191
                }
192
            }
193
194
            $fieldDescription->setOption('actions', $actions);
195
        }
196
197
        return $fieldDescription;
198
    }
199
200
    /**
201
     * @param string $type
202
     *
203
     * @return string
204
     */
205
    private function getTemplate($type)
206
    {
207
        if (!isset($this->templates[$type])) {
208
            return;
209
        }
210
211
        return $this->templates[$type];
212
    }
213
}
214