Passed
Push — trunk ( 36db4a...0fa3f8 )
by Christian
16:05 queued 14s
created

MediaPathPostUpdater   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A handle() 0 11 1
A getTotal() 0 3 1
A iterate() 0 11 2
A getDecorated() 0 3 1
A __construct() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Media\Infrastructure\Path;
4
5
use Doctrine\DBAL\ArrayParameterType;
6
use Doctrine\DBAL\Connection;
7
use Shopware\Core\Content\Media\Core\Application\MediaPathUpdater;
8
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
9
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer;
10
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
11
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\PostUpdateIndexer;
12
use Shopware\Core\Framework\Log\Package;
13
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
14
use Shopware\Core\Framework\Uuid\Uuid;
15
16
#[Package('core')]
17
class MediaPathPostUpdater extends PostUpdateIndexer
18
{
19
    /**
20
     * @internal
21
     */
22
    public function __construct(
23
        private readonly IteratorFactory $iteratorFactory,
24
        private readonly MediaPathUpdater $updater,
25
        private readonly Connection $connection
26
    ) {
27
    }
28
29
    public function getName(): string
30
    {
31
        return 'media.path.post_update';
32
    }
33
34
    public function iterate(?array $offset): ?EntityIndexingMessage
35
    {
36
        $iterator = $this->iteratorFactory->createIterator('media', $offset);
37
38
        $ids = $iterator->fetch();
39
40
        if (empty($ids)) {
41
            return null;
42
        }
43
44
        return new EntityIndexingMessage(array_values($ids), $iterator->getOffset());
45
    }
46
47
    public function handle(EntityIndexingMessage $message): void
48
    {
49
        $this->updater->updateMedia($message->getData());
50
51
        $thumbnails = $this->connection->fetchFirstColumn(
52
            'SELECT LOWER(HEX(id)) FROM media_thumbnail WHERE media_id IN (:ids)',
53
            ['ids' => Uuid::fromHexToBytesList($message->getData())],
54
            ['ids' => ArrayParameterType::STRING]
55
        );
56
57
        $this->updater->updateThumbnails($thumbnails);
58
    }
59
60
    public function getTotal(): int
61
    {
62
        return $this->iteratorFactory->createIterator('media', null)->fetchCount();
63
    }
64
65
    public function getDecorated(): EntityIndexer
66
    {
67
        throw new DecorationPatternException(self::class);
68
    }
69
}
70