Completed
Push — 2.x ( 359f6e )
by Sullivan
14:45
created

DatagridBuilder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 14
c 3
b 1
f 2
lcom 2
cbo 9
dl 0
loc 123
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B fixFieldDescription() 0 28 5
B addFilter() 0 34 6
A getBaseDatagrid() 0 14 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\DoctrineORMAdminBundle\Builder;
13
14
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Datagrid\DatagridInterface;
17
use Sonata\AdminBundle\Datagrid\Datagrid;
18
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
19
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
20
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
21
22
use Sonata\DoctrineORMAdminBundle\Datagrid\Pager;
23
use Symfony\Component\Form\FormFactory;
24
25
class DatagridBuilder implements DatagridBuilderInterface
26
{
27
    protected $filterFactory;
28
29
    protected $formFactory;
30
31
    protected $guesser;
32
33
    protected $csrfTokenEnabled;
34
35
    /**
36
     * @param FormFactory            $formFactory
37
     * @param FilterFactoryInterface $filterFactory
38
     * @param TypeGuesserInterface   $guesser
39
     * @param boolean                $csrfTokenEnabled
40
     */
41
    public function __construct(FormFactory $formFactory, FilterFactoryInterface $filterFactory, TypeGuesserInterface $guesser, $csrfTokenEnabled = true)
42
    {
43
        $this->formFactory      = $formFactory;
44
        $this->filterFactory    = $filterFactory;
45
        $this->guesser          = $guesser;
46
        $this->csrfTokenEnabled = $csrfTokenEnabled;
47
    }
48
49
    /**
50
     * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
51
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
52
     *
53
     * @return void
54
     */
55
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
56
    {
57
        // set default values
58
        $fieldDescription->setAdmin($admin);
59
60
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
61
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
62
63
            // set the default field mapping
64
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
65
                $fieldDescription->setOption('field_mapping', $fieldDescription->getOption('field_mapping', $metadata->fieldMappings[$lastPropertyName]));
66
67
                if ($metadata->fieldMappings[$lastPropertyName]['type'] == 'string') {
68
                    $fieldDescription->setOption('global_search', $fieldDescription->getOption('global_search', true)); // always search on string field only
69
                }
70
            }
71
72
            // set the default association mapping
73
            if (isset($metadata->associationMappings[$lastPropertyName])) {
74
                $fieldDescription->setOption('association_mapping', $fieldDescription->getOption('association_mapping', $metadata->associationMappings[$lastPropertyName]));
75
            }
76
77
            $fieldDescription->setOption('parent_association_mappings', $fieldDescription->getOption('parent_association_mappings', $parentAssociationMappings));
78
        }
79
80
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
81
        $fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName()));
82
    }
83
84
    /**
85
     * @param \Sonata\AdminBundle\Datagrid\DatagridInterface      $datagrid
86
     * @param null                                                $type
87
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
88
     * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
89
     *
90
     * @return void
91
     */
92
    public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
93
    {
94
        if ($type == null) {
95
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
96
97
            $type = $guessType->getType();
98
99
            $fieldDescription->setType($type);
100
101
            $options = $guessType->getOptions();
102
103
            foreach ($options as $name => $value) {
104
                if (is_array($value)) {
105
                    $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, array())));
106
                } else {
107
                    $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
108
                }
109
            }
110
        } else {
111
            $fieldDescription->setType($type);
112
        }
113
114
        $this->fixFieldDescription($admin, $fieldDescription);
115
        $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
116
117
        $fieldDescription->mergeOption('field_options', array('required' => false));
118
        $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
119
120
        if (false !== $filter->getLabel() && !$filter->getLabel()) {
121
            $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
122
        }
123
124
        $datagrid->addFilter($filter);
125
    }
126
127
    /**
128
     * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
129
     * @param array                                    $values
130
     *
131
     * @return \Sonata\AdminBundle\Datagrid\DatagridInterface
132
     */
133
    public function getBaseDatagrid(AdminInterface $admin, array $values = array())
134
    {
135
        $pager = new Pager;
136
        $pager->setCountColumn($admin->getModelManager()->getIdentifierFieldNames($admin->getClass()));
137
138
        $defaultOptions = array();
139
        if ($this->csrfTokenEnabled) {
140
            $defaultOptions['csrf_protection'] = false;
141
        }
142
143
        $formBuilder = $this->formFactory->createNamedBuilder('filter', 'form', array(), $defaultOptions);
144
145
        return new Datagrid($admin->createQuery(), $admin->getList(), $pager, $formBuilder, $values);
0 ignored issues
show
Compatibility introduced by
$formBuilder of type object<Symfony\Component...m\FormBuilderInterface> is not a sub-type of object<Symfony\Component\Form\FormBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Form\FormBuilderInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
146
    }
147
}
148