Passed
Push — trunk ( e12360...bd02e6 )
by Christian
17:36 queued 05:07
created

CustomerLoggedInRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 4 1
A getConstraints() 0 4 1
A match() 0 11 2
A __construct() 0 4 1
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\Rule\Rule;
7
use Shopware\Core\Framework\Rule\RuleConfig;
8
use Shopware\Core\Framework\Rule\RuleConstraints;
9
use Shopware\Core\Framework\Rule\RuleScope;
10
11
/**
12
 * @package business-ops
13
 */
14
class CustomerLoggedInRule extends Rule
15
{
16
    public const RULE_NAME = 'customerLoggedIn';
17
18
    protected bool $isLoggedIn;
19
20
    /**
21
     * @internal
22
     */
23
    public function __construct(bool $isLoggedIn = false)
24
    {
25
        parent::__construct();
26
        $this->isLoggedIn = $isLoggedIn;
27
    }
28
29
    public function match(RuleScope $scope): bool
30
    {
31
        if (!$scope instanceof CheckoutRuleScope) {
32
            return false;
33
        }
34
35
        $customer = $scope->getSalesChannelContext()->getCustomer();
36
37
        $loggedIn = $customer !== null;
38
39
        return $this->isLoggedIn === $loggedIn;
40
    }
41
42
    public function getConstraints(): array
43
    {
44
        return [
45
            'isLoggedIn' => RuleConstraints::bool(true),
46
        ];
47
    }
48
49
    public function getConfig(): RuleConfig
50
    {
51
        return (new RuleConfig())
52
            ->booleanField('isLoggedIn');
53
    }
54
}
55