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

FlowIndexer::refreshPlugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Flow\Indexing;
4
5
use Shopware\Core\Content\Flow\Events\FlowIndexerEvent;
6
use Shopware\Core\Content\Flow\FlowDefinition;
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 FlowIndexer extends EntityIndexer
21
{
22
    public const NAME = 'flow.indexer';
23
24
    /**
25
     * @internal
26
     */
27
    public function __construct(
28
        private readonly IteratorFactory $iteratorFactory,
29
        private readonly EntityRepository $repository,
30
        private readonly FlowPayloadUpdater $payloadUpdater,
31
        private readonly EventDispatcherInterface $eventDispatcher
32
    ) {
33
    }
34
35
    public function getName(): string
36
    {
37
        return self::NAME;
38
    }
39
40
    public function iterate(?array $offset): ?EntityIndexingMessage
41
    {
42
        $iterator = $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
43
44
        $ids = $iterator->fetch();
45
46
        if (empty($ids)) {
47
            return null;
48
        }
49
50
        return new FlowIndexingMessage(array_values($ids), $iterator->getOffset());
51
    }
52
53
    public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
54
    {
55
        $updates = $event->getPrimaryKeys(FlowDefinition::ENTITY_NAME);
56
57
        if (empty($updates)) {
58
            return null;
59
        }
60
61
        $this->handle(new FlowIndexingMessage(array_values($updates), null, $event->getContext()));
62
63
        return null;
64
    }
65
66
    public function handle(EntityIndexingMessage $message): void
67
    {
68
        $ids = array_unique(array_filter($message->getData()));
69
70
        if (empty($ids)) {
71
            return;
72
        }
73
74
        $this->payloadUpdater->update($ids);
75
76
        $this->eventDispatcher->dispatch(new FlowIndexerEvent($ids, $message->getContext()));
77
    }
78
79
    public function getTotal(): int
80
    {
81
        return $this->iteratorFactory->createIterator($this->repository->getDefinition())->fetchCount();
82
    }
83
84
    public function getDecorated(): EntityIndexer
85
    {
86
        throw new DecorationPatternException(static::class);
87
    }
88
}
89