Completed
Push — master ( 702e62...20fc8a )
by André
124:57 queued 101:37
created

SubtreeIn::canVisit()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.2
cc 4
eloc 5
nc 4
nop 1
1
<?php
2
3
/**
4
 * File containing the SubtreeIn criterion visitor class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Search\Elasticsearch\Content\Location\CriterionVisitor;
12
13
use eZ\Publish\Core\Search\Elasticsearch\Content\CriterionVisitorDispatcher as Dispatcher;
14
use eZ\Publish\Core\Search\Elasticsearch\Content\CriterionVisitor;
15
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
16
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
17
18
/**
19
 * Visits the Subtree criterion.
20
 */
21
class SubtreeIn extends CriterionVisitor
22
{
23
    /**
24
     * Check if visitor is applicable to current criterion.
25
     *
26
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
27
     *
28
     * @return bool
29
     */
30
    public function canVisit(Criterion $criterion)
31
    {
32
        return
33
            $criterion instanceof Criterion\Subtree &&
34
            (
35
                ($criterion->operator ?: Operator::IN) === Operator::IN ||
36
                $criterion->operator === Operator::EQ
37
            );
38
    }
39
40
    /**
41
     * Returns condition common for filter and query contexts.
42
     *
43
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
44
     *
45
     * @return array
46
     */
47 View Code Duplication
    protected function getCondition(Criterion $criterion)
48
    {
49
        $filters = array();
50
51
        foreach ($criterion->value as $value) {
0 ignored issues
show
Bug introduced by
The expression $criterion->value of type integer|string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
52
            $filters[] = array(
53
                'prefix' => array(
54
                    'path_string_id' => $value,
55
                ),
56
            );
57
        }
58
59
        return $filters;
60
    }
61
62
    /**
63
     * Map field value to a proper Elasticsearch representation.
64
     *
65
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
66
     * @param \eZ\Publish\Core\Search\Elasticsearch\Content\CriterionVisitorDispatcher $dispatcher
67
     * @param array $languageFilter
68
     *
69
     * @return mixed
70
     */
71
    public function visitFilter(Criterion $criterion, Dispatcher $dispatcher, array $languageFilter)
72
    {
73
        return array(
74
            'or' => $this->getCondition($criterion),
75
        );
76
    }
77
78
    /**
79
     * Map field value to a proper Elasticsearch query representation.
80
     *
81
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
82
     * @param \eZ\Publish\Core\Search\Elasticsearch\Content\CriterionVisitorDispatcher $dispatcher
83
     * @param array $languageFilter
84
     *
85
     * @return mixed
86
     */
87
    public function visitQuery(Criterion $criterion, Dispatcher $dispatcher, array $languageFilter)
88
    {
89
        return array(
90
            'bool' => array(
91
                'should' => $this->getCondition($criterion),
92
                'minimum_should_match' => 1,
93
            ),
94
        );
95
    }
96
}
97