Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
RunMethodCallOnSingleResourceRouteDefinition
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
7 / 7
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getActionOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOperationId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getUrl
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createFrom
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\RestApi\RouteDefinitions;
3
4use Apie\Common\ActionDefinitions\ActionDefinitionInterface;
5use Apie\Common\ActionDefinitions\RunResourceMethodDefinition;
6use Apie\Common\Actions\RunItemMethodAction;
7use Apie\Core\Attributes\ActionOptions;
8use Apie\Core\BoundedContext\BoundedContextId;
9use Apie\Core\ContextConstants;
10use Apie\Core\Entities\EntityInterface;
11use Apie\Core\Enums\RequestMethod;
12use Apie\Core\ValueObjects\UrlRouteDefinition;
13use ReflectionClass;
14use ReflectionMethod;
15
16/**
17 * Route definition for running method on single resource
18 */
19class RunMethodCallOnSingleResourceRouteDefinition extends AbstractRestApiRouteDefinition
20{
21    private string $suffix = '';
22    /**
23     * @param ReflectionClass<covariant EntityInterface> $className
24     */
25    public function __construct(
26        ReflectionClass $className,
27        ReflectionMethod $method,
28        BoundedContextId $boundedContextId,
29        private ?ActionOptions $actionOptions = null
30    ) {
31        parent::__construct($className, $boundedContextId, $method);
32        if ($this->actionOptions) {
33            $this->suffix = '-' . md5(json_encode($this->actionOptions));
34        }
35    }
36
37    public function getActionOptions(): ?ActionOptions
38    {
39        return $this->actionOptions;
40    }
41
42    public function getOperationId(): string
43    {
44        return 'get-single-' . $this->class->getShortName() . '-run-' . $this->method->name . $this->suffix;
45    }
46    
47    public function getMethod(): RequestMethod
48    {
49        if ($this->actionOptions?->requestMethod) {
50            return $this->actionOptions->requestMethod;
51        }
52        if ($this->method->getNumberOfParameters() === 0) {
53            return RequestMethod::GET;
54        }
55        if (str_starts_with($this->method->name, 'remove')) {
56            return RequestMethod::DELETE;
57        }
58        return RequestMethod::POST;
59    }
60
61    public function getUrl(): UrlRouteDefinition
62    {
63        $url = $this->class->getShortName();
64        $url .= ($this->method->isStatic()) ? '/' : ('/{' . ContextConstants::RESOURCE_ID . '}/');
65        $url .= RunItemMethodAction::getDisplayNameForMethod($this->method);
66        if ($this->actionOptions?->pathRemainder) {
67            $url .= '/' . $this->actionOptions->pathRemainder;
68        }
69        return new UrlRouteDefinition($url);
70    }
71
72    public function getAction(): string
73    {
74        return RunItemMethodAction::class;
75    }
76
77    public static function createFrom(ActionDefinitionInterface $actionDefinition): ?AbstractRestApiRouteDefinition
78    {
79        if ($actionDefinition instanceof RunResourceMethodDefinition) {
80            return new self(
81                $actionDefinition->getResourceName(),
82                $actionDefinition->getMethod(),
83                $actionDefinition->getBoundedContextId(),
84                $actionDefinition->getActionOptions()
85            );
86        }
87        return null;
88    }
89}