From 82266ae1575a2f1395b1a67286d4212996eda8d0 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Sun, 16 Jun 2024 17:03:54 +0300 Subject: [PATCH] requested changes --- src/App/src/Message.php | 5 ++-- .../src/Middleware/DeprecationMiddleware.php | 29 +++++++++++++------ .../App/Attribute/MethodDeprecationTest.php | 4 +-- .../Middleware/DeprecationMiddlewareTest.php | 2 +- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/App/src/Message.php b/src/App/src/Message.php index 5650beb..d89d953 100644 --- a/src/App/src/Message.php +++ b/src/App/src/Message.php @@ -18,12 +18,13 @@ class Message public const INVALID_CLIENT_ID = 'Invalid client_id.'; public const INVALID_CONFIG = 'Invalid configuration value: \'%s\''; public const INVALID_VALUE = 'The value specified for \'%s\' is invalid.'; + public const MAIL_NOT_SENT_TO = 'Could not send mail to \'%s\'.'; + public const MAIL_SENT_RECOVER_IDENTITY = 'If the provided email identifies an account in our system, ' . 'you will receive an email with your account\'s identity.'; public const MAIL_SENT_RESET_PASSWORD = 'If the provided email identifies an account in our system, ' . 'you will receive an email with further instructions on resetting your account\'s password.'; public const MAIL_SENT_USER_ACTIVATION = 'User activation mail has been successfully sent to \'%s\''; - public const MAIL_NOT_SENT_TO = 'Could not send mail to \'%s\'.'; public const MISSING_CONFIG = 'Missing configuration value: \'%s\'.'; public const RESET_PASSWORD_EXPIRED = 'Password reset request for hash: \'%s\' is invalid (expired).'; public const RESET_PASSWORD_NOT_FOUND = 'Could not find password reset request identified by hash: \'%s\''; @@ -31,6 +32,7 @@ class Message public const RESET_PASSWORD_USED = 'Password reset request for hash: \'%s\' is invalid (used).'; public const RESET_PASSWORD_VALID = 'Password reset request for hash: \'%s\' is valid.'; public const RESOURCE_NOT_ALLOWED = 'You are not allowed to access this resource.'; + public const RESTRICTION_DEPRECATION = 'Cannot use both `%s` and `%s` attributes on the same object.'; public const RESTRICTION_IMAGE = 'File must be an image (jpg, png).'; public const RESTRICTION_ROLES = 'User accounts must have at least one role.'; public const ROLE_NOT_FOUND = 'Role not found.'; @@ -44,5 +46,4 @@ class Message public const VALIDATOR_REQUIRED_FIELD = 'This field is required and cannot be empty.'; public const VALIDATOR_REQUIRED_FIELD_BY_NAME = '%s is required and cannot be empty.'; public const VALIDATOR_REQUIRED_UPLOAD = 'A file must be uploaded first.'; - public const RESTRICTION_DEPRECATION = 'Cannot use both `%s` and `%s` attributes on the same object.'; } diff --git a/src/App/src/Middleware/DeprecationMiddleware.php b/src/App/src/Middleware/DeprecationMiddleware.php index 3596938..f8a36bf 100644 --- a/src/App/src/Middleware/DeprecationMiddleware.php +++ b/src/App/src/Middleware/DeprecationMiddleware.php @@ -9,6 +9,7 @@ use Api\App\Exception\DeprecationConflictException; use Api\App\Handler\ResponseTrait; use Api\App\Message; +use Laminas\Stratigility\MiddlewarePipe; use Mezzio\Middleware\LazyLoadingMiddleware; use Mezzio\Router\RouteResult; use Psr\Http\Message\ResponseInterface; @@ -43,13 +44,20 @@ public function process( } $reflectionHandler = null; - $routeMiddleware = $routeResult->getMatchedRoute()->getMiddleware(); + $matchedRoute = $routeResult->getMatchedRoute(); + if (! $matchedRoute) { + return $response; + } + + $routeMiddleware = $matchedRoute->getMiddleware(); if ($routeMiddleware instanceof LazyLoadingMiddleware) { - $reflectionMiddlewareClass = new ReflectionClass($routeMiddleware->middlewareName); + /** @var class-string $routeMiddlewareName */ + $routeMiddlewareName = $routeMiddleware->middlewareName; + $reflectionMiddlewareClass = new ReflectionClass($routeMiddlewareName); if ($reflectionMiddlewareClass->implementsInterface(RequestHandlerInterface::class)) { $reflectionHandler = $reflectionMiddlewareClass; } - } else { + } elseif ($routeMiddleware instanceof MiddlewarePipe) { $reflectionClass = new ReflectionClass($routeMiddleware); $middlewarePipeline = $reflectionClass->getProperty('pipeline')->getValue($routeMiddleware); for ($middlewarePipeline->rewind(); $middlewarePipeline->valid(); $middlewarePipeline->next()) { @@ -80,29 +88,32 @@ public function process( ); } - $sunset = ''; - $link = ''; - if ($attributes[self::RESOURCE_DEPRECATION_ATTRIBUTE] ?? '') { + $sunset = null; + $link = null; + if (array_key_exists(self::RESOURCE_DEPRECATION_ATTRIBUTE, $attributes)) { $sunset = $attributes[self::RESOURCE_DEPRECATION_ATTRIBUTE]['sunset']; $link = $attributes[self::RESOURCE_DEPRECATION_ATTRIBUTE]['link']; } - if ($attributes[self::METHOD_DEPRECATION_ATTRIBUTE] ?? '') { + if (array_key_exists(self::METHOD_DEPRECATION_ATTRIBUTE, $attributes)) { $sunset = $attributes[self::METHOD_DEPRECATION_ATTRIBUTE]['sunset']; $link = $attributes[self::METHOD_DEPRECATION_ATTRIBUTE]['link']; } - if ($sunset) { + if ($sunset !== null) { $response = $response->withHeader('sunset', $sunset); } - if ($link) { + if ($link !== null) { $response = $response->withHeader('link', $link); } return $response; } + /** + * @param class-string $type + */ public function getAttributes(ReflectionClass|ReflectionMethod $reflection, string $type): array { $attributes = []; diff --git a/test/Unit/App/Attribute/MethodDeprecationTest.php b/test/Unit/App/Attribute/MethodDeprecationTest.php index 173eac5..ddc7288 100644 --- a/test/Unit/App/Attribute/MethodDeprecationTest.php +++ b/test/Unit/App/Attribute/MethodDeprecationTest.php @@ -19,7 +19,7 @@ public function testInvalidDateThrowsException(): void link: 'test-link', deprecationReason: 'test-deprecation-reason', )] - public function test() + public function test(): void { } }; @@ -40,7 +40,7 @@ public function testValidDatePassesValidation(): void link: 'test-link', deprecationReason: 'test-deprecation-reason', )] - public function test() + public function test(): void { } }; diff --git a/test/Unit/App/Middleware/DeprecationMiddlewareTest.php b/test/Unit/App/Middleware/DeprecationMiddlewareTest.php index 93bb2e5..7352a51 100644 --- a/test/Unit/App/Middleware/DeprecationMiddlewareTest.php +++ b/test/Unit/App/Middleware/DeprecationMiddlewareTest.php @@ -48,7 +48,7 @@ protected function setUp(): void * @throws ReflectionException * @throws Exception */ - public function testThrowsDeprecationConflictException() + public function testThrowsDeprecationConflictException(): void { $handler = new #[ResourceDeprecation( sunset: '2038-01-01',