Completed
Push — permissions_subtree_internal ( 4ae292...58fc43 )
by André
38:07 queued 12:38
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
use eZ\Publish\Core\Repository\Values\Content\Query\Criterion\PermissionSubtree;
18
19
/**
20
 * Visits the Subtree criterion.
21
 */
22
class SubtreeIn extends CriterionVisitor
23
{
24
    /**
25
     * Check if visitor is applicable to current criterion.
26
     *
27
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
28
     *
29
     * @return bool
30
     */
31
    public function canVisit(Criterion $criterion)
32
    {
33
        return
34
            $criterion instanceof Criterion\Subtree &&
35
            (
36
                ($criterion->operator ?: Operator::IN) === Operator::IN ||
37
                $criterion->operator === Operator::EQ
38
            );
39
    }
40
41
    /**
42
     * Returns condition common for filter and query contexts.
43
     *
44
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
45
     *
46
     * @return array
47
     */
48 View Code Duplication
    protected function getCondition(Criterion $criterion)
49
    {
50
        $filters = array();
51
52
        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...
53
            $filters[] = array(
54
                'prefix' => array(
55
                    'path_string_id' => $value,
56
                ),
57
            );
58
        }
59
60
        return $filters;
61
    }
62
63
    /**
64
     * Map field value to a proper Elasticsearch representation.
65
     *
66
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
67
     * @param \eZ\Publish\Core\Search\Elasticsearch\Content\CriterionVisitorDispatcher $dispatcher
68
     * @param array $languageFilter
69
     *
70
     * @return mixed
71
     */
72
    public function visitFilter(Criterion $criterion, Dispatcher $dispatcher, array $languageFilter)
73
    {
74
        return array(
75
            'or' => $this->getCondition($criterion),
76
        );
77
    }
78
79
    /**
80
     * Map field value to a proper Elasticsearch query representation.
81
     *
82
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
83
     * @param \eZ\Publish\Core\Search\Elasticsearch\Content\CriterionVisitorDispatcher $dispatcher
84
     * @param array $languageFilter
85
     *
86
     * @return mixed
87
     */
88
    public function visitQuery(Criterion $criterion, Dispatcher $dispatcher, array $languageFilter)
89
    {
90
        return array(
91
            'bool' => array(
92
                'should' => $this->getCondition($criterion),
93
                'minimum_should_match' => 1,
94
            ),
95
        );
96
    }
97
}
98