|
12 | 12 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
13 | 13 | use Symfony\Component\HttpKernel\Exception\HttpException;
|
14 | 14 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
| 15 | +use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; |
15 | 16 | use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
16 | 17 | use Symfony\Component\HttpKernel\KernelEvents;
|
17 | 18 | use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
18 | 19 | use Throwable;
|
| 20 | +use Unleash\Client\Bundle\Attribute\ControllerAttribute; |
19 | 21 | use Unleash\Client\Bundle\Attribute\IsEnabled;
|
| 22 | +use Unleash\Client\Bundle\Attribute\IsNotEnabled; |
20 | 23 | use Unleash\Client\Bundle\Event\BeforeExceptionThrownForAttributeEvent;
|
21 | 24 | use Unleash\Client\Bundle\Event\UnleashEvents;
|
22 | 25 | use Unleash\Client\Exception\InvalidValueException;
|
@@ -61,36 +64,44 @@ public function onControllerResolved(ControllerEvent $event): void
|
61 | 64 | $reflectionClass = new ReflectionClass($class);
|
62 | 65 | $reflectionMethod = $reflectionClass->getMethod($method);
|
63 | 66 |
|
64 |
| - /** @var array<ReflectionAttribute<IsEnabled>> $attributes */ |
| 67 | + /** @var array<ReflectionAttribute<ControllerAttribute>> $attributes */ |
65 | 68 | $attributes = [
|
66 |
| - ...$reflectionClass->getAttributes(IsEnabled::class), |
67 |
| - ...$reflectionMethod->getAttributes(IsEnabled::class), |
| 69 | + ...$reflectionClass->getAttributes(ControllerAttribute::class, ReflectionAttribute::IS_INSTANCEOF), |
| 70 | + ...$reflectionMethod->getAttributes(ControllerAttribute::class, ReflectionAttribute::IS_INSTANCEOF), |
68 | 71 | ];
|
69 | 72 |
|
70 | 73 | foreach ($attributes as $attribute) {
|
71 | 74 | $attribute = $attribute->newInstance();
|
72 |
| - assert($attribute instanceof IsEnabled); |
73 |
| - if (!$this->unleash->isEnabled($attribute->featureName)) { |
| 75 | + assert($attribute instanceof ControllerAttribute); |
| 76 | + |
| 77 | + $isFeatureEnabled = $this->unleash->isEnabled($attribute->getFeatureName()); |
| 78 | + $throwException = match ($attribute::class) { |
| 79 | + IsEnabled::class => !$isFeatureEnabled, |
| 80 | + IsNotEnabled::class => $isFeatureEnabled, |
| 81 | + default => false, |
| 82 | + }; |
| 83 | + if ($throwException) { |
74 | 84 | throw $this->getException($attribute);
|
75 | 85 | }
|
76 | 86 | }
|
77 | 87 | }
|
78 | 88 |
|
79 |
| - private function getException(IsEnabled $attribute): HttpException|Throwable |
| 89 | + private function getException(ControllerAttribute $attribute): HttpException|Throwable |
80 | 90 | {
|
81 |
| - $event = new BeforeExceptionThrownForAttributeEvent($attribute->errorCode); |
| 91 | + $event = new BeforeExceptionThrownForAttributeEvent($attribute->getErrorCode()); |
82 | 92 | $this->eventDispatcher->dispatch($event, UnleashEvents::BEFORE_EXCEPTION_THROWN_FOR_ATTRIBUTE);
|
83 | 93 | $exception = $event->getException();
|
84 | 94 | if ($exception !== null) {
|
85 | 95 | return $exception;
|
86 | 96 | }
|
87 | 97 |
|
88 |
| - return match ($attribute->errorCode) { |
| 98 | + return match ($attribute->getErrorCode()) { |
89 | 99 | Response::HTTP_BAD_REQUEST => new BadRequestHttpException(),
|
90 | 100 | Response::HTTP_UNAUTHORIZED => new UnauthorizedHttpException('Unauthorized'),
|
91 | 101 | Response::HTTP_FORBIDDEN => new AccessDeniedHttpException(),
|
92 | 102 | Response::HTTP_NOT_FOUND => new NotFoundHttpException(),
|
93 |
| - default => throw new InvalidValueException("Unsupported status code: {$attribute->errorCode}"), |
| 103 | + Response::HTTP_SERVICE_UNAVAILABLE => new ServiceUnavailableHttpException(), |
| 104 | + default => throw new InvalidValueException("Unsupported status code: {$attribute->getErrorCode()}"), |
94 | 105 | };
|
95 | 106 | }
|
96 | 107 | }
|
0 commit comments