Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractRestApiRouteDefinition
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
8 / 8
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInputType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOutputType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPossibleActionResponseStatuses
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getController
n/a
0 / 0
n/a
0 / 0
0
 getDescription
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTags
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getActionOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRouteAttributes
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2namespace Apie\Common\RouteDefinitions;
3
4use Apie\Common\Interfaces\RestApiRouteDefinition;
5use Apie\Core\Actions\ActionResponseStatusList;
6use Apie\Core\Attributes\ActionOptions;
7use Apie\Core\Attributes\ActionResponseList;
8use Apie\Core\BoundedContext\BoundedContextId;
9use Apie\Core\ContextConstants;
10use Apie\Core\Dto\ListOf;
11use Apie\Core\Lists\StringList;
12use Apie\Core\Utils\ConverterUtils;
13use ReflectionClass;
14use ReflectionMethod;
15use ReflectionType;
16
17abstract class AbstractRestApiRouteDefinition implements RestApiRouteDefinition
18{
19    /**
20     * @param ReflectionClass<covariant object> $class
21     */
22    public function __construct(
23        protected readonly ReflectionClass $class,
24        protected readonly ?BoundedContextId $boundedContextId = null,
25        protected readonly ?ReflectionMethod $method = null
26    ) {
27    }
28
29    /**
30     * @return ReflectionClass<covariant object>|ReflectionMethod|ReflectionType
31     */
32    final public function getInputType(): ReflectionClass|ReflectionMethod|ReflectionType
33    {
34        $actionClass = $this->getAction();
35        return $actionClass::getInputType($this->class, $this->method);
36    }
37
38    /**
39     * @return ReflectionClass<covariant object>|ReflectionMethod|ReflectionType|ListOf
40     */
41    final public function getOutputType(): ReflectionClass|ReflectionMethod|ReflectionType|ListOf
42    {
43        $actionClass = $this->getAction();
44        return $actionClass::getOutputType($this->class, $this->method);
45    }
46
47    final public function getPossibleActionResponseStatuses(): ActionResponseStatusList
48    {
49        $actionClass = $this->getAction();
50        $output = $actionClass::getOutputType($this->class, $this->method);
51        $target = $this->method ?? ConverterUtils::toReflectionClass(
52            $output instanceof ListOf ? $output->type : $output
53        );
54        if ($target) {
55            foreach ($target->getAttributes(ActionResponseList::class) as $attribute) {
56                return new ActionResponseStatusList($attribute->newInstance()->statuses);
57            }
58        }
59        return $actionClass::getPossibleActionResponseStatuses($this->method);
60    }
61
62    /**
63     * @return class-string<covariant object>
64     */
65    abstract public function getController(): string;
66
67    final public function getDescription(): string
68    {
69        $actionClass = $this->getAction();
70        return $actionClass::getDescription($this->class, $this->method);
71    }
72
73    final public function getTags(): StringList
74    {
75        $actionClass = $this->getAction();
76        // @phpstan-ignore-next-line arguments.count
77        return $actionClass::getTags($this->class, $this->method);
78    }
79
80    public function getActionOptions(): ?ActionOptions
81    {
82        return null;
83    }
84
85    final public function getRouteAttributes(): array
86    {
87        $actionClass = $this->getAction();
88        $attributes = $actionClass::getRouteAttributes($this->class, $this->method);
89        $actionOptions = $this->getActionOptions();
90        if ($actionOptions) {
91            foreach ($actionOptions->placeholders as $key => $value) {
92                $attributes[$key] = $value;
93            }
94        }
95        $attributes[ContextConstants::APIE_ACTION] = $this->getAction();
96        $attributes[ContextConstants::OPERATION_ID] = $this->getOperationId();
97        $attributes[ContextConstants::BOUNDED_CONTEXT_ID] = $this->boundedContextId->toNative();
98        return $attributes;
99    }
100}