-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCryptographySubscriber.php
More file actions
127 lines (96 loc) · 3.4 KB
/
Copy pathCryptographySubscriber.php
File metadata and controls
127 lines (96 loc) · 3.4 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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Cryptography;
use Patchlevel\Hydrator\Event\PostExtract;
use Patchlevel\Hydrator\Event\PreExtract;
use Patchlevel\Hydrator\Event\PreHydrate;
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Stringable;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use function is_int;
use function is_string;
final class CryptographySubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly PayloadCryptographer $cryptography,
) {
}
public function preHydrate(PreHydrate $event): void
{
$parentSubjectId = $event->context['subjectId'] ?? null;
$currentSubjectId = $this->subjectIdFromData($event->data, $event->metadata) ?: $parentSubjectId;
$event->data = $this->cryptography->decrypt($event->metadata, $event->data, $currentSubjectId);
$event->context['subjectId'] = $currentSubjectId;
}
public function preExtract(PreExtract $event): void
{
$parentSubjectId = $event->context['subjectId'] ?? null;
$currentSubjectId = $this->subjectIdFromObject($event->object, $event->metadata) ?: $parentSubjectId;
$event->context['subjectId'] = $currentSubjectId;
}
public function postExtract(PostExtract $event): void
{
$event->data = $this->cryptography->encrypt(
$event->metadata,
$event->data,
$event->context['subjectId'] ?? null
);
}
/** @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>> */
public static function getSubscribedEvents(): array
{
return [
PreHydrate::class => 'preHydrate',
PreExtract::class => 'preExtract',
PostExtract::class => 'postExtract',
];
}
private function subjectIdFromObject(object $object, ClassMetadata $metadata): string|null
{
$subjectIdField = $metadata->dataSubjectIdField();
if ($subjectIdField === null) {
return null;
}
$property = $metadata->propertyForField($subjectIdField);
$value = $property->getValue($object);
if ($value === null) {
return null;
}
$normalizer = $property->normalizer();
if ($normalizer !== null) {
$value = $normalizer->normalize($value);
}
if ($value instanceof Stringable) {
$value = (string)$value;
}
if (is_int($value)) {
$value = (string)$value;
}
if (!is_string($value)) {
throw new UnsupportedSubjectId($metadata->className(), $subjectIdField, $value);
}
return $value;
}
/** @param array<string, mixed> $data */
private function subjectIdFromData(array $data, ClassMetadata $metadata): string|null
{
$subjectIdField = $metadata->dataSubjectIdField();
if ($subjectIdField === null) {
return null;
}
$value = $data[$subjectIdField] ?? null;
if ($value === null) {
return null;
}
if ($value instanceof Stringable) {
$value = (string)$value;
}
if (is_int($value)) {
$value = (string)$value;
}
if (!is_string($value)) {
throw new UnsupportedSubjectId($metadata->className(), $subjectIdField, $value);
}
return $value;
}
}