Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
18 / 19 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| RunGlobalMethodRouteDefinition | |
94.74% |
18 / 19 |
|
83.33% |
5 / 6 |
13.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMethod | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
4.25 | |||
| getUrl | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| getAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOperationId | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| createFrom | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\RestApi\RouteDefinitions; |
| 3 | |
| 4 | use Apie\Common\ActionDefinitions\ActionDefinitionInterface; |
| 5 | use Apie\Common\ActionDefinitions\RunGlobalMethodDefinition; |
| 6 | use Apie\Common\Actions\RunAction; |
| 7 | use Apie\Common\Concerns\ReadsRouteAttribute; |
| 8 | use Apie\Core\Attributes\Route; |
| 9 | use Apie\Core\BoundedContext\BoundedContextId; |
| 10 | use Apie\Core\Enums\RequestMethod; |
| 11 | use Apie\Core\ValueObjects\UrlRouteDefinition; |
| 12 | use ReflectionMethod; |
| 13 | |
| 14 | class RunGlobalMethodRouteDefinition extends AbstractRestApiRouteDefinition |
| 15 | { |
| 16 | use ReadsRouteAttribute; |
| 17 | |
| 18 | private const CURRENT_TARGET = Route::API; |
| 19 | |
| 20 | public function __construct(ReflectionMethod $method, BoundedContextId $boundedContextId) |
| 21 | { |
| 22 | parent::__construct($method->getDeclaringClass(), $boundedContextId, $method); |
| 23 | } |
| 24 | |
| 25 | public function getMethod(): RequestMethod |
| 26 | { |
| 27 | $route = $this->getRouteAttribute(); |
| 28 | if ($route && $route->requestMethod) { |
| 29 | return $route->requestMethod; |
| 30 | } |
| 31 | return empty($this->method->getParameters()) ? RequestMethod::GET : RequestMethod::POST; |
| 32 | } |
| 33 | |
| 34 | public function getUrl(): UrlRouteDefinition |
| 35 | { |
| 36 | $route = $this->getRouteAttribute(); |
| 37 | if ($route) { |
| 38 | return new UrlRouteDefinition($route->routeDefinition); |
| 39 | } |
| 40 | $methodName = $this->method->getName(); |
| 41 | if ($methodName === '__invoke') { |
| 42 | return new UrlRouteDefinition($this->method->getDeclaringClass()->getShortName()); |
| 43 | } |
| 44 | return new UrlRouteDefinition($this->method->getDeclaringClass()->getShortName() . '/' . $methodName); |
| 45 | } |
| 46 | |
| 47 | public function getAction(): string |
| 48 | { |
| 49 | return RunAction::class; |
| 50 | } |
| 51 | |
| 52 | public function getOperationId(): string |
| 53 | { |
| 54 | $methodName = $this->method->getName(); |
| 55 | $suffix = $methodName === '__invoke' ? '' : ('-' . $methodName); |
| 56 | return 'call-method-' . $this->method->getDeclaringClass()->getShortName() . $suffix; |
| 57 | } |
| 58 | |
| 59 | public static function createFrom(ActionDefinitionInterface $actionDefinition): ?AbstractRestApiRouteDefinition |
| 60 | { |
| 61 | if ($actionDefinition instanceof RunGlobalMethodDefinition) { |
| 62 | return new self($actionDefinition->getMethod(), $actionDefinition->getBoundedContextId()); |
| 63 | } |
| 64 | return null; |
| 65 | } |
| 66 | } |