DatagridBuilder   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 10
dl 0
loc 144
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setPager() 0 4 1
A getPager() 0 8 2
A fixFieldDescription() 0 26 5
B addFilter() 0 31 6
A getBaseDatagrid() 0 16 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\DoctrinePHPCRAdminBundle\Builder;
15
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
18
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
19
use Sonata\AdminBundle\Datagrid\Datagrid;
20
use Sonata\AdminBundle\Datagrid\DatagridInterface;
21
use Sonata\AdminBundle\Datagrid\PagerInterface;
22
use Sonata\AdminBundle\Datagrid\SimplePager;
23
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
24
use Sonata\AdminBundle\Filter\FilterInterface;
25
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
26
use Symfony\Component\Form\FormFactory;
27
28
class DatagridBuilder implements DatagridBuilderInterface
29
{
30
    /**
31
     * @var FilterFactoryInterface
32
     */
33
    protected $filterFactory;
34
35
    /**
36
     * @var FormFactory
37
     */
38
    protected $formFactory;
39
40
    /**
41
     * @var TypeGuesserInterface
42
     */
43
    protected $guesser;
44
45
    /**
46
     * Indicates that csrf protection enabled.
47
     *
48
     * @var bool
49
     */
50
    protected $csrfTokenEnabled;
51
52
    /**
53
     * @var PagerInterface
54
     */
55
    protected $pager;
56
57
    /**
58
     * @param bool $csrfTokenEnabled
59
     */
60
    public function __construct(FormFactory $formFactory, FilterFactoryInterface $filterFactory, TypeGuesserInterface $guesser, $csrfTokenEnabled = true)
61
    {
62
        $this->formFactory = $formFactory;
63
        $this->filterFactory = $filterFactory;
64
        $this->guesser = $guesser;
65
        $this->csrfTokenEnabled = $csrfTokenEnabled;
66
    }
67
68
    public function setPager(PagerInterface $pager): void
69
    {
70
        $this->pager = $pager;
71
    }
72
73
    /**
74
     * @return PagerInterface
75
     */
76
    public function getPager()
77
    {
78
        if (null === $this->pager) {
79
            $this->pager = new SimplePager();
80
        }
81
82
        return $this->pager;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void
89
    {
90
        // set default values
91
        $fieldDescription->setAdmin($admin);
92
93
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
94
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
95
96
            // set the default field mapping
97
            if (isset($metadata->mappings[$fieldDescription->getName()])) {
98
                $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
99
100
                if ('string' === $metadata->mappings[$fieldDescription->getName()]['type']) {
101
                    $fieldDescription->setOption('global_search', $fieldDescription->getOption('global_search', true)); // always search on string field only
102
                }
103
            }
104
105
            // set the default association mapping
106
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
107
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
108
            }
109
        }
110
111
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
112
        $fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName()));
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     *
118
     * @return FilterInterface
119
     */
120
    public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
121
    {
122
        if (null === $type) {
123
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
124
            $type = $guessType->getType();
125
            $fieldDescription->setType($type);
126
            $options = $guessType->getOptions();
127
128
            foreach ($options as $name => $value) {
129
                if (\is_array($value)) {
130
                    $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, [])));
131
                } else {
132
                    $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
133
                }
134
            }
135
        } else {
136
            $fieldDescription->setType($type);
137
        }
138
139
        $this->fixFieldDescription($admin, $fieldDescription);
140
        $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
141
142
        $fieldDescription->mergeOption('field_options', ['required' => false]);
143
        $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
144
145
        if (false !== $filter->getLabel() && !$filter->getLabel()) {
146
            $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
147
        }
148
149
        return $datagrid->addFilter($filter);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function getBaseDatagrid(AdminInterface $admin, array $values = [])
156
    {
157
        $defaultOptions = [];
158
        if ($this->csrfTokenEnabled) {
159
            $defaultOptions['csrf_protection'] = false;
160
        }
161
162
        $formBuilder = $this->formFactory->createNamedBuilder(
163
            'filter',
164
            'Symfony\Component\Form\Extension\Core\Type\FormType',
165
            [],
166
            $defaultOptions
167
        );
168
169
        return new Datagrid($admin->createQuery(), $admin->getList(), $this->getPager(), $formBuilder, $values);
170
    }
171
}
172