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

RuleLoader::load()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
nc 7
nop 1
dl 0
loc 21
rs 9.2222
c 1
b 0
f 0
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