Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| RunResourceMethodDefinition | |
100.00% |
22 / 22 |
|
100.00% |
5 / 5 |
11 | |
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 | |||
| getMethod | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| provideActionDefinitions | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\ActionDefinitions; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContext; |
| 5 | use Apie\Core\BoundedContext\BoundedContextId; |
| 6 | use Apie\Core\Context\ApieContext; |
| 7 | use Apie\Core\ContextConstants; |
| 8 | use Apie\Core\Entities\EntityInterface; |
| 9 | use Apie\Core\Entities\PolymorphicEntityInterface; |
| 10 | use Apie\Core\Utils\EntityUtils; |
| 11 | use ReflectionClass; |
| 12 | use ReflectionMethod; |
| 13 | |
| 14 | /** |
| 15 | * Action definition for modifying a single resource by id in a specific bounded context. |
| 16 | */ |
| 17 | final class RunResourceMethodDefinition implements ActionDefinitionInterface |
| 18 | { |
| 19 | /** |
| 20 | * @param ReflectionClass<EntityInterface> $resourceName |
| 21 | */ |
| 22 | public function __construct( |
| 23 | private readonly ReflectionClass $resourceName, |
| 24 | private readonly ReflectionMethod $method, |
| 25 | private readonly BoundedContextId $boundedContextId |
| 26 | ) { |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @return ReflectionClass<EntityInterface> |
| 31 | */ |
| 32 | public function getResourceName(): ReflectionClass |
| 33 | { |
| 34 | return $this->resourceName; |
| 35 | } |
| 36 | |
| 37 | public function getBoundedContextId(): BoundedContextId |
| 38 | { |
| 39 | return $this->boundedContextId; |
| 40 | } |
| 41 | |
| 42 | public function getMethod(): ReflectionMethod |
| 43 | { |
| 44 | return $this->method; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Business logic: |
| 49 | * - Check apie context with ContextConstants::RESOURCE_METHOD |
| 50 | */ |
| 51 | public static function provideActionDefinitions(BoundedContext $boundedContext, ApieContext $apieContext, bool $runtimeChecks = false): array |
| 52 | { |
| 53 | $actionDefinitions = []; |
| 54 | $resourceActionContext = $apieContext->withContext(ContextConstants::RESOURCE_METHOD, true); |
| 55 | foreach ($boundedContext->resources->filterOnApieContext($resourceActionContext, $runtimeChecks) as $resource) { |
| 56 | $resourceList = [$resource]; |
| 57 | if (in_array(PolymorphicEntityInterface::class, $resource->getInterfaceNames())) { |
| 58 | if ($runtimeChecks) { |
| 59 | if ($apieContext->hasContext(ContextConstants::RESOURCE)) { |
| 60 | $resourceList = [new ReflectionClass($apieContext->getContext(ContextConstants::RESOURCE))]; |
| 61 | } |
| 62 | } else { |
| 63 | $resourceList = EntityUtils::getDiscriminatorClasses($resource); |
| 64 | } |
| 65 | } |
| 66 | foreach ($resourceList as $actualClass) { |
| 67 | foreach ($resourceActionContext->getApplicableMethods($actualClass, $runtimeChecks) as $method) { |
| 68 | $definition = new RunResourceMethodDefinition( |
| 69 | $resource, |
| 70 | $method, |
| 71 | $boundedContext->getId() |
| 72 | ); |
| 73 | $actionDefinitions[] = $definition; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | return $actionDefinitions; |
| 78 | } |
| 79 | } |