Passed
Push — trunk ( 97604d...be826f )
by Christian
12:05 queued 14s
created

NumberOfReviewsRule::getConstraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Customer\Rule;
4
5
use Shopware\Core\Checkout\CheckoutRuleScope;
6
use Shopware\Core\Framework\Log\Package;
7
use Shopware\Core\Framework\Rule\Rule;
8
use Shopware\Core\Framework\Rule\RuleComparison;
9
use Shopware\Core\Framework\Rule\RuleConfig;
10
use Shopware\Core\Framework\Rule\RuleConstraints;
11
use Shopware\Core\Framework\Rule\RuleScope;
12
13
#[Package('business-ops')]
14
class NumberOfReviewsRule extends Rule
15
{
16
    final public const RULE_NAME = 'numberOfReviews';
17
18
    protected string $operator;
19
20
    protected int $count;
21
22
    public function match(RuleScope $scope): bool
23
    {
24
        if (!$scope instanceof CheckoutRuleScope) {
25
            return false;
26
        }
27
        if (!$customer = $scope->getSalesChannelContext()->getCustomer()) {
28
            return RuleComparison::isNegativeOperator($this->operator);
29
        }
30
31
        $numberOfReviews = $customer->getReviewCount();
32
33
        return RuleComparison::numeric($numberOfReviews, $this->count, $this->operator);
34
    }
35
36
    public function getConstraints(): array
37
    {
38
        return [
39
            'count' => RuleConstraints::int(),
40
            'operator' => RuleConstraints::numericOperators(false),
41
        ];
42
    }
43
44
    public function getConfig(): RuleConfig
45
    {
46
        return (new RuleConfig())
47
            ->operatorSet(RuleConfig::OPERATOR_SET_NUMBER)
48
            ->intField('count');
49
    }
50
}
51