Passed
Push — trunk ( ad06ea...8f56e6 )
by Christian
23:45 queued 17s
created

RuleIndexer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 78
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDecorated() 0 3 1
A update() 0 11 2
A getName() 0 3 1
A getTotal() 0 3 1
A __construct() 0 7 1
A iterate() 0 11 2
A handle() 0 18 4
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Rule\DataAbstractionLayer;
4
5
use Shopware\Core\Content\Rule\Event\RuleIndexerEvent;
6
use Shopware\Core\Content\Rule\RuleDefinition;
7
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
8
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
9
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
10
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer;
11
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
12
use Shopware\Core\Framework\Log\Package;
13
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
14
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
15
16
/**
17
 * @final
18
 */
19
#[Package('business-ops')]
20
class RuleIndexer extends EntityIndexer
21
{
22
    final public const PAYLOAD_UPDATER = 'rule.payload';
23
24
    final public const AREA_UPDATER = 'rule.area';
25
26
    /**
27
     * @internal
28
     */
29
    public function __construct(
30
        private readonly IteratorFactory $iteratorFactory,
31
        private readonly EntityRepository $repository,
32
        private readonly RulePayloadUpdater $payloadUpdater,
33
        private readonly RuleAreaUpdater $areaUpdater,
34
        private readonly EventDispatcherInterface $eventDispatcher
35
    ) {
36
    }
37
38
    public function getName(): string
39
    {
40
        return 'rule.indexer';
41
    }
42
43
    public function iterate(?array $offset): ?EntityIndexingMessage
44
    {
45
        $iterator = $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
46
47
        $ids = $iterator->fetch();
48
49
        if (empty($ids)) {
50
            return null;
51
        }
52
53
        return new RuleIndexingMessage(array_values($ids), $iterator->getOffset());
54
    }
55
56
    public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
57
    {
58
        $updates = $event->getPrimaryKeys(RuleDefinition::ENTITY_NAME);
59
60
        if (empty($updates)) {
61
            return null;
62
        }
63
64
        $this->handle(new RuleIndexingMessage(array_values($updates), null, $event->getContext()));
65
66
        return null;
67
    }
68
69
    public function handle(EntityIndexingMessage $message): void
70
    {
71
        $ids = $message->getData();
72
73
        $ids = array_unique(array_filter($ids));
74
        if (empty($ids)) {
75
            return;
76
        }
77
78
        if ($message->allow(self::PAYLOAD_UPDATER)) {
79
            $this->payloadUpdater->update($ids);
80
        }
81
82
        if ($message->allow(self::AREA_UPDATER)) {
83
            $this->areaUpdater->update($ids);
84
        }
85
86
        $this->eventDispatcher->dispatch(new RuleIndexerEvent($ids, $message->getContext(), $message->getSkip()));
87
    }
88
89
    public function getTotal(): int
90
    {
91
        return $this->iteratorFactory->createIterator($this->repository->getDefinition())->fetchCount();
92
    }
93
94
    public function getDecorated(): EntityIndexer
95
    {
96
        throw new DecorationPatternException(static::class);
97
    }
98
}
99