Test Failed
Pull Request — master (#14)
by Pavel
05:24
created

SimpleCriteriaVisitor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A walkComparison() 0 10 3
A walkValue() 0 4 1
A walkCompositeExpression() 0 15 3
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Persister;
4
5
use Doctrine\Common\Collections\Expr\Comparison;
6
use Doctrine\Common\Collections\Expr\CompositeExpression;
7
use Doctrine\Common\Collections\Expr\ExpressionVisitor;
8
use Doctrine\Common\Collections\Expr\Value;
9
10
final class SimpleCriteriaVisitor extends ExpressionVisitor
11
{
12
    /**
13
     * Converts a comparison expression into the target query language output.
14
     *
15
     * @param Comparison $comparison
16
     *
17
     * @return mixed
18
     */
19
    public function walkComparison(Comparison $comparison)
20
    {
21
        switch ($comparison->getOperator()) {
22
            case Comparison::EQ:
23
            case Comparison::IN:
24
                return [$comparison->getField() => $this->walkValue($comparison->getValue())];
25
            default:
26
                throw new \InvalidArgumentException('Simple API queries support only EQ and IN operators');
27
        }
28
    }
29
30
    /**
31
     * Converts a value expression into the target query language part.
32
     *
33
     * @param Value $value
34
     *
35
     * @return mixed
36
     */
37
    public function walkValue(Value $value)
38
    {
39
        return $value->getValue();
40
    }
41
42
    /**
43
     * Converts a composite expression into the target query language output.
44
     *
45
     * @param CompositeExpression $expr
46
     *
47
     * @return mixed
48
     */
49
    public function walkCompositeExpression(CompositeExpression $expr)
50
    {
51
        $expressions = [];
52
        switch ($expr->getType()) {
53
            case CompositeExpression::TYPE_AND:
54
                foreach ($expr->getExpressionList() as $expression) {
55
                    $expressions[] = $this->dispatch($expression);
56
                }
57
                break;
58
            default:
59
                throw new \InvalidArgumentException('Simple API queries support only AND composite expression');
60
        }
61
62
        return call_user_func_array('array_replace', $expressions);
63
    }
64
}
65