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

RuleCollection::equals()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 13
rs 10
c 1
b 0
f 0
ccs 0
cts 0
cp 0
crap 20
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