Completed
Pull Request — master (#144)
by Mantas
63:58
created

DocumentField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getField() 0 10 2
A getState() 0 13 2
A extractDocumentValue() 0 10 2
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\Document\DocumentInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * This class filters results by request document's field value.
19
 */
20
class DocumentField extends MatchSearch
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getField()
26
    {
27
        $field = parent::getField();
28
29
        if ($field === null) {
30
            throw new \InvalidArgumentException('Field must be set.');
31
        }
32
33
        return $field;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getState(Request $request)
40
    {
41
        $state = parent::getState($request);
42
43
        $value = $this->extractDocumentValue($request);
44
45
        if ($value !== null) {
46
            $state->setValue($value);
47
            $state->setActive(true);
48
        }
49
50
        return $state;
51
    }
52
53
    /**
54
     * Extracts document value.
55
     *
56
     * @param Request $request
57
     *
58
     * @return mixed
59
     */
60
    protected function extractDocumentValue(Request $request)
61
    {
62
        $document = $request->get($this->getRequestField());
63
64
        if (!$document instanceof DocumentInterface) {
65
            return null;
66
        }
67
68
        return $document->getId();
69
    }
70
}
71