Skip to content

Commit

Permalink
Fix SA
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBadura committed Feb 27, 2024
1 parent 83e0d3e commit 8bd3b44
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ parameters:
count: 1
path: src/Console/DoctrineHelper.php

-
message: "#^Property Patchlevel\\\\EventSourcing\\\\EventBus\\\\Message\\<T of object\\>\\:\\:\\$headers \\(array\\{aggregateName\\?\\: string, aggregateId\\?\\: string, playhead\\?\\: int\\<1, max\\>, recordedOn\\?\\: DateTimeImmutable, newStreamStart\\?\\: bool, archived\\?\\: bool\\}\\) does not accept non\\-empty\\-array\\<string, mixed\\>\\.$#"
count: 1
path: src/EventBus/Message.php

-
message: "#^Parameter \\#3 \\$errorContext of class Patchlevel\\\\EventSourcing\\\\Projection\\\\Projection\\\\ProjectionError constructor expects array\\<int, array\\{class\\: class\\-string, message\\: string, code\\: int\\|string, file\\: string, line\\: int, trace\\: array\\<int, array\\{file\\?\\: string, line\\?\\: int, function\\?\\: string, class\\?\\: string, type\\?\\: string, args\\?\\: array\\}\\>\\}\\>\\|null, mixed given\\.$#"
count: 1
Expand All @@ -30,11 +25,6 @@ parameters:
count: 1
path: src/Store/ArrayStream.php

-
message: "#^Parameter \\#1 \\$headers of method Patchlevel\\\\EventSourcing\\\\EventBus\\\\Message\\<object\\>\\:\\:withHeaders\\(\\) expects array\\{aggregateName\\?\\: string, aggregateId\\?\\: string, playhead\\?\\: int\\<1, max\\>, recordedOn\\?\\: DateTimeImmutable, newStreamStart\\?\\: bool, archived\\?\\: bool\\}, array\\<string, mixed\\> given\\.$#"
count: 1
path: src/Store/DoctrineDbalStoreStream.php

-
message: "#^Ternary operator condition is always true\\.$#"
count: 1
Expand Down
19 changes: 15 additions & 4 deletions src/EventBus/Serializer/EventSerializerMessageSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
use function serialize;
use function unserialize;

/**
* @psalm-type EncodedData array{serializedEvent: array{name: string, payload: string}, headers: array{name: string, payload: string}}
*/
final class EventSerializerMessageSerializer implements MessageSerializer
{
public function __construct(
private readonly EventSerializer $eventSerializer,
private readonly HeadersSerializer $headersSerializer,
private readonly Hydrator $hydrator,
private readonly Encoder $encoder,
) {
}
Expand All @@ -44,16 +46,25 @@ public function deserialize(string $content): Message
$messageData = $this->encoder->decode($content);

if (
!is_array($messageData)
|| !isset($messageData['serializedEvent'], $messageData['headers'])
!isset($messageData['serializedEvent'], $messageData['headers'])
|| !is_array($messageData['serializedEvent'])
|| !is_array($messageData['headers'])
|| !isset($messageData['serializedEvent']['name'], $messageData['serializedEvent']['payload'])
|| !is_string($messageData['serializedEvent']['name'])
|| !is_string($messageData['serializedEvent']['payload'])
|| !isset($messageData['headers']['name'], $messageData['headers']['payload'])
|| !is_string($messageData['headers']['name'])
|| !is_string($messageData['headers']['payload'])
) {
throw DeserializeFailed::invalidData($messageData);
}

$event = $this->eventSerializer->deserialize(new SerializedEvent($messageData['serializedEvent']['name'], $messageData['serializedEvent']['payload']));
$headers = $this->headersSerializer->deserialize(array_map(fn (array $headerData) => new SerializedHeader($headerData['name'], $headerData['payload']), $messageData['headers']));
$headers = $this->headersSerializer->deserialize(array_map(
/** @param array{name: string, payload: string} $headerData */
fn (array $headerData) => new SerializedHeader($headerData['name'], $headerData['payload']),
$messageData['headers']
));

return Message::createWithHeaders($event, $headers);
}
Expand Down
4 changes: 2 additions & 2 deletions src/EventBus/Serializer/HeadersSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
interface HeadersSerializer
{
/**
* @param array<string, Header> $headers
* @param list<Header> $headers
* @return array<SerializedHeader>
*/
public function serialize(array $headers): array;

/**
* @param array<SerializedHeader> $serializedHeaders
* @return array<string, Header>
* @return list<Header>
*/
public function deserialize(array $serializedHeaders): array;
}
1 change: 1 addition & 0 deletions src/Store/DoctrineDbalStoreStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private function buildGenerator(
$event = $eventSerializer->deserialize(new SerializedEvent($data['event'], $data['payload']));

$customHeaders = $headersSerializer->deserialize(array_map(
/** @param array{name: string, payload: string} $customHeader */
fn (array $customHeader) => new SerializedHeader($customHeader['name'], $customHeader['payload']),
DoctrineHelper::normalizeCustomHeaders($data['custom_headers'], $platform)
));
Expand Down
4 changes: 2 additions & 2 deletions src/Store/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function normalizePlayhead(string|int $playhead, AbstractPlatform
return $normalizedPlayhead;
}

/** @return array<array> */
/** @return array<array{name: string, payload: string}> */
public static function normalizeCustomHeaders(string $customHeaders, AbstractPlatform $platform): array
{
$normalizedCustomHeaders = Type::getType(Types::JSON)->convertToPHPValue($customHeaders, $platform);
Expand All @@ -49,7 +49,7 @@ public static function normalizeCustomHeaders(string $customHeaders, AbstractPla
throw new InvalidType('custom_headers', 'array');
}

/** @var array<array>> $normalizedCustomHeaders */
/** @var array<array{name: string, payload: string}> $normalizedCustomHeaders */
return $normalizedCustomHeaders;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use Patchlevel\EventSourcing\EventBus\Serializer\EventSerializerMessageSerializer;
use Patchlevel\EventSourcing\EventBus\Serializer\HeadersSerializer;
use Patchlevel\EventSourcing\EventBus\Serializer\SerializedHeader;
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventRegistryFactory;
use Patchlevel\EventSourcing\Metadata\Message\AttributeMessageHeaderRegistryFactory;
use Patchlevel\EventSourcing\Serializer\Encoder\JsonEncoder;
use Patchlevel\EventSourcing\Serializer\EventSerializer;
use Patchlevel\EventSourcing\Serializer\SerializedEvent;
Expand Down Expand Up @@ -54,7 +52,6 @@ public function testSerialize(): void
$eventSerializer->reveal(),
$headersSerializer->reveal(),
new MetadataHydrator(),
new JsonEncoder(),
);

$content = $serializer->serialize($message);
Expand Down Expand Up @@ -86,7 +83,6 @@ public function testDeserialize(): void
$eventSerializer->reveal(),
$headersSerializer->reveal(),
new MetadataHydrator(),
new JsonEncoder(),
);

$deserializedMessage = $serializer->deserialize('{"serializedEvent":{"name":"profile_visited","payload":"{id: foo}"},"headers":[{"name":"aggregate","payload": "{aggregateName:profile,aggregateId:1,playhead:1,recordedOn:2020-01-01T20:00:00+01:00}"}, {"name": "archived", "payload":"{archived:false}"}]}');
Expand Down Expand Up @@ -141,7 +137,6 @@ public function testEquals(): void
$eventSerializer->reveal(),
$headersSerializer->reveal(),
new MetadataHydrator(),
new JsonEncoder(),
);

$content = $serializer->serialize($message);
Expand Down

0 comments on commit 8bd3b44

Please sign in to comment.