Completed
Push — master ( cc21e8...5c80a5 )
by Simonas
65:42
created

MatchSearch   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 8.22 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 6
loc 73
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B modifySearch() 0 14 5
B buildMatchPart() 6 19 5
A setOperator() 0 4 1
A setFuzziness() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the ONGR package.
4
 *
5
 * (c) NFQ Technologies UAB <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace ONGR\FilterManagerBundle\Filter\Widget\Search;
11
12
use ONGR\ElasticsearchDSL\BuilderInterface;
13
use ONGR\ElasticsearchDSL\Query\BoolQuery;
14
use ONGR\ElasticsearchDSL\Query\MatchQuery;
15
use ONGR\ElasticsearchDSL\Query\NestedQuery;
16
use ONGR\ElasticsearchDSL\Search;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Search\SearchRequest;
19
20
/**
21
 * This class runs match search.
22
 */
23
class MatchSearch extends AbstractSingleValue
24
{
25
    /**
26
     * @var array
27
     */
28
    private $parameters = [];
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
34
    {
35
        if ($state && $state->isActive()) {
36
            if (strpos($this->getField(), ',') !== false) {
37
                $subQuery = new BoolQuery();
38
                foreach (explode(',', $this->getField()) as $field) {
39
                    $subQuery->add($this->buildMatchPart($field, $state), 'should');
40
                }
41
                $search->addQuery($subQuery, 'must');
42
            } else {
43
                $search->addQuery($this->buildMatchPart($this->getField(), $state), 'must');
44
            }
45
        }
46
    }
47
48
    /**
49
     * Build possibly nested match part.
50
     *
51
     * @param string      $field
52
     * @param FilterState $state
53
     *
54
     * @return BuilderInterface
55
     */
56
    private function buildMatchPart($field, FilterState $state)
57
    {
58 View Code Duplication
        if (strpos($field, '+') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            list ($path, $field) = explode('+', $field);
60
        }
61
62 View Code Duplication
        if (strpos($field, '^') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            list ($field, $boost) = explode('^', $field);
64
        }
65
66
        $query =  new MatchQuery($field, $state->getValue(), $this->parameters);
67
        !isset($boost) ? : $query->addParameter('boost', $boost);
68
69
        if (isset($path)) {
70
            $query = new NestedQuery($path, $query);
71
        }
72
73
        return $query;
74
    }
75
76
    /**
77
     * Sets operator
78
     *
79
     * @param string $operator
80
     */
81
    public function setOperator($operator)
82
    {
83
        $this->parameters['operator'] = $operator;
84
    }
85
86
    /**
87
     * Sets the maximum edit distance
88
     *
89
     * @param string|int|float $fuzziness
90
     */
91
    public function setFuzziness($fuzziness)
92
    {
93
        $this->parameters['fuzziness'] = $fuzziness;
94
    }
95
}
96