Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ parameters:
path: tests/Unit/Fixture/DtoWithHooks.php

-
message: '#^Method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:454\:\:postHydrate\(\) is unused\.$#'
message: '#^Method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:477\:\:postHydrate\(\) is unused\.$#'
identifier: method.unused
count: 1
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php

-
message: '#^Method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:454\:\:preExtract\(\) is unused\.$#'
message: '#^Method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:477\:\:preExtract\(\) is unused\.$#'
identifier: method.unused
count: 1
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php
Expand All @@ -193,13 +193,13 @@ parameters:
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php

-
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:482\:\:postHydrate\(\) is unused\.$#'
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:505\:\:postHydrate\(\) is unused\.$#'
identifier: method.unused
count: 1
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php

-
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:482\:\:preExtract\(\) is unused\.$#'
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:505\:\:preExtract\(\) is unused\.$#'
identifier: method.unused
count: 1
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/Cryptography/BaseCryptographer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function encrypt(string $subjectId, mixed $value): array
$cipherKey = $this->cipherKeyStore->currentKeyFor($subjectId);
} catch (CipherKeyNotExists) {
$cipherKey = ($this->cipherKeyFactory)($subjectId);
$this->cipherKeyStore->store($cipherKey->id, $cipherKey);
$this->cipherKeyStore->store($cipherKey);
}

$parameter = $this->cipher->encrypt($cipherKey, $value);
Expand Down
4 changes: 2 additions & 2 deletions src/Extension/Cryptography/Store/CipherKeyStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ interface CipherKeyStore
public function currentKeyFor(string $subjectId): CipherKey;

/** @throws CipherKeyNotExists */
public function get(string $keyId): CipherKey;
public function get(string $id): CipherKey;

public function store(string $id, CipherKey $key): void;
public function store(CipherKey $key): void;

public function remove(string $id): void;

Expand Down
42 changes: 15 additions & 27 deletions src/Extension/Cryptography/Store/InMemoryCipherKeyStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class InMemoryCipherKeyStore implements CipherKeyStore
/** @var array<string, CipherKey> */
private array $indexById = [];

/** @var array<string, list<CipherKey>> */
/** @var array<string, array<string, CipherKey>> */
private array $indexBySubjectId = [];

public function currentKeyFor(string $subjectId): CipherKey
Expand All @@ -32,43 +32,31 @@ public function currentKeyFor(string $subjectId): CipherKey
return $this->indexBySubjectId[$subjectId][$lastKey];
}

public function get(string $keyId): CipherKey
public function get(string $id): CipherKey
{
return $this->indexById[$keyId] ?? throw CipherKeyNotExists::forKeyId($keyId);
return $this->indexById[$id] ?? throw CipherKeyNotExists::forKeyId($id);
}

public function store(string $id, CipherKey $key): void
public function store(CipherKey $key): void
{
$this->indexById[$id] = $key;
$this->remove($key->id);

if (!isset($this->indexBySubjectId[$key->subjectId])) {
$this->indexBySubjectId[$key->subjectId] = [];
}

$this->indexBySubjectId[$key->subjectId][] = $key;
$this->indexById[$key->id] = $key;
$this->indexBySubjectId[$key->subjectId][$key->id] = $key;
}

public function remove(string $id): void
{
unset($this->indexById[$id]);

foreach ($this->indexBySubjectId as $subjectId => $keys) {
$filtered = [];

foreach ($keys as $key) {
if ($key->id === $id) {
continue;
}
$key = $this->indexById[$id] ?? null;

$filtered[] = $key;
}

if ($filtered === []) {
unset($this->indexBySubjectId[$subjectId]);
} else {
$this->indexBySubjectId[$subjectId] = $filtered;
}
if (!$key) {
return;
}

unset(
$this->indexBySubjectId[$key->subjectId][$id],
$this->indexById[$id],
);
}

public function clear(): void
Expand Down
11 changes: 6 additions & 5 deletions src/Metadata/AttributeMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ private function getPropertyMetadataList(ReflectionClass $reflectionClass): arra
);
}

$type = $this->typeResolver->resolve($reflectionProperty);

$properties[$fieldName] = new PropertyMetadata(
$reflectionProperty,
$fieldName,
$this->getNormalizer($reflectionProperty),
$this->getNormalizer($reflectionProperty, $type),
...$this->getPersonalData($reflectionProperty),
type: $type,
);
}

Expand Down Expand Up @@ -352,18 +355,16 @@ private function validate(ClassMetadata $metadata): void
}
}

private function getNormalizer(ReflectionProperty $reflectionProperty): Normalizer|null
private function getNormalizer(ReflectionProperty $reflectionProperty, Type $type): Normalizer|null
{
$normalizer = $this->findNormalizerOnProperty($reflectionProperty);
$type = null;

if (!$normalizer) {
$type = $this->typeResolver->resolve($reflectionProperty);
$normalizer = $this->inferNormalizerByType($type);
}

if ($normalizer instanceof TypeAwareNormalizer) {
$normalizer->handleType($type ?? $this->typeResolver->resolve($reflectionProperty));
$normalizer->handleType($type);
}

if ($normalizer instanceof ReflectionTypeAwareNormalizer) {
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use Patchlevel\Hydrator\Normalizer\Normalizer;
use ReflectionProperty;
use Symfony\Component\TypeInfo\Type;

use function str_starts_with;

Expand Down Expand Up @@ -40,6 +41,7 @@ public function __construct(
public readonly mixed $personalDataFallback = null,
public readonly mixed $personalDataFallbackCallable = null,
public array $extras = [],
public readonly Type|null $type = null,
) {
$this->propertyName = $reflection->getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testEncryptWithoutKey(): void
$cipherKeyStore
->expects($this->once())
->method('store')
->with('key-456', $cipherKey);
->with($cipherKey);

$cipher = $this->createMock(Cipher::class);
$cipher->expects($this->once())->method('encrypt')->with($cipherKey, 'info@patchlevel.de')
Expand Down
Loading
Loading