Passed
Push — master ( 58f8c5...b76322 )
by Christian
13:22 queued 12s
created

RuleCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 41
rs 10
c 1
b 0
f 0
ccs 5
cts 6
cp 0.8333
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A filterMatchingRules() 0 5 1
A sortByPriority() 0 4 1
A getExpectedClass() 0 3 1
A equals() 0 13 4
A getApiAlias() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Rule;
4
5
use Shopware\Core\Checkout\Cart\Cart;
6
use Shopware\Core\Checkout\Cart\Rule\CartRuleScope;
7
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
8
use Shopware\Core\System\SalesChannel\SalesChannelContext;
9
10
/**
11
 * @method void            add(RuleEntity $entity)
12 61
 * @method void            set(string $key, RuleEntity $entity)
13
 * @method RuleEntity[]    getIterator()
14 61
 * @method RuleEntity[]    getElements()
15
 * @method RuleEntity|null get(string $key)
16
 * @method RuleEntity|null first()
17 61
 * @method RuleEntity|null last()
18
 */
19
class RuleCollection extends EntityCollection
20
{
21 61
    public function filterMatchingRules(Cart $cart, SalesChannelContext $context)
22
    {
23
        return $this->filter(
24
            function (RuleEntity $rule) use ($cart, $context) {
25 61
                return $rule->getPayload()->match(new CartRuleScope($cart, $context));
26 61
            }
27
        );
28 21
    }
29
30 21
    public function sortByPriority(): void
31
    {
32
        $this->sort(function (RuleEntity $a, RuleEntity $b) {
33
            return $b->getPriority() <=> $a->getPriority();
34
        });
35
    }
36
37
    public function equals(RuleCollection $rules): bool
38
    {
39
        if ($this->count() !== $rules->count()) {
40
            return false;
41
        }
42
43
        foreach ($this->elements as $element) {
44
            if (!$rules->has($element->getId())) {
45
                return false;
46
            }
47
        }
48
49
        return true;
50
    }
51
52
    public function getApiAlias(): string
53
    {
54
        return 'rule_collection';
55
    }
56
57
    protected function getExpectedClass(): string
58
    {
59
        return RuleEntity::class;
60
    }
61
}
62