Test Failed
Push — master ( 803dec...3fd2d6 )
by Hirofumi
05:43
created

NotificationMetadataSpecification::isSatisfiedBy()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 1

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
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Domain\Model;
5
6
use Adbar\Dot;
7
use Doctrine\Common\Collections\Expr\Comparison;
8
use RuntimeException;
9
use Tanigami\Specification\Specification;
10
11
class NotificationMetadataSpecification extends Specification
12
{
13
    /**
14
     * @var Comparison
15
     */
16
    private $expression;
17
18
    /**
19
     * @param string $key
20
     * @param string $operator
21
     * @param mixed $value
22
     */
23
    public function __construct(string $key, string $operator, $value)
24
    {
25
        $this->expression = new Comparison($key, $operator, $value);
26
    }
27
28
    /**
29
     * @param Notification $notification
30
     * @return bool
31
     */
32
    public function isSatisfiedBy($notification): bool
33
    {
34
        $value = (new Dot($notification->metadata()))->get($this->expression->getField());
35
        if (is_null($value)) {
36
            return false;
37
        }
38
        $operator = $this->expression->getOperator();
39
        $expressionValue = $this->expression->getValue()->getValue();
40
        switch ($operator) {
41
            case Comparison::EQ:
42
                return $value === $expressionValue;
43
            case Comparison::NEQ:
44
                return $value !== $expressionValue;
45
            case Comparison::CONTAINS:
46
                return strpos($value, $expressionValue) !== false;
47
            case Comparison::GT:
48
                return $value > $expressionValue;
49
            case Comparison::GTE:
50
                return $value >= $expressionValue;
51
            case Comparison::LT:
52
                return $value < $expressionValue;
53
            case Comparison::LTE:
54
                return $value <= $expressionValue;
55
            case Comparison::IN:
56
                return in_array($value, $expressionValue, true);
57
            default:
58
                throw new RuntimeException(sprintf('Unknown operator: %s', $operator));
59
        }
60
    }
61
}
62