44
55namespace Api \App \Middleware ;
66
7- use Api \App \Attribute \MethodDeprecation ;
87use Api \App \Attribute \ResourceDeprecation ;
98use Api \App \Exception \ConflictException ;
109use Api \App \Exception \RuntimeException ;
1110use Api \App \Service \HandlerService ;
12- use Core \App \Message ;
1311use Dot \DependencyInjection \Attribute \Inject ;
1412use Psr \Http \Message \ResponseInterface ;
1513use Psr \Http \Message \ServerRequestInterface ;
1614use Psr \Http \Server \MiddlewareInterface ;
1715use Psr \Http \Server \RequestHandlerInterface ;
1816use ReflectionClass ;
1917use ReflectionException ;
20- use ReflectionMethod ;
2118
22- use function array_column ;
2319use function array_filter ;
24- use function array_intersect ;
2520use function array_merge ;
2621use function array_values ;
27- use function count ;
2822use function implode ;
2923use function is_string ;
3024use function sprintf ;
3125
3226class DeprecationMiddleware implements MiddlewareInterface
3327{
3428 public const RESOURCE_DEPRECATION_ATTRIBUTE = ResourceDeprecation::class;
35- public const METHOD_DEPRECATION_ATTRIBUTE = MethodDeprecation::class;
36-
37- public const DEPRECATION_ATTRIBUTES = [
38- self ::RESOURCE_DEPRECATION_ATTRIBUTE ,
39- self ::METHOD_DEPRECATION_ATTRIBUTE ,
40- ];
4129
4230 /**
4331 * @param array<non-empty-string, mixed> $config
@@ -71,7 +59,6 @@ public function process(
7159 return $ response ;
7260 }
7361
74- $ this ->validateAttributes ($ attributes );
7562 $ attribute = $ this ->getAttribute ($ attributes );
7663 if (null === $ attribute ) {
7764 return $ response ;
@@ -96,23 +83,12 @@ public function process(
9683 */
9784 private function getAttribute (array $ attributes ): ?array
9885 {
99- $ attribute = array_values (
86+ return array_values (
10087 array_filter (
10188 $ attributes ,
10289 fn (array $ attribute ): bool => $ attribute ['deprecationType ' ] === self ::RESOURCE_DEPRECATION_ATTRIBUTE
10390 )
10491 )[0 ] ?? null ;
105-
106- if (null === $ attribute ) {
107- $ attribute = array_values (
108- array_filter (
109- $ attributes ,
110- fn (array $ attribute ): bool => $ attribute ['deprecationType ' ] === self ::METHOD_DEPRECATION_ATTRIBUTE
111- )
112- )[0 ] ?? null ;
113- }
114-
115- return $ attribute ;
11692 }
11793
11894 /**
@@ -122,40 +98,17 @@ private function getAttribute(array $attributes): ?array
12298 private function getReflectionAttributes (ReflectionClass $ reflectionObject ): array
12399 {
124100 $ attributes = [];
101+
125102 foreach ($ reflectionObject ->getAttributes (self ::RESOURCE_DEPRECATION_ATTRIBUTE ) as $ attribute ) {
126103 $ attributes [] = array_merge (
127104 ($ attribute ->newInstance ())->toArray (),
128105 ['identifier ' => $ reflectionObject ->name ]
129106 );
130107 }
131108
132- foreach ($ reflectionObject ->getMethods (ReflectionMethod::IS_PUBLIC ) as $ refMethod ) {
133- foreach ($ refMethod ->getAttributes (self ::METHOD_DEPRECATION_ATTRIBUTE ) as $ attribute ) {
134- $ attributes [] = array_merge (($ attribute ->newInstance ())->toArray (), ['identifier ' => $ refMethod ->name ]);
135- }
136- }
137-
138109 return $ attributes ;
139110 }
140111
141- /**
142- * @param array<int, mixed> $attributes
143- * @throws ConflictException
144- */
145- private function validateAttributes (array $ attributes ): void
146- {
147- $ intersect = array_intersect (self ::DEPRECATION_ATTRIBUTES , array_column ($ attributes , 'deprecationType ' ));
148- if (count ($ intersect ) === count (self ::DEPRECATION_ATTRIBUTES )) {
149- throw ConflictException::create (
150- sprintf (
151- Message::RESTRICTION_DEPRECATION ,
152- self ::RESOURCE_DEPRECATION_ATTRIBUTE ,
153- self ::METHOD_DEPRECATION_ATTRIBUTE
154- )
155- );
156- }
157- }
158-
159112 /**
160113 * @param non-empty-string $baseLink
161114 * @param array<non-empty-string, mixed> $attribute
0 commit comments