Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
RunGlobalMethodFormRouteDefinition
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
8 / 8
12
100.00% covered (success)
100.00%
1 / 1
 createFrom
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUrl
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 getMainMenuUri
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getController
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOperationId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\Cms\RouteDefinitions;
3
4use Apie\Cms\Controllers\RunGlobalMethodFormController;
5use Apie\Common\ActionDefinitions\ActionDefinitionInterface;
6use Apie\Common\ActionDefinitions\RunGlobalMethodDefinition;
7use Apie\Common\Actions\RunAction;
8use Apie\Common\Concerns\ReadsRouteAttribute;
9use Apie\Core\Attributes\Route;
10use Apie\Core\BoundedContext\BoundedContextId;
11use Apie\Core\Enums\RequestMethod;
12use Apie\Core\ValueObjects\UrlRouteDefinition;
13use ReflectionMethod;
14
15class RunGlobalMethodFormRouteDefinition extends AbstractCmsRouteDefinition
16{
17    use ReadsRouteAttribute;
18
19    private const CURRENT_TARGET = Route::CMS;
20
21    public static function createFrom(ActionDefinitionInterface $actionDefinition): ?AbstractCmsRouteDefinition
22    {
23        if ($actionDefinition instanceof RunGlobalMethodDefinition) {
24            return new self($actionDefinition->getMethod(), $actionDefinition->getBoundedContextId());
25        }
26        return null;
27    }
28
29    public function __construct(ReflectionMethod $method, BoundedContextId $boundedContextId)
30    {
31        parent::__construct($method->getDeclaringClass(), $boundedContextId, $method);
32    }
33
34    public function getMethod(): RequestMethod
35    {
36        return RequestMethod::GET;
37    }
38
39    public function getUrl(): UrlRouteDefinition
40    {
41        $route = $this->getRouteAttribute();
42        if ($route) {
43            return new UrlRouteDefinition($route->routeDefinition);
44        }
45        $methodName = $this->method->getName();
46        if ($methodName === '__invoke') {
47            return new UrlRouteDefinition('action/' . $this->method->getDeclaringClass()->getShortName());
48        }
49        return new UrlRouteDefinition('action/' . $this->method->getDeclaringClass()->getShortName() . '/' . $methodName);
50    }
51
52    public function getMainMenuUri(): ?UrlRouteDefinition
53    {
54        $url = preg_replace('/^\/?action\//', '', $this->getUrl()->toNative());
55        return new UrlRouteDefinition($url);
56    }
57
58    public function getController(): string
59    {
60        return RunGlobalMethodFormController::class;
61    }
62
63    public function getAction(): string
64    {
65        return RunAction::class;
66    }
67
68    public function getOperationId(): string
69    {
70        $methodName = $this->method->getName();
71        $suffix = $methodName === '__invoke' ? '' : ('-' . $methodName);
72        return 'form-call-method-' . $this->method->getDeclaringClass()->getShortName() . $suffix;
73    }
74}