Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| DownloadFilesActionDefinition | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
6 | |
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 |
3 | |||
| 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\Entities\EntityInterface; |
| 8 | use Apie\Core\PropertyToFieldMetadataUtil; |
| 9 | use Apie\Core\Utils\ConverterUtils; |
| 10 | use Apie\TypeConverter\ReflectionTypeFactory; |
| 11 | use Psr\Http\Message\UploadedFileInterface; |
| 12 | use ReflectionClass; |
| 13 | use ReflectionNamedType; |
| 14 | |
| 15 | class DownloadFilesActionDefinition 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 | * - Add route definition if there's a getter with a UploadedFileInterface reference or no typehint (resource). |
| 42 | */ |
| 43 | public static function provideActionDefinitions( |
| 44 | BoundedContext $boundedContext, |
| 45 | ApieContext $apieContext, |
| 46 | bool $runtimeChecks = false |
| 47 | ): array { |
| 48 | $actionDefinitions = []; |
| 49 | $resourceActionContext = $apieContext; |
| 50 | $uploadedFileType = ReflectionTypeFactory::createReflectionType(UploadedFileInterface::class); |
| 51 | assert($uploadedFileType instanceof ReflectionNamedType); |
| 52 | foreach ($boundedContext->resources->filterOnApieContext($resourceActionContext, $runtimeChecks) as $resource) { |
| 53 | if (PropertyToFieldMetadataUtil::hasPropertyWithType( |
| 54 | ConverterUtils::toReflectionType($resource), |
| 55 | $uploadedFileType, |
| 56 | $resourceActionContext |
| 57 | )) { |
| 58 | $actionDefinitions[] = new self($resource, $boundedContext->getId()); |
| 59 | } |
| 60 | } |
| 61 | // @phpstan-ignore return.type |
| 62 | return $actionDefinitions; |
| 63 | } |
| 64 | } |