Passed
Push — trunk ( 610829...353855 )
by Christian
13:15 queued 12s
created

CustomerCreatedByAdminRule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getConstraints() 0 4 1
A getConfig() 0 4 1
A match() 0 11 3
A getName() 0 3 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 CustomerCreatedByAdminRule extends Rule
15
{
16
    /**
17
     * @internal
18
     */
19
    public function __construct(private bool $shouldCustomerBeCreatedByAdmin = true)
20
    {
21
        parent::__construct();
22
    }
23
24
    public function getName(): string
25
    {
26
        return 'customerCreatedByAdmin';
27
    }
28
29
    public function match(RuleScope $scope): bool
30
    {
31
        if (!$scope instanceof CheckoutRuleScope) {
32
            return false;
33
        }
34
35
        if (!$customer = $scope->getSalesChannelContext()->getCustomer()) {
36
            return false;
37
        }
38
39
        return $this->shouldCustomerBeCreatedByAdmin === (bool) $customer->getCreatedById();
40
    }
41
42
    public function getConstraints(): array
43
    {
44
        return [
45
            'shouldCustomerBeCreatedByAdmin' => RuleConstraints::bool(true),
46
        ];
47
    }
48
49
    public function getConfig(): RuleConfig
50
    {
51
        return (new RuleConfig())
52
            ->booleanField('shouldCustomerBeCreatedByAdmin');
53
    }
54
}
55