Completed
Pull Request — v0.10 (#630)
by Tautrimas
93:19
created

CardinalityAggregation::setPrecisionThreshold()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %
Metric Value
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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\ElasticsearchBundle\DSL\Aggregation;
13
14
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait;
15
use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait;
16
17
/**
18
 * Difference values counter.
19
 */
20 View Code Duplication
class CardinalityAggregation extends AbstractAggregation
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
21
{
22
    use MetricTrait;
23
    use ScriptAwareTrait;
24
25
    /**
26
     * @var int
27
     */
28
    private $precisionThreshold;
29
30
    /**
31
     * @var bool
32
     */
33
    private $rehash;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getArray()
39
    {
40
        $out = array_filter(
41
            [
42
                'field' => $this->getField(),
43
                'script' => $this->getScript(),
44
                'precision_threshold' => $this->getPrecisionThreshold(),
45
                'rehash' => $this->isRehash(),
46
            ],
47
            function ($val) {
48
                return ($val || is_bool($val));
49
            }
50
        );
51
52
        $this->checkRequiredFields($out);
53
54
        return $out;
55
    }
56
57
    /**
58
     * Precision threshold.
59
     *
60
     * @param int $precision Precision Threshold.
61
     */
62
    public function setPrecisionThreshold($precision)
63
    {
64
        $this->precisionThreshold = $precision;
65
    }
66
67
    /**
68
     * @return int
69
     */
70
    public function getPrecisionThreshold()
71
    {
72
        return $this->precisionThreshold;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    public function isRehash()
79
    {
80
        return $this->rehash;
81
    }
82
83
    /**
84
     * @param bool $rehash
85
     */
86
    public function setRehash($rehash)
87
    {
88
        $this->rehash = $rehash;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getType()
95
    {
96
        return 'cardinality';
97
    }
98
99
    /**
100
     * Checks if required fields are set.
101
     *
102
     * @param array $fields
103
     *
104
     * @throws \LogicException
105
     */
106
    private function checkRequiredFields($fields)
107
    {
108
        if (!array_key_exists('field', $fields) && !array_key_exists('script', $fields)) {
109
            throw new \LogicException('Cardinality aggregation must have field or script set.');
110
        }
111
    }
112
}
113