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\MediaLocationBuilder; |
8
|
|
|
use Shopware\Core\Content\Media\Core\Event\MediaLocationEvent; |
9
|
|
|
use Shopware\Core\Content\Media\Core\Event\ThumbnailLocationEvent; |
10
|
|
|
use Shopware\Core\Content\Media\Core\Params\MediaLocationStruct; |
11
|
|
|
use Shopware\Core\Content\Media\Core\Params\ThumbnailLocationStruct; |
12
|
|
|
use Shopware\Core\Framework\Log\Package; |
13
|
|
|
use Shopware\Core\Framework\Uuid\Uuid; |
14
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @internal |
18
|
|
|
* |
19
|
|
|
* @codeCoverageIgnore (see \Shopware\Tests\Integration\Core\Content\Media\Infrastructure\Path\MediaLocationBuilderTest) |
20
|
|
|
*/ |
21
|
|
|
#[Package('content')] |
22
|
|
|
class SqlMediaLocationBuilder implements MediaLocationBuilder |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @internal |
26
|
|
|
*/ |
27
|
|
|
public function __construct( |
28
|
|
|
private readonly EventDispatcherInterface $dispatcher, |
29
|
|
|
private readonly Connection $connection |
30
|
|
|
) { |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function media(array $ids): array |
37
|
|
|
{ |
38
|
|
|
$ids = \array_unique($ids); |
39
|
|
|
if (empty($ids)) { |
40
|
|
|
return []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$data = $this->connection->fetchAllAssociativeIndexed( |
44
|
|
|
'SELECT LOWER(HEX(id)) as array_key, |
45
|
|
|
LOWER(HEX(id)) as id, |
46
|
|
|
file_extension, |
47
|
|
|
file_name, |
48
|
|
|
uploaded_at |
49
|
|
|
FROM media |
50
|
|
|
WHERE id IN (:ids)', |
51
|
|
|
['ids' => Uuid::fromHexToBytesList($ids)], |
52
|
|
|
['ids' => ArrayParameterType::STRING] |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$locations = []; |
56
|
|
|
|
57
|
|
|
foreach ($data as $key => $row) { |
58
|
|
|
$locations[(string) $key] = new MediaLocationStruct( |
59
|
|
|
$row['id'], |
60
|
|
|
$row['file_extension'], |
61
|
|
|
$row['file_name'], |
62
|
|
|
$row['uploaded_at'] ? new \DateTimeImmutable($row['uploaded_at']) : null |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->dispatcher->dispatch( |
67
|
|
|
new MediaLocationEvent($locations) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
return $locations; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function thumbnails(array $ids): array |
77
|
|
|
{ |
78
|
|
|
$ids = \array_unique($ids); |
79
|
|
|
if (empty($ids)) { |
80
|
|
|
return []; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$data = $this->connection->fetchAllAssociativeIndexed( |
84
|
|
|
'SELECT LOWER(HEX(media_thumbnail.id)) as array_key, |
85
|
|
|
LOWER(HEX(media_thumbnail.id)) as id, |
86
|
|
|
media.file_extension, |
87
|
|
|
media.file_name, |
88
|
|
|
LOWER(HEX(media.id)) as media_id, |
89
|
|
|
width, |
90
|
|
|
height, |
91
|
|
|
uploaded_at |
92
|
|
|
FROM media_thumbnail |
93
|
|
|
INNER JOIN media ON media.id = media_thumbnail.media_id |
94
|
|
|
WHERE media_thumbnail.id IN (:ids)', |
95
|
|
|
['ids' => Uuid::fromHexToBytesList($ids)], |
96
|
|
|
['ids' => ArrayParameterType::STRING] |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$locations = []; |
100
|
|
|
|
101
|
|
|
foreach ($data as $key => $row) { |
102
|
|
|
$media = new MediaLocationStruct( |
103
|
|
|
$row['media_id'], |
104
|
|
|
$row['file_extension'], |
105
|
|
|
$row['file_name'], |
106
|
|
|
$row['uploaded_at'] ? new \DateTimeImmutable($row['uploaded_at']) : null |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$locations[(string) $key] = new ThumbnailLocationStruct( |
110
|
|
|
$row['id'], |
111
|
|
|
(int) $row['width'], |
112
|
|
|
(int) $row['height'], |
113
|
|
|
$media |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->dispatcher->dispatch( |
118
|
|
|
new ThumbnailLocationEvent($locations) |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
return $locations; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|