Skip to content

Commit

Permalink
improved code according to PHPStan reports
Browse files Browse the repository at this point in the history
  • Loading branch information
konradoboza committed Jun 19, 2024
1 parent 3884487 commit f654f6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
25 changes: 0 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2305,31 +2305,6 @@ parameters:
count: 1
path: src/lib/Schema/Worker.php

-
message: "#^Access to an undefined property GraphQL\\\\Language\\\\AST\\\\BooleanValueNode\\|GraphQL\\\\Language\\\\AST\\\\EnumValueNode\\|GraphQL\\\\Language\\\\AST\\\\FloatValueNode\\|GraphQL\\\\Language\\\\AST\\\\IntValueNode\\|GraphQL\\\\Language\\\\AST\\\\ListValueNode\\|GraphQL\\\\Language\\\\AST\\\\NullValueNode\\|GraphQL\\\\Language\\\\AST\\\\ObjectValueNode\\|GraphQL\\\\Language\\\\AST\\\\StringValueNode\\|GraphQL\\\\Language\\\\AST\\\\VariableNode\\:\\:\\$value\\.$#"
count: 1
path: src/lib/Security/JWTAuthenticator.php

-
message: "#^Parameter \\#1 \\$user of method Lexik\\\\Bundle\\\\JWTAuthenticationBundle\\\\Services\\\\JWTTokenManagerInterface\\:\\:create\\(\\) expects Symfony\\\\Component\\\\Security\\\\Core\\\\User\\\\UserInterface, Symfony\\\\Component\\\\Security\\\\Core\\\\User\\\\UserInterface\\|null given\\.$#"
count: 1
path: src/lib/Security/JWTAuthenticator.php

-
message: "#^Cannot call method getContent\\(\\) on Symfony\\\\Component\\\\HttpFoundation\\\\Response\\|null\\.$#"
count: 1
path: src/lib/Security/JWTTokenMutationFormatEventSubscriber.php

-
message: "#^Cannot call method setContent\\(\\) on Symfony\\\\Component\\\\HttpFoundation\\\\Response\\|null\\.$#"
count: 1
path: src/lib/Security/JWTTokenMutationFormatEventSubscriber.php

-
message: "#^Method Ibexa\\\\GraphQL\\\\Security\\\\JWTTokenMutationFormatEventSubscriber\\:\\:formatMutationResponseData\\(\\) should return string but returns string\\|false\\.$#"
count: 1
path: src/lib/Security/JWTTokenMutationFormatEventSubscriber.php

-
message: "#^Cannot cast object to string\\.$#"
count: 1
Expand Down
12 changes: 10 additions & 2 deletions src/lib/Security/JWTAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ public function authenticate(Request $request): Passport
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
$user = $token->getUser();
if ($user === null) {
throw new AuthenticationException('No authenticated user found.', 401);
}

return new Response(
json_encode(
[
'token' => $this->tokenManager->create($token->getUser()),
'token' => $this->tokenManager->create($user),
'message' => null,
],
JSON_THROW_ON_ERROR
Expand Down Expand Up @@ -131,7 +136,10 @@ private function extractCredentials(string $graphqlQuery): array
$parsed,
[
NodeKind::ARGUMENT => static function (ArgumentNode $node) use (&$credentials): void {
$credentials[$node->name->value] = (string)$node->value->value;
/** @var \GraphQL\Language\AST\StringValueNode $nodeValue */
$nodeValue = $node->value;

$credentials[$node->name->value] = $nodeValue->value;
},
]
);
Expand Down
8 changes: 7 additions & 1 deletion src/lib/Security/JWTTokenMutationFormatEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static function getSubscribedEvents(): array
public function onAuthorizationFinishes(LoginSuccessEvent|LoginFailureEvent $event): void
{
$response = $event->getResponse();
if ($response === null) {
return;
}

$response->setContent(
$this->formatMutationResponseData($response->getContent())
);
Expand All @@ -41,10 +45,12 @@ public function onAuthorizationFinishes(LoginSuccessEvent|LoginFailureEvent $eve
*/
private function formatMutationResponseData(mixed $data): string
{
return json_encode([
$formatted = json_encode([
'data' => [
'CreateToken' => json_decode($data, true, 512, JSON_THROW_ON_ERROR),
],
]);

return $formatted === false ? '' : $formatted;
}
}

0 comments on commit f654f6f

Please sign in to comment.