Completed
Pull Request — master (#208)
by Simonas
124:56 queued 59:48
created

DocumentValue::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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 ONGR\FilterManagerBundle\Filter\Widget\Search;
13
14
use ONGR\ElasticsearchBundle\Mapping\Caser;
15
use ONGR\ElasticsearchDSL\Query\TermQuery;
16
use ONGR\ElasticsearchDSL\Search;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Filter\Relation\RelationAwareTrait;
19
use ONGR\FilterManagerBundle\Search\SearchRequest;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * Filter for filtering on exact value in specified field.
24
 */
25
class DocumentValue extends AbstractSingleValue
26
{
27
28
    use RelationAwareTrait;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getState(Request $request)
34
    {
35
        $state = new FilterState();
36
        $document = $request->get('document');
37
38
        if (is_object($document)) {
39
            try {
40
                $state->setValue(function ($document) {return $document->{$this->getOption('field')};});
41
                $state->setActive(true);
42
            } catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
43
        }
44
45
        return $state;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
52
    {
53
        $search->addPostFilter(new TermQuery($this->getDocumentField(), $state->getValue()));
0 ignored issues
show
Bug introduced by
It seems like $state is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
54
    }
55
}
56