Skip to content

Commit

Permalink
Refactor body preparation into a separate method
Browse files Browse the repository at this point in the history
Extract body preparation logic from main code block into a new `prepareBody` method. This enhances code readability and maintainability by isolating the responsibility and making the `EmbedInterceptor` class easier to extend and test.
  • Loading branch information
koriym committed Oct 22, 2024
1 parent ea76d55 commit 48b846c
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/EmbedInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,16 @@ private function embedResource(array $embeds, ResourceObject $ro, array $query):
$uri = uri_template($templateUri, $query);
/** @var Request $request */ // phpcs:ignore SlevomatCodingStandard.PHP.RequireExplicitAssertion.RequiredExplicitAssertion
$request = $this->resource->get->uri($uri);
if ($ro->body === null) {
$ro->body = [];
}

if (! is_array($ro->body)) {
throw new LinkException($embed->rel); // @codeCoverageIgnore
}
$this->prepareBody($ro, $embed);

if ($embed->rel === self::SELF_LINK) {
$this->linkSelf($request, $ro);

continue;
}

assert(is_array($ro->body));

$ro->body[$embed->rel] = clone $request;
} catch (BadRequestException $e) {
// wrap ResourceNotFound or Uri exception
Expand All @@ -97,6 +93,17 @@ private function getFullUri(string $uri, ResourceObject $ro): string
return $uri;
}

public function prepareBody(ResourceObject $ro, Embed $embed): void
{
if ($ro->body === null) {
$ro->body = [];
}

if (! is_array($ro->body)) {
throw new LinkException($embed->rel); // @codeCoverageIgnore
}
}

/** @return array<string, mixed> */
private function getArgsByInvocation(MethodInvocation $invocation): array
{
Expand Down

0 comments on commit 48b846c

Please sign in to comment.