Passed
Push — master ( 6afdcd...dc022a )
by Hirofumi
05:03
created

whereExpression()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Domain\Model;
5
6
use Adbar\Dot;
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\Common\Collections\Expr\Comparison;
9
use Doctrine\Common\Collections\Expr\Expression;
10
use RuntimeException;
11
use Tanigami\Specification\Specification;
12
13
class NotificationMetadataSpecification extends Specification
14
{
15
    /**
16
     * @var Comparison
17
     */
18
    protected $expression;
19
20
    /**
21
     * @param string $key
22
     * @param string $operator
23
     * @param mixed $value
24
     */
25 11
    public function __construct(string $key, string $operator, $value)
26
    {
27 11
        $this->expression = new Comparison($key, $operator, $value);
28 11
    }
29
30
    /**
31
     * @param Notification $notification
32
     * @return bool
33
     */
34 10
    public function isSatisfiedBy($notification): bool
35
    {
36 10
        $value = (new Dot($notification->metadata()))->get($this->expression->getField());
37 10
        if (is_null($value)) {
38 2
            return false;
39
        }
40 9
        $operator = $this->expression->getOperator();
41 9
        $expressionValue = $this->expression->getValue()->getValue();
42
        switch ($operator) {
43 9
            case Comparison::EQ:
44 2
                return $value === $expressionValue;
45 7
            case Comparison::NEQ:
46 1
                return $value !== $expressionValue;
47 6
            case Comparison::CONTAINS:
48 1
                return strpos($value, $expressionValue) !== false;
49 5
            case Comparison::GT:
50 1
                return $value > $expressionValue;
51 4
            case Comparison::GTE:
52 1
                return $value >= $expressionValue;
53 3
            case Comparison::LT:
54 1
                return $value < $expressionValue;
55 2
            case Comparison::LTE:
56 1
                return $value <= $expressionValue;
57 1
            case Comparison::IN:
58 1
                return in_array($value, $expressionValue, true);
59
            default:
60
                throw new RuntimeException(sprintf('Unknown operator: %s', $operator));
61
        }
62
    }
63
64
    /**
65
     * @return Comparison
66
     */
67 1
    public function expression(): Comparison
68
    {
69 1
        return $this->expression;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function whereExpression(string $alias): string
76
    {
77 1
        $value = $this->expression()->getValue()->getValue();
78 1
        if (is_string($value)) {
79 1
            $value = "'$value'";
80
        }
81
82 1
        return sprintf(
83 1
            "JSON_EXTRACT(%s.metadata, '$.%s') %s %s",
84 1
            $alias,
85 1
            $this->expression()->getField(),
86 1
            $this->expression()->getOperator(),
87 1
            $value
88
        );
89
    }
90
}
91