Completed
Push — master ( 8386c2...ffd2be )
by Simonas
61:26
created

DocumentValue   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 6
dl 0
loc 78
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDocumentField() 0 4 1
A setDocumentField() 0 4 1
A setValue() 0 6 1
A getValue() 0 4 1
A getState() 0 12 2
A modifySearch() 0 4 1
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\ElasticsearchDSL\Query\TermQuery;
15
use ONGR\ElasticsearchDSL\Search;
16
use ONGR\FilterManagerBundle\Filter\FilterState;
17
use ONGR\FilterManagerBundle\Filter\Relation\RelationAwareInterface;
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 implements RelationAwareInterface
26
{
27
    use RelationAwareTrait;
28
29
    /**
30
     * Field name of the document object.
31
     *
32
     * @var string
33
     */
34
    private $documentField;
35
36
    /**
37
     * @var string
38
     */
39
    protected $value;
40
41
    /**
42
     * @return string
43
     */
44
    public function getDocumentField()
45
    {
46
        return $this->documentField;
47
    }
48
49
    /**
50
     * @param string $documentField
51
     */
52
    public function setDocumentField($documentField)
53
    {
54
        $this->documentField = $documentField;
55
    }
56
57
    /**
58
     * Setter for field value.
59
     *
60
     * @param $value
61
     *
62
     * @return $this
63
     */
64
    public function setValue($value)
65
    {
66
        $this->value = $value;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getValue()
75
    {
76
        return $this->value;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getState(Request $request)
83
    {
84
        $state = new FilterState();
85
        $document = $request->get('document');
86
87
        if ($document) {
88
            $this->setValue($document->{$this->getDocumentField()});
89
            $state->setActive(true);
90
        }
91
92
        return $state;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
99
    {
100
        $search->addPostFilter(new TermQuery($this->getField(), $this->getValue()));
101
    }
102
}
103