Completed
Push — 3.x ( 839353...0c5d0b )
by Grégoire
02:14
created

ModelFilter   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
B filter() 0 18 6
A getDefaultOptions() 0 11 1
A getRenderSettings() 0 10 1
B handleMultiple() 0 19 5
A handleScalar() 0 16 4
A fixIdentifier() 0 8 2
A getIdentifierField() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project 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\DoctrineMongoDBAdminBundle\Filter;
13
14
use Doctrine\Common\Collections\Collection;
15
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
16
use Sonata\CoreBundle\Form\Type\EqualType;
17
18
class ModelFilter extends Filter
19
{
20
    /**
21
     * @param ProxyQueryInterface $queryBuilder
22
     * @param string              $alias
23
     * @param string              $field
24
     * @param mixed               $data
25
     *
26
     * @return
27
     */
28
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
29
    {
30
        if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
31
            return;
32
        }
33
34
        if ($data['value'] instanceof Collection) {
35
            $data['value'] = $data['value']->toArray();
36
        }
37
38
        $field = $this->getIdentifierField($field);
39
40
        if (is_array($data['value'])) {
41
            $this->handleMultiple($queryBuilder, $alias, $field, $data);
0 ignored issues
show
Documentation introduced by
$alias is of type string, but the function expects a object<Sonata\DoctrineMo...dminBundle\Filter\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$field is of type string, but the function expects a object<Sonata\DoctrineMo...dminBundle\Filter\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$data is of type array<string,array,{"value":"array"}>, but the function expects a object<Sonata\DoctrineMo...dminBundle\Filter\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
        } else {
43
            $this->handleScalar($queryBuilder, $alias, $field, $data);
0 ignored issues
show
Documentation introduced by
$alias is of type string, but the function expects a object<Sonata\DoctrineMo...dminBundle\Filter\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$field is of type string, but the function expects a object<Sonata\DoctrineMo...dminBundle\Filter\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$data is of type array<string,?,{"value":"?"}>, but the function expects a object<Sonata\DoctrineMo...dminBundle\Filter\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        }
45
    }
46
47
    public function getDefaultOptions()
48
    {
49
        return array(
50
            'mapping_type' => false,
51
            'field_name' => false,
52
            'field_type' => 'document',
53
            'field_options' => array(),
54
            'operator_type' => 'sonata_type_equal',
55
            'operator_options' => array(),
56
        );
57
    }
58
59
    public function getRenderSettings()
60
    {
61
        return array('sonata_type_filter_default', array(
62
            'field_type' => $this->getFieldType(),
63
            'field_options' => $this->getFieldOptions(),
64
            'operator_type' => $this->getOption('operator_type'),
65
            'operator_options' => $this->getOption('operator_options'),
66
            'label' => $this->getLabel(),
67
        ));
68
    }
69
70
    /**
71
     * @param ProxyQueryInterface $queryBuilder
72
     * @param type                $alias
73
     * @param type                $field
74
     * @param type                $data
75
     *
76
     * @return type
77
     */
78
    protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
0 ignored issues
show
Unused Code introduced by
The parameter $alias is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        if (count($data['value']) == 0) {
81
            return;
82
        }
83
84
        $ids = array();
85
        foreach ($data['value'] as $value) {
86
            $ids[] = self::fixIdentifier($value->getId());
87
        }
88
89
        if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
90
            $queryBuilder->field($field)->notIn($ids);
91
        } else {
92
            $queryBuilder->field($field)->in($ids);
93
        }
94
95
        $this->active = true;
96
    }
97
98
    /**
99
     * @param ProxyQueryInterface $queryBuilder
100
     * @param type                $alias
101
     * @param type                $field
102
     * @param type                $data
103
     *
104
     * @return type
105
     */
106
    protected function handleScalar(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
0 ignored issues
show
Unused Code introduced by
The parameter $alias is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108
        if (empty($data['value'])) {
109
            return;
110
        }
111
112
        $id = self::fixIdentifier($data['value']->getId());
113
114
        if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
115
            $queryBuilder->field($field)->notEqual($id);
116
        } else {
117
            $queryBuilder->field($field)->equals($id);
118
        }
119
120
        $this->active = true;
121
    }
122
123
    /**
124
     * Return \MongoId if $id is MongoId in string representation, otherwise custom string.
125
     *
126
     * @param mixed $id
127
     *
128
     * @return \MongoId|string
129
     */
130
    protected static function fixIdentifier($id)
131
    {
132
        try {
133
            return new \MongoId($id);
134
        } catch (\MongoException $ex) {
135
            return $id;
136
        }
137
    }
138
139
    /**
140
     * Identifier field name is 'field' if mapping type is simple; otherwise, it's 'field.$id'.
141
     *
142
     * @param string $field
143
     *
144
     * @return string
145
     */
146
    protected function getIdentifierField($field)
147
    {
148
        $field_mapping = $this->getFieldMapping();
149
150
        return (true === $field_mapping['simple']) ? $field : $field.'.$id';
151
    }
152
}
153