Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.22% covered (success)
97.22%
35 / 36
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CmsRouteDefinitionProvider
97.22% covered (success)
97.22%
35 / 36
50.00% covered (danger)
50.00%
1 / 2
13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActionsForBoundedContext
97.14% covered (success)
97.14%
34 / 35
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2namespace Apie\Cms\RouteDefinitions;
3
4use Apie\Common\ActionDefinitionProvider;
5use Apie\Common\ActionDefinitions\CreateResourceActionDefinition;
6use Apie\Common\ActionDefinitions\ModifyResourceActionDefinition;
7use Apie\Common\ActionDefinitions\ReplaceResourceActionDefinition;
8use Apie\Common\ActionDefinitions\RunGlobalMethodDefinition;
9use Apie\Common\Interfaces\RouteDefinitionProviderInterface;
10use Apie\Common\RouteDefinitions\ActionHashmap;
11use Apie\Core\BoundedContext\BoundedContext;
12use Apie\Core\Context\ApieContext;
13use Psr\Log\LoggerInterface;
14
15class CmsRouteDefinitionProvider implements RouteDefinitionProviderInterface
16{
17    private const CLASSES = [
18        CreateResourceFormRouteDefinition::class,
19        CreateResourceCommitRouteDefinition::class,
20        DisplayResourceRouteDefinition::class,
21        DisplayResourceOverviewRouteDefinition::class,
22        ModifyResourceFormRouteDefinition::class,
23        ModifyResourceCommitRouteDefinition::class,
24        RemoveResourceFormRouteDefinition::class,
25        RemoveResourceFormCommitRouteDefinition::class,
26        RunGlobalMethodCommitRouteDefinition::class,
27        RunGlobalMethodFormRouteDefinition::class,
28        RunMethodCallOnSingleResourceCommitRouteDefinition::class,
29        RunMethodCallOnSingleResourceFormRouteDefinition::class,
30        StreamMethodCallOnSingleResourceRouteDefinition::class,
31    ];
32
33    public function __construct(
34        private ActionDefinitionProvider $actionDefinitionProvider,
35        private LoggerInterface $logger,
36        private bool $enableCmsDropdown = true,
37    ) {
38    }
39
40    public function getActionsForBoundedContext(BoundedContext $boundedContext, ApieContext $apieContext): ActionHashmap
41    {
42        $routes = [];
43        $definition = new DashboardRouteDefinition($boundedContext->getId());
44        $routes[$definition->getOperationId()] = $definition;
45        $definition = new LastActionResultRouteDefinition($boundedContext->getId());
46        $routes[$definition->getOperationId()] = $definition;
47
48        foreach ($this->actionDefinitionProvider->provideActionDefinitions($boundedContext, $apieContext) as $actionDefinition) {
49            $found = false;
50            foreach (self::CLASSES as $routeDefinitionClass) {
51                $routeDefinition = $routeDefinitionClass::createFrom($actionDefinition);
52                if ($routeDefinition) {
53                    $routes[$routeDefinition->getOperationId()] = $routeDefinition;
54                    $found = true;
55                }
56            }
57            if ($this->enableCmsDropdown &&
58                ($actionDefinition instanceof CreateResourceActionDefinition || $actionDefinition instanceof ReplaceResourceActionDefinition)) {
59                $routeDefinition = new \Apie\CmsApiDropdownOption\RouteDefinitions\DropdownOptionsForNewObjectRouteDefinition(
60                    $actionDefinition->getResourceName(),
61                    $actionDefinition->getBoundedContextId(),
62                );
63                $routes[$routeDefinition->getOperationId()] = $routeDefinition;
64            }
65            if ($this->enableCmsDropdown && $actionDefinition instanceof ModifyResourceActionDefinition) {
66                $routeDefinition = new \Apie\CmsApiDropdownOption\RouteDefinitions\DropdownOptionsForExistingObjectRouteDefinition(
67                    $actionDefinition->getResourceName(),
68                    $actionDefinition->getBoundedContextId(),
69                );
70                $routes[$routeDefinition->getOperationId()] = $routeDefinition;
71            }
72            if ($this->enableCmsDropdown && $actionDefinition instanceof RunGlobalMethodDefinition) {
73                $routeDefinition = new \Apie\CmsApiDropdownOption\RouteDefinitions\DropdownOptionsForGlobalMethodRouteDefinition(
74                    $actionDefinition->getMethod()->getDeclaringClass(),
75                    $actionDefinition->getBoundedContextId(),
76                    $actionDefinition->getMethod(),
77                );
78                $routes[$routeDefinition->getOperationId()] = $routeDefinition;
79            }
80            if (!$found) {
81                $this->logger->debug('No route definition created for ' . get_debug_type($actionDefinition));
82            }
83        }
84
85        return new ActionHashmap($routes);
86    }
87}