Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.24% covered (warning)
88.24%
15 / 17
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreateResourceActionDefinition
88.24% covered (warning)
88.24%
15 / 17
75.00% covered (warning)
75.00%
3 / 4
12.23
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
 getResourceName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBoundedContextId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 provideActionDefinitions
85.71% covered (warning)
85.71%
12 / 14
0.00% covered (danger)
0.00%
0 / 1
9.24
1<?php
2namespace Apie\Common\ActionDefinitions;
3
4use Apie\Common\Actions\CreateObjectAction;
5use Apie\Core\BoundedContext\BoundedContext;
6use Apie\Core\BoundedContext\BoundedContextId;
7use Apie\Core\Context\ApieContext;
8use Apie\Core\ContextConstants;
9use Apie\Core\Entities\EntityInterface;
10use Apie\Core\Entities\PolymorphicEntityInterface;
11use Apie\Core\Metadata\MetadataFactory;
12use ReflectionClass;
13
14/**
15 * Definition for creating a single resource class in a specific bounded context.
16 */
17final class CreateResourceActionDefinition implements ActionDefinitionInterface
18{
19    /**
20     * @param ReflectionClass<EntityInterface> $resourceName
21     */
22    public function __construct(
23        private readonly ReflectionClass $resourceName,
24        private readonly BoundedContextId $boundedContextId
25    ) {
26    }
27
28    /**
29     * @return ReflectionClass<EntityInterface>
30     */
31    public function getResourceName(): ReflectionClass
32    {
33        return $this->resourceName;
34    }
35
36    public function getBoundedContextId(): BoundedContextId
37    {
38        return $this->boundedContextId;
39    }
40
41    /**
42     * Business logic for creating resources:
43     * - constructor should not have a required id argument
44     * - ContextConstants::CREATE_OBJECT restrictions can be added if it is not wanted
45     * - constructor is private or protected, but resource is not implementing PolymorphicEntityInterface
46     */
47    public static function provideActionDefinitions(BoundedContext $boundedContext, ApieContext $apieContext, bool $runtimeChecks = false): array
48    {
49        $actionDefinitions = [];
50        $postContext = $apieContext->withContext(ContextConstants::CREATE_OBJECT, true)
51            ->registerInstance($boundedContext);
52        foreach ($boundedContext->resources->filterOnApieContext($postContext, $runtimeChecks) as $resource) {
53            if ($runtimeChecks && !CreateObjectAction::isAuthorized($postContext->withContext(ContextConstants::RESOURCE_NAME, $resource->name), true)) {
54                continue;
55            }
56            $constructor = $resource->getConstructor();
57            if ($constructor && !$constructor->isPublic() && !$resource->implementsInterface(PolymorphicEntityInterface::class)) {
58                continue;
59            }
60            $metadata = MetadataFactory::getCreationMetadata($resource, $postContext);
61            $hashmap = $metadata->getHashmap();
62            if (!isset($hashmap['id']) || !$hashmap['id']->isRequired()) {
63                $actionDefinitions[] = new CreateResourceActionDefinition($resource, $boundedContext->getId());
64            }
65        }
66
67        return $actionDefinitions;
68    }
69}