Passed
Push — master ( a5379f...f484f2 )
by Christian
12:11 queued 10s
created

RuleLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 21 6
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Cart;
4
5
use Shopware\Core\Content\Rule\RuleCollection;
6
use Shopware\Core\Framework\Context;
7
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\RepositoryIterator;
8
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
11
12
/**
13
 * @internal
14
 */
15
class RuleLoader
16
{
17
    /**
18
     * @var EntityRepositoryInterface
19
     */
20
    private $repository;
21
22
    public function __construct(EntityRepositoryInterface $repository)
23
    {
24
        $this->repository = $repository;
25
    }
26
27
    public function load(Context $context): RuleCollection
28
    {
29
        $criteria = new Criteria();
30
        $criteria->addSorting(new FieldSorting('priority', FieldSorting::DESCENDING));
31
        $criteria->setLimit(500);
32
        $criteria->setTitle('cart-rule-loader::load-rules');
33
34
        $repositoryIterator = new RepositoryIterator($this->repository, $context, $criteria);
35
        $rules = new RuleCollection();
36
        while (($result = $repositoryIterator->fetch()) !== null) {
37
            foreach ($result->getEntities() as $rule) {
38
                if (!$rule->isInvalid() && $rule->getPayload()) {
39
                    $rules->add($rule);
40
                }
41
            }
42
            if ($result->count() < 500) {
43
                break;
44
            }
45
        }
46
47
        return $rules;
48
    }
49
}
50