Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| RemoveResourceActionDefinition | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
10 | |
100.00% |
1 / 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 | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\ActionDefinitions; |
| 3 | |
| 4 | use Apie\Core\Attributes\RemovalCheck; |
| 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 ReflectionClass; |
| 11 | |
| 12 | /** |
| 13 | * Action definition for removing a single resource by id in a specific bounded context. |
| 14 | */ |
| 15 | final class RemoveResourceActionDefinition implements ActionDefinitionInterface |
| 16 | { |
| 17 | /** |
| 18 | * @param ReflectionClass<EntityInterface> $resourceName |
| 19 | */ |
| 20 | public function __construct( |
| 21 | private readonly ReflectionClass $resourceName, |
| 22 | private readonly BoundedContextId $boundedContextId |
| 23 | ) { |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @return ReflectionClass<EntityInterface> |
| 28 | */ |
| 29 | public function getResourceName(): ReflectionClass |
| 30 | { |
| 31 | return $this->resourceName; |
| 32 | } |
| 33 | |
| 34 | public function getBoundedContextId(): BoundedContextId |
| 35 | { |
| 36 | return $this->boundedContextId; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Business logic for removing single resources: |
| 41 | * - Class should have a CanRemove attribute at top of the class. |
| 42 | * - ContextConstants::REMOVE_OBJECT restrictions can be added if it is not wanted in some cases |
| 43 | */ |
| 44 | public static function provideActionDefinitions(BoundedContext $boundedContext, ApieContext $apieContext, bool $runtimeChecks = false): array |
| 45 | { |
| 46 | $actionDefinitions = []; |
| 47 | $getSingleContext = $apieContext->withContext(ContextConstants::REMOVE_OBJECT, true) |
| 48 | ->registerInstance($boundedContext); |
| 49 | foreach ($boundedContext->resources->filterOnApieContext($getSingleContext, $runtimeChecks) as $resource) { |
| 50 | $foundAttribute = false; |
| 51 | foreach ($resource->getAttributes(RemovalCheck::class) as $removalCheckAttribute) { |
| 52 | $removalCheck = $removalCheckAttribute->newInstance(); |
| 53 | if ($removalCheck->applies($apieContext) && ($runtimeChecks || $removalCheck->isStaticCheck())) { |
| 54 | $foundAttribute = true; |
| 55 | } |
| 56 | } |
| 57 | if ($foundAttribute) { |
| 58 | $actionDefinitions[] = new RemoveResourceActionDefinition($resource, $boundedContext->getId()); |
| 59 | } |
| 60 | } |
| 61 | return $actionDefinitions; |
| 62 | } |
| 63 | } |