Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.00% covered (success)
95.00%
19 / 20
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RestApiRouteDefinitionProvider
95.00% covered (success)
95.00%
19 / 20
50.00% covered (danger)
50.00%
1 / 2
6
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
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
5.00
1<?php
2namespace Apie\RestApi\RouteDefinitions;
3
4use Apie\Common\ActionDefinitionProvider;
5use Apie\Common\Interfaces\RouteDefinitionProviderInterface;
6use Apie\Common\RouteDefinitions\ActionHashmap;
7use Apie\Core\BoundedContext\BoundedContext;
8use Apie\Core\Context\ApieContext;
9use Psr\Log\LoggerInterface;
10
11final class RestApiRouteDefinitionProvider implements RouteDefinitionProviderInterface
12{
13    private const CLASSES = [
14        CreateResourceRouteDefinition::class,
15        PutResourceRouteDefinition::class,
16        RemoveSingleResourceRouteDefinition::class,
17        GetResourceListRouteDefinition::class,
18        GetSingleResourceRouteDefinition::class,
19        PatchSingleResourceRouteDefinition::class,
20        RunGlobalMethodRouteDefinition::class,
21        RunMethodCallOnSingleResourceRouteDefinition::class,
22        StreamMethodCallOnSingleResourceRouteDefinition::class,
23    ];
24
25    public function __construct(
26        private ActionDefinitionProvider $actionDefinitionProvider,
27        private LoggerInterface $logger,
28    ) {
29    }
30
31    public function getActionsForBoundedContext(BoundedContext $boundedContext, ApieContext $apieContext): ActionHashmap
32    {
33        $routes = [];
34        $definition = new OpenApiDocumentationRouteDefinition(true, $boundedContext->getId());
35        $routes[$definition->getOperationId()] = $definition;
36        $definition = new OpenApiDocumentationRouteDefinition(false, $boundedContext->getId());
37        $routes[$definition->getOperationId()] = $definition;
38        $definition = new SwaggerUIRouteDefinition($boundedContext->getId());
39        $routes[$definition->getOperationId()] = $definition;
40        $definition = new SwaggerUIRedirectRouteDefinition($boundedContext->getId());
41        $routes[$definition->getOperationId()] = $definition;
42
43        foreach ($this->actionDefinitionProvider->provideActionDefinitions($boundedContext, $apieContext) as $actionDefinition) {
44            $found = false;
45            foreach (self::CLASSES as $routeDefinitionClass) {
46                $routeDefinition = $routeDefinitionClass::createFrom($actionDefinition);
47                if ($routeDefinition) {
48                    $routes[$routeDefinition->getOperationId()] = $routeDefinition;
49                    $found = true;
50                }
51            }
52            if (!$found) {
53                $this->logger->debug('No route definition created for ' . get_debug_type($actionDefinition));
54            }
55        }
56        return new ActionHashmap($routes);
57    }
58}