DatagridBuilder   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 11

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 11
dl 0
loc 112
rs 10
c 0
b 0
f 0

4 Methods

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