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
12 changes: 0 additions & 12 deletions src/App/src/Attribute/MethodDeprecation.php

This file was deleted.

6 changes: 0 additions & 6 deletions src/App/src/Handler/PostErrorReportResourceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Api\App\Handler;

use Api\App\Attribute\MethodDeprecation;
use Api\App\Exception\BadRequestException;
use Api\App\InputFilter\ErrorReportInputFilter;
use Api\App\Service\ErrorReportServiceInterface;
Expand All @@ -31,11 +30,6 @@ public function __construct(
* @throws BadRequestException
* @throws RuntimeException
*/
#[MethodDeprecation(
sunset: '2038-01-01',
link: 'https://docs.dotkernel.org/api-documentation/v5/core-features/versioning',
deprecationReason: 'Method deprecation example.',
)]
public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->inputFilter->setData((array) $request->getParsedBody());
Expand Down
51 changes: 2 additions & 49 deletions src/App/src/Middleware/DeprecationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,28 @@

namespace Api\App\Middleware;

use Api\App\Attribute\MethodDeprecation;
use Api\App\Attribute\ResourceDeprecation;
use Api\App\Exception\ConflictException;
use Api\App\Exception\RuntimeException;
use Api\App\Service\HandlerService;
use Core\App\Message;
use Dot\DependencyInjection\Attribute\Inject;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;

use function array_column;
use function array_filter;
use function array_intersect;
use function array_merge;
use function array_values;
use function count;
use function implode;
use function is_string;
use function sprintf;

class DeprecationMiddleware implements MiddlewareInterface
{
public const RESOURCE_DEPRECATION_ATTRIBUTE = ResourceDeprecation::class;
public const METHOD_DEPRECATION_ATTRIBUTE = MethodDeprecation::class;

public const DEPRECATION_ATTRIBUTES = [
self::RESOURCE_DEPRECATION_ATTRIBUTE,
self::METHOD_DEPRECATION_ATTRIBUTE,
];

/**
* @param array<non-empty-string, mixed> $config
Expand Down Expand Up @@ -71,7 +59,6 @@ public function process(
return $response;
}

$this->validateAttributes($attributes);
$attribute = $this->getAttribute($attributes);
if (null === $attribute) {
return $response;
Expand All @@ -96,23 +83,12 @@ public function process(
*/
private function getAttribute(array $attributes): ?array
{
$attribute = array_values(
return array_values(
array_filter(
$attributes,
fn (array $attribute): bool => $attribute['deprecationType'] === self::RESOURCE_DEPRECATION_ATTRIBUTE
)
)[0] ?? null;

if (null === $attribute) {
$attribute = array_values(
array_filter(
$attributes,
fn (array $attribute): bool => $attribute['deprecationType'] === self::METHOD_DEPRECATION_ATTRIBUTE
)
)[0] ?? null;
}

return $attribute;
}

/**
Expand All @@ -122,40 +98,17 @@ private function getAttribute(array $attributes): ?array
private function getReflectionAttributes(ReflectionClass $reflectionObject): array
{
$attributes = [];

foreach ($reflectionObject->getAttributes(self::RESOURCE_DEPRECATION_ATTRIBUTE) as $attribute) {
$attributes[] = array_merge(
($attribute->newInstance())->toArray(),
['identifier' => $reflectionObject->name]
);
}

foreach ($reflectionObject->getMethods(ReflectionMethod::IS_PUBLIC) as $refMethod) {
foreach ($refMethod->getAttributes(self::METHOD_DEPRECATION_ATTRIBUTE) as $attribute) {
$attributes[] = array_merge(($attribute->newInstance())->toArray(), ['identifier' => $refMethod->name]);
}
}

return $attributes;
}

/**
* @param array<int, mixed> $attributes
* @throws ConflictException
*/
private function validateAttributes(array $attributes): void
{
$intersect = array_intersect(self::DEPRECATION_ATTRIBUTES, array_column($attributes, 'deprecationType'));
if (count($intersect) === count(self::DEPRECATION_ATTRIBUTES)) {
throw ConflictException::create(
sprintf(
Message::RESTRICTION_DEPRECATION,
self::RESOURCE_DEPRECATION_ATTRIBUTE,
self::METHOD_DEPRECATION_ATTRIBUTE
)
);
}
}

/**
* @param non-empty-string $baseLink
* @param array<non-empty-string, mixed> $attribute
Expand Down
9 changes: 0 additions & 9 deletions src/Core/src/App/src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class Message
public const RESOURCE_ALREADY_REGISTERED = 'Resource "%s" is already registered.';
public const RESOURCE_NOT_ALLOWED = 'You are not allowed to access this resource.';
public const RESOURCE_NOT_FOUND = '%s not found.';
public const RESTRICTION_DEPRECATION = 'Cannot use both "%s" and "%s" attributes on the same object.';
public const RESTRICTION_IMAGE = 'File must be an image> Accepted mim type(s): %s';
public const RESTRICTION_ROLES = 'At least one role is required.';
public const ROLE_NOT_FOUND = 'Role not found.';
Expand Down Expand Up @@ -143,14 +142,6 @@ public static function resourceNotFound(string $resource = 'Resource'): string
return sprintf(self::RESOURCE_NOT_FOUND, $resource);
}

/**
* @return non-empty-string
*/
public static function restrictionDeprecation(string $first, string $second): string
{
return sprintf(self::RESTRICTION_DEPRECATION, $first, $second);
}

/**
* @param string[] $mimeTypes
* @return non-empty-string
Expand Down
128 changes: 0 additions & 128 deletions test/Unit/App/Attribute/MethodDeprecationTest.php

This file was deleted.

41 changes: 0 additions & 41 deletions test/Unit/App/Middleware/DeprecationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ApiTest\Unit\App\Middleware;

use Api\App\Attribute\MethodDeprecation;
use Api\App\Attribute\ResourceDeprecation;
use Api\App\Exception\ConflictException;
use Api\App\Middleware\DeprecationMiddleware;
Expand Down Expand Up @@ -50,46 +49,6 @@ protected function setUp(): void
$this->deprecationMiddleware = new DeprecationMiddleware(self::VERSIONING_CONFIG);
}

/**
* @throws ReflectionException
* @throws Exception
*/
public function testThrowsDeprecationConflictException(): void
{
$handler = new #[ResourceDeprecation(
sunset: '2038-01-01',
link: 'test-link',
deprecationReason: 'test-deprecation-reason',
)] class implements RequestHandlerInterface {
#[MethodDeprecation(
sunset: '2038-01-01',
link: 'test-link',
deprecationReason: 'test-deprecation-reason',
)]
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new EmptyResponse();
}
};

$routeResult = $this->createMock(RouteResult::class);
$route = $this->createMock(Route::class);
$lazyLoadingMiddleware = new LazyLoadingMiddleware(
$this->createMock(MiddlewareContainer::class),
$handler::class,
);

$route->method('getMiddleware')->willReturn($lazyLoadingMiddleware);
$routeResult->method('isFailure')->willReturn(false);
$routeResult->method('getMatchedRoute')->willReturn($route);
$this->request->method('getAttribute')->with(RouteResult::class)->willReturn($routeResult);
$this->handler->method('handle')->with($this->request)->willReturn($this->response);

$this->expectException(ConflictException::class);

$this->deprecationMiddleware->process($this->request, $this->handler);
}

/**
* @throws Exception
* @throws ConflictException
Expand Down