Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ModifyResourceActionDefinition
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 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
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2namespace Apie\Common\ActionDefinitions;
3
4use Apie\Core\BoundedContext\BoundedContext;
5use Apie\Core\BoundedContext\BoundedContextId;
6use Apie\Core\Context\ApieContext;
7use Apie\Core\ContextConstants;
8use Apie\Core\Entities\EntityInterface;
9use Apie\Core\Metadata\MetadataFactory;
10use ReflectionClass;
11
12/**
13 * Action definition for modifying a single resource by id in a specific bounded context.
14 */
15final class ModifyResourceActionDefinition 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:
41     * - Check apie context with ContextConstants::EDIT_OBJECT
42     * - A writable public property or setter should be defined in the class.
43     */
44    public static function provideActionDefinitions(BoundedContext $boundedContext, ApieContext $apieContext, bool $runtimeChecks = false): array
45    {
46        $actionDefinitions = [];
47        $patchSingleContext = $apieContext->withContext(ContextConstants::EDIT_OBJECT, true)
48            ->registerInstance($boundedContext);
49        foreach ($boundedContext->resources->filterOnApieContext($patchSingleContext, $runtimeChecks) as $resource) {
50            $metadata = MetadataFactory::getModificationMetadata($resource, $patchSingleContext);
51            if ($metadata->getHashmap()->count()) {
52                $actionDefinitions[] = new ModifyResourceActionDefinition($resource, $boundedContext->getId());
53            }
54        }
55
56        return $actionDefinitions;
57    }
58}