Completed
Push — master ( a8e426...cdcd92 )
by Simonas
62:46
created

Filter/Widget/Range/Range.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Range;
13
14
use ONGR\ElasticsearchDSL\Aggregation\Metric\StatsAggregation;
15
use ONGR\ElasticsearchDSL\Search;
16
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Filter\ViewData;
19
use Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * Range filter, selects documents from lower limit to upper limit.
23
 */
24
class Range extends AbstractRange
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getState(Request $request)
30
    {
31
        $state = parent::getState($request);
32
33
        if (!$state->isActive()) {
34
            return $state;
35
        }
36
37
        $values = explode(';', $state->getValue(), 2);
38
39
        if (count($values) < 2) {
40
            $state->setActive(false);
41
42
            return $state;
43
        }
44
45
        $gt = $this->isInclusive() ? 'gte' : 'gt';
46
        $lt = $this->isInclusive() ? 'lte' : 'lt';
47
48
        $normalized[$gt] = $values[0];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$normalized was never initialized. Although not strictly required by PHP, it is generally a good practice to add $normalized = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
49
        $normalized[$lt] = $values[1];
50
51
        $state->setValue($normalized);
52
53
        return $state;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null)
60
    {
61
        $stateAgg = new StatsAggregation($state->getName());
62
        $stateAgg->setField($this->getDocumentField());
63
        $search->addAggregation($stateAgg);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getViewData(DocumentIterator $result, ViewData $data)
70
    {
71
        $name = $data->getState()->getName();
72
        /** @var $data ViewData\RangeAwareViewData */
73
        $data->setMinBounds($result->getAggregation($name)['min']);
74
        $data->setMaxBounds($result->getAggregation($name)['max']);
75
76
        return $data;
77
    }
78
}
79