-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCustomMetadataAdapter.php
More file actions
179 lines (152 loc) · 5.75 KB
/
CustomMetadataAdapter.php
File metadata and controls
179 lines (152 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Patcher\Adapter;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Repository\MetadataRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Service\DataResolverServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Service\MetadataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Patcher\Adapter\PatchAdapterInterface;
use Pimcore\Bundle\StudioBackendBundle\Patcher\Service\Loader\TaggedIteratorAdapter;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use Pimcore\Model\Asset;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\UserInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use function array_key_exists;
use function in_array;
use function sprintf;
/**
* @internal
*/
#[AutoconfigureTag(TaggedIteratorAdapter::ADAPTER_TAG)]
final class CustomMetadataAdapter implements PatchAdapterInterface
{
private const string INDEX_KEY = 'metadata';
private const array PATCHABLE_KEYS = [
'language',
'data',
];
public function __construct(
private readonly DataResolverServiceInterface $dataResolverService,
private readonly MetadataRepositoryInterface $metadataRepository
) {
}
/**
* @throws InvalidArgumentException
*/
public function patch(ElementInterface $element, array $data, UserInterface $user): void
{
if (!$element instanceof Asset || !isset($data[self::INDEX_KEY])) {
return;
}
$metadataForPatch = $data[self::INDEX_KEY];
$currentMetadata = $element->getMetadata(null, null, false, true);
$patchedMetadata = [];
foreach ($currentMetadata as $metadata) {
$index = $this->findIndexOfMatch($metadata, $metadataForPatch);
if ($index === false) {
$patchedMetadata[] = $metadata;
continue;
}
foreach (self::PATCHABLE_KEYS as $patchKey) {
if (array_key_exists($patchKey, $metadataForPatch[$index])) {
$metadata[$patchKey] = $this->getExistingEntryValue(
$metadataForPatch[$index],
$metadata,
$patchKey,
$user
);
}
}
$patchedMetadata[] = $metadata;
// unset them, everything that is still in there, needs to be added
unset($metadataForPatch[$index]);
}
$patchedMetadata = [
...$patchedMetadata,
...array_map(
fn (array $metaData) => $this->processNewMetadataEntry($metaData, $user),
$metadataForPatch
),
];
if (!empty($patchedMetadata)) {
$element->setMetadata($patchedMetadata);
}
}
public function getIndexKey(): string
{
return self::INDEX_KEY;
}
public function supportedElementTypes(): array
{
return [
ElementTypes::TYPE_ASSET,
];
}
private function getExistingEntryValue(
array $metadata,
array $existingMetadata,
string $key,
UserInterface $user
): mixed {
if ($key !== 'data') {
return $metadata[$key];
}
return $this->dataResolverService->denormalizeData($metadata, $user, $existingMetadata, true);
}
/**
* @throws InvalidArgumentException
*/
private function processNewMetadataEntry(array $metadata, UserInterface $user): array
{
if (!isset($metadata['name'])) {
throw new InvalidArgumentException('Metadata name is required');
}
if (in_array($metadata['name'], MetadataServiceInterface::DEFAULT_METADATA, true)) {
return $this->addDefaultMetadata($metadata);
}
$predefined = $this->metadataRepository->getPredefinedMetadataByName($metadata['name']);
if (!$predefined) {
throw new InvalidArgumentException(sprintf('Predefined metadata %s not found', $metadata['name']));
}
return [
'name' => $predefined->getName(),
'language' => $metadata['language'] ?? '',
'type' => $predefined->getType(),
'data' => $metadata['data'] ?
$this->dataResolverService->denormalizeData($metadata, $user, [], true) :
null,
];
}
private function findIndexOfMatch(array $metadata, array $patchMetadata): int|bool
{
// Try to find a match. array_filter keeps the original index which we can get with array_keys
$match = array_keys(array_filter($patchMetadata, static function ($patch) use ($metadata) {
$language = $patch['language'] ?? '';
return $patch['name'] === $metadata['name'] && $language === $metadata['language'];
}));
// Return the key of the first match
return !empty($match) ? current($match) : false;
}
private function addDefaultMetadata(array $metadata): array
{
return [
'name' => $metadata['name'],
'language' => $metadata['language'] ?? '',
'type' => 'input',
'data' => $metadata['data'] ?? null,
];
}
}