CallbackFilter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 44
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 8 2
A getDefaultOptions() 0 9 1
A getRenderSettings() 0 10 1
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\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\DoctrinePHPCRAdminBundle\Filter\Filter as BaseFilter;
18
19
class CallbackFilter extends BaseFilter
20
{
21
    /**
22
     * {@inheritdoc}
23
     *
24
     * @throws \InvalidArgumentException if the filter is not configured with a
25
     *                                   callable in the 'callback' option field
26
     */
27
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data): void
28
    {
29
        if (!\is_callable($this->getOption('callback'))) {
30
            throw new \RuntimeException(sprintf('Please provide a valid callback for option "callback" and field "%s"', $this->getName()));
31
        }
32
33
        $this->active = true === \call_user_func($this->getOption('callback'), $proxyQuery, $alias, $field, $data);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getDefaultOptions()
40
    {
41
        return [
42
            'callback' => null,
43
            'field_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
44
            'operator_type' => 'hidden',
45
            'operator_options' => [],
46
        ];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getRenderSettings()
53
    {
54
        return ['sonata_type_filter_default', [
55
            'field_type' => $this->getFieldType(),
56
            'field_options' => $this->getFieldOptions(),
57
            'operator_type' => $this->getOption('operator_type'),
58
            'operator_options' => $this->getOption('operator_options'),
59
            'label' => $this->getLabel(),
60
        ]];
61
    }
62
}
63