1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Bridge\ObjectAgent\Doctrine\Collections; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ExpressionBuilder; |
8
|
|
|
use Psi\Component\ObjectAgent\Exception\BadMethodCallException; |
9
|
|
|
use Psi\Component\ObjectAgent\Query\Comparison; |
10
|
|
|
use Psi\Component\ObjectAgent\Query\Composite; |
11
|
|
|
use Psi\Component\ObjectAgent\Query\Expression; |
12
|
|
|
|
13
|
|
|
class CollectionsVisitor |
14
|
|
|
{ |
15
|
|
|
private $expressionBuilder; |
16
|
|
|
|
17
|
|
|
public function __construct(ExpressionBuilder $expressionBuilder) |
18
|
|
|
{ |
19
|
|
|
$this->expressionBuilder = $expressionBuilder; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function dispatch(Expression $expr) |
23
|
|
|
{ |
24
|
|
View Code Duplication |
switch (true) { |
|
|
|
|
25
|
|
|
case $expr instanceof Comparison: |
26
|
|
|
return $this->walkComparison($expr); |
27
|
|
|
break; |
|
|
|
|
28
|
|
|
|
29
|
|
|
case $expr instanceof Composite: |
30
|
|
|
return $this->walkComposite($expr); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
throw new \RuntimeException('Unknown Expression: ' . get_class($expr)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function walkComparison(Comparison $comparison) |
37
|
|
|
{ |
38
|
|
|
$field = $comparison->getField(); |
39
|
|
|
$value = $comparison->getValue(); |
40
|
|
|
|
41
|
|
|
switch ($comparison->getComparator()) { |
42
|
|
|
case Comparison::EQUALS: |
43
|
|
|
return $this->expressionBuilder->eq($field, $value); |
44
|
|
|
|
45
|
|
|
case Comparison::NOT_EQUALS: |
46
|
|
|
return $this->expressionBuilder->neq($field, $value); |
47
|
|
|
|
48
|
|
|
case Comparison::LESS_THAN: |
49
|
|
|
return $this->expressionBuilder->lt($field, $value); |
50
|
|
|
|
51
|
|
|
case Comparison::LESS_THAN_EQUAL: |
52
|
|
|
return $this->expressionBuilder->lte($field, $value); |
53
|
|
|
|
54
|
|
|
case Comparison::GREATER_THAN: |
55
|
|
|
return $this->expressionBuilder->gt($field, $value); |
56
|
|
|
|
57
|
|
|
case Comparison::GREATER_THAN_EQUAL: |
58
|
|
|
return $this->expressionBuilder->gte($field, $value); |
59
|
|
|
|
60
|
|
|
case Comparison::IN: |
61
|
|
|
return $this->expressionBuilder->in($field, $value); |
62
|
|
|
|
63
|
|
|
case Comparison::NOT_IN: |
64
|
|
|
return $this->expressionBuilder->notIn($field, $value); |
65
|
|
|
|
66
|
|
|
case Comparison::CONTAINS: |
67
|
|
|
return $this->expressionBuilder->contains($field, $value); |
68
|
|
|
|
69
|
|
|
case Comparison::NOT_CONTAINS: |
70
|
|
|
throw BadMethodCallException::comparisonNotSupported(Comparison::NOT_CONTAINS); |
71
|
|
|
case Comparison::NULL: |
72
|
|
|
return $this->expressionBuilder->isNull($field, $value); |
|
|
|
|
73
|
|
|
|
74
|
|
|
case Comparison::NOT_NULL: |
75
|
|
|
throw BadMethodCallException::comparisonNotSupported(Comparison::NOT_NULL); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
throw new \RuntimeException('Unknown comparator: ' . $comparison->getComparator()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
private function walkComposite(Composite $expression) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$expressions = $expression->getExpressions(); |
84
|
|
|
|
85
|
|
|
$ormExpressions = []; |
86
|
|
|
foreach ($expressions as $index => $childExpression) { |
87
|
|
|
$ormExpressions[] = $this->dispatch($childExpression); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$method = $expression->getType() == Composite::AND ? 'andX' : 'orX'; |
91
|
|
|
|
92
|
|
|
return call_user_func_array([$this->expressionBuilder, $method], $ormExpressions); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.