Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.12% |
16 / 17 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ReplaceResourceActionDefinition | |
94.12% |
16 / 17 |
|
75.00% |
3 / 4 |
12.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getResourceName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBoundedContextId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| provideActionDefinitions | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
9.03 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\ActionDefinitions; |
| 3 | |
| 4 | use Apie\Common\Actions\CreateObjectAction; |
| 5 | use Apie\Core\BoundedContext\BoundedContext; |
| 6 | use Apie\Core\BoundedContext\BoundedContextId; |
| 7 | use Apie\Core\Context\ApieContext; |
| 8 | use Apie\Core\ContextConstants; |
| 9 | use Apie\Core\Entities\EntityInterface; |
| 10 | use Apie\Core\Entities\PolymorphicEntityInterface; |
| 11 | use Apie\Core\Metadata\MetadataFactory; |
| 12 | use ReflectionClass; |
| 13 | |
| 14 | /** |
| 15 | * Definition for replacing a single resource class in a specific bounded context. |
| 16 | */ |
| 17 | final class ReplaceResourceActionDefinition 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 have a id argument or setId method, does not have to be required |
| 44 | * - ContextConstants::REPLACE_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 ReplaceResourceActionDefinition($resource, $boundedContext->getId()); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return $actionDefinitions; |
| 68 | } |
| 69 | } |