Passed
Branch master (bbfb46)
by Jan
22:41 queued 14:44
created

Count42Rule   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 6
1
<?php declare(strict_types=1);
2
3
namespace SwagCustomRule\Core\Rule;
4
5
use Shopware\Core\Framework\Rule\Exception\UnsupportedOperatorException;
6
use Shopware\Core\Framework\Rule\Rule;
7
use Shopware\Core\Framework\Rule\RuleScope;
8
use Symfony\Component\Validator\Constraints\Choice;
9
use Symfony\Component\Validator\Constraints\NotBlank;
10
use Symfony\Component\Validator\Constraints\Type;
11
12
class Count42Rule extends Rule
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $operator;
18
19
    /**
20
     * @var int
21
     */
22
    protected $count;
23
24
    public function __construct()
25
    {
26
        $this->operator = self::OPERATOR_EQ;
27
    }
28
29
    public function match(RuleScope $scope): bool
30
    {
31
        switch ($this->operator) {
32
            case self::OPERATOR_EQ:
33
                return $this->count === 42;
34
            case self::OPERATOR_NEQ:
35
                return $this->count !== 42;
36
            default:
37
                throw new UnsupportedOperatorException($this->operator, __CLASS__);
38
        }
39
    }
40
41
    public function getConstraints(): array
42
    {
43
        return [
44
            'operator' => [new Choice([self::OPERATOR_EQ, self::OPERATOR_NEQ])],
45
            'count' => [new NotBlank(), new Type('int')],
46
        ];
47
    }
48
49
    public function getName(): string
50
    {
51
        return 'swagCount42';
52
    }
53
}
54