ExpressionVisitor::walkComparison()   C
last analyzed

Complexity

Conditions 13
Paths 13

Size

Total Lines 48
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 5.0877
c 0
b 0
f 0
cc 13
eloc 31
nc 13
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm;
6
7
use Doctrine\ODM\PHPCR\Query\Builder\AbstractNode;
8
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
9
use Psi\Component\ObjectAgent\Query\Comparison;
10
use Psi\Component\ObjectAgent\Query\Composite;
11
use Psi\Component\ObjectAgent\Query\Expression;
12
13
class ExpressionVisitor
14
{
15
    private $queryBuilder;
16
    private $sourceAlias;
17
18
    /**
19
     * @param QueryBuilder $queryBuilder
20
     */
21
    public function __construct(QueryBuilder $queryBuilder, string $sourceAlias)
22
    {
23
        $this->queryBuilder = $queryBuilder;
24
        $this->sourceAlias = $sourceAlias;
25
    }
26
27
    /**
28
     * Walk the given expression to build up the PHPCR-ODM query builder.
29
     *
30
     * @param Expression $expr
31
     * @param AbstractNode|null $parentNode
32
     */
33
    public function dispatch(Expression $expr, AbstractNode $parentNode = null)
34
    {
35
        if ($parentNode === null) {
36
            $parentNode = $this->queryBuilder->where();
37
        }
38
39 View Code Duplication
        switch (true) {
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...
40
            case $expr instanceof Comparison:
41
                return $this->walkComparison($expr, $parentNode);
42
43
            case $expr instanceof Composite:
44
                return $this->walkComposite($expr, $parentNode);
45
        }
46
47
        throw new \RuntimeException('Unknown Expression: ' . get_class($expr));
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    private function walkComparison(Comparison $comparison, AbstractNode $parentNode)
54
    {
55
        $field = $comparison->getField();
56
        $value = $comparison->getValue();
57
58
        switch ($comparison->getComparator()) {
59
            case Comparison::EQUALS:
60
                return $parentNode->eq()->field($this->getField($field))->literal($value)->end();
61
62
            case Comparison::NOT_EQUALS:
63
                return $parentNode->neq()->field($this->getField($field))->literal($value)->end();
64
65
            case Comparison::LESS_THAN:
66
                return $parentNode->lt()->field($this->getField($field))->literal($value)->end();
67
68
            case Comparison::LESS_THAN_EQUAL:
69
                return $parentNode->lte()->field($this->getField($field))->literal($value)->end();
70
71
            case Comparison::GREATER_THAN:
72
                return $parentNode->gt()->field($this->getField($field))->literal($value)->end();
73
74
            case Comparison::GREATER_THAN_EQUAL:
75
                return $parentNode->gte()->field($this->getField($field))->literal($value)->end();
76
77
            case Comparison::IN:
78
                return $this->getInConstraint($parentNode, $field, $value);
79
80
            case Comparison::NOT_IN:
81
                $node = $parentNode->not();
82
                $this->getInConstraint($node, $field, $value);
83
84
                return $node->end();
85
86
            case Comparison::CONTAINS:
87
                return $parentNode->like()->field($this->getField($field))->literal($value)->end();
88
89
            case Comparison::NOT_CONTAINS:
90
                return $parentNode->not()->like()->field($this->getField($field))->literal($value)->end()->end();
91
92
            case Comparison::NULL:
93
                return $parentNode->not()->fieldIsset($this->getField($field))->end();
94
95
            case Comparison::NOT_NULL:
96
                return $parentNode->fieldIsset($this->getField($field));
97
        }
98
99
        throw new \RuntimeException('Unknown comparator: ' . $comparison->getComparator());
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    private function walkComposite(Composite $expression, AbstractNode $parentNode)
106
    {
107
        $node = $expression->getType() === Composite::AND ? $parentNode->andX() : $parentNode->orX();
108
109
        $expressions = $expression->getExpressions();
110
111
        if (empty($expressions)) {
112
            return $node;
113
        }
114
115
        $leftExpression = array_shift($expressions);
116
        $this->dispatch($leftExpression, $node);
117
118
        $parentNode = $node;
119
        foreach ($expressions as $index => $expression) {
120
            if (count($expressions) === $index + 1) {
121
                $this->dispatch($expression, $parentNode);
122
                break;
123
            }
124
125
            $this->dispatch($expression, $parentNode);
126
        }
127
128
        return $node;
129
    }
130
131
    /**
132
     * @param string $field
133
     *
134
     * @return string
135
     */
136
    private function getField($field): string
137
    {
138
        return $this->sourceAlias . '.' . $field;
139
    }
140
141
    /**
142
     * @param AbstractNode $parentNode
143
     * @param string $field
144
     * @param array $values
145
     */
146
    private function getInConstraint(AbstractNode $parentNode, $field, array $values)
147
    {
148
        $orNode = $parentNode->orx();
149
150
        foreach ($values as $value) {
151
            $orNode->eq()->field($this->getField($field))->literal($value);
152
        }
153
154
        $orNode->end();
155
    }
156
}
157