-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCustomMetadataAdapter.php
More file actions
84 lines (70 loc) · 2.7 KB
/
CustomMetadataAdapter.php
File metadata and controls
84 lines (70 loc) · 2.7 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
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Updater\Adapter;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Event\PreSet\CustomMetadataEvent;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Service\DataResolverServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Updater\Adapter\UpdateAdapterInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use Pimcore\Model\Asset;
use Pimcore\Model\Element\ElementInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use function array_key_exists;
/**
* @internal
*/
#[AutoconfigureTag('pimcore.studio_backend.update_adapter')]
final readonly class CustomMetadataAdapter implements UpdateAdapterInterface
{
private const string INDEX_KEY = 'metadata';
public function __construct(
private DataResolverServiceInterface $dataResolverService,
private EventDispatcherInterface $eventDispatcher,
private SecurityServiceInterface $securityService,
) {
}
public function update(ElementInterface $element, array $data): void
{
if (!$element instanceof Asset || !array_key_exists($this->getIndexKey(), $data)) {
return;
}
$metadataEvent = new CustomMetadataEvent(
$element->getId(),
$this->denormalizeMetadata($data[$this->getIndexKey()])
);
$this->eventDispatcher->dispatch($metadataEvent, CustomMetadataEvent::EVENT_NAME);
$element->setMetadata($this->dataResolverService->prepareData($metadataEvent->getCustomMetadata()));
}
public function getIndexKey(): string
{
return self::INDEX_KEY;
}
public function supportedElementTypes(): array
{
return [
ElementTypes::TYPE_ASSET,
];
}
private function denormalizeMetadata(array $customMetadata): array
{
$user = $this->securityService->getCurrentUser();
return array_map(function ($metadataItem) use ($user) {
$metadataItem['data'] = $this->dataResolverService->denormalizeData(
$metadataItem,
$user,
$metadataItem['type']
);
return $metadataItem;
}, $customMetadata);
}
}