Completed
Push — 1.2 ( d3ea0d...d8c512 )
by David
16:21
created

ListBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata 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 Sonata\AdminBundle\Admin\FieldDescriptionInterface;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
17
use Sonata\AdminBundle\Builder\ListBuilderInterface;
18
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
19
20
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
21
22
class ListBuilder implements ListBuilderInterface
23
{
24
    /**
25
     * @var TypeGuesserInterface
26
     */
27
    protected $guesser;
28
29
    /**
30
     * @var array
31
     */
32
    protected $templates;
33
34
    /**
35
     * @param TypeGuesserInterface $guesser
36
     * @param array $templates
37
     */
38
    public function __construct(TypeGuesserInterface $guesser, array $templates = array())
39
    {
40
        $this->guesser = $guesser;
41
        $this->templates = $templates;
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function getBaseList(array $options = array())
48
    {
49
        return new FieldDescriptionCollection();
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function buildField($type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
56
    {
57
        if ($type == null) {
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
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
71
    {
72
        $this->buildField($type, $fieldDescription, $admin);
73
        $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription);
74
75
        $list->add($fieldDescription);
76
    }
77
78
    /**
79
     * @param string $type
80
     *
81
     * @return string
82
     */
83
    private function getTemplate($type)
84
    {
85
        if (!isset($this->templates[$type])) {
86
            return null;
87
        }
88
89
        return $this->templates[$type];
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     *
95
     * @throws \RuntimeException if the $fieldDescription does not have a type.
96
     */
97
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
98
    {
99
        if ($fieldDescription->getName() == '_action') {
100
            $this->buildActionFieldDescription($fieldDescription);
101
        }
102
103
        $fieldDescription->setAdmin($admin);
104
105
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
106
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
107
108
            // TODO sort on parent associations or node name
109
            $defaultSortable = true;
110
            if ($metadata->hasAssociation($fieldDescription->getName())
111
                || $metadata->nodename === $fieldDescription->getName()
112
            ) {
113
                $defaultSortable = false;
114
            }
115
116
            // TODO get and set parent association mappings, see
117
            // https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle/issues/106
118
            //$fieldDescription->setParentAssociationMappings($parentAssociationMappings);
119
120
            // set the default field mapping
121
            if (isset($metadata->mappings[$fieldDescription->getName()])) {
122
                $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
123
                if ($fieldDescription->getOption('sortable') !== false) {
124
                    $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', $defaultSortable));
125
                    $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings()));
126
                    $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping()));
127
                }
128
            }
129
130
            // set the default association mapping
131
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
132
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
133
            }
134
135
            $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC'));
136
        }
137
138
        if (!$fieldDescription->getType()) {
139
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
140
        }
141
142
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
143
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
144
145
        if (!$fieldDescription->getTemplate()) {
146
147
            $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
148
149
            if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_ONE) {
150
                $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_many_to_one.html.twig');
151
            }
152
153
            if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_MANY) {
154
                $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_many_to_many.html.twig');
155
            }
156
157
            if ($fieldDescription->getMappingType() == 'child' || $fieldDescription->getMappingType() == 'parent') {
158
                $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_one_to_one.html.twig');
159
            }
160
161
            if ($fieldDescription->getMappingType() == 'children' || $fieldDescription->getMappingType() == 'referrers') {
162
                $fieldDescription->setTemplate('SonataDoctrinePHPCRAdminBundle:CRUD:list_phpcr_one_to_many.html.twig');
163
            }
164
        }
165
166
        $mappingTypes = array(
167
            ClassMetadata::MANY_TO_ONE,
168
            ClassMetadata::MANY_TO_MANY,
169
            'children',
170
            'child', 'parent',
171
            'referrers',
172
        );
173
174
        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...
175
            $admin->attachAdminClass($fieldDescription);
176
        }
177
    }
178
179
    /**
180
     * @param FieldDescriptionInterface $fieldDescription
181
     *
182
     * @return FieldDescriptionInterface
183
     */
184
    public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription)
185
    {
186
        if (null === $fieldDescription->getTemplate()) {
187
            $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig');
188
        }
189
190
        if (null === $fieldDescription->getType()) {
191
            $fieldDescription->setType('action');
192
        }
193
194
        if (null === $fieldDescription->getOption('name')) {
195
            $fieldDescription->setOption('name', 'Action');
196
        }
197
198
        if (null === $fieldDescription->getOption('code')) {
199
            $fieldDescription->setOption('code', 'Action');
200
        }
201
202
        if (null !== $fieldDescription->getOption('actions')) {
203
            $actions = $fieldDescription->getOption('actions');
204
            foreach ($actions as $k => $action) {
205
                if (!isset($action['template'])) {
206
                    $actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k);
207
                }
208
            }
209
210
            $fieldDescription->setOption('actions', $actions);
211
        }
212
213
        return $fieldDescription;
214
    }
215
}
216