Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.43% |
27 / 28 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
| GetListAction | |
96.43% |
27 / 28 |
|
88.89% |
8 / 9 |
12 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isAuthorized | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| __invoke | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
| getInputType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOutputType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPossibleActionResponseStatuses | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getDescription | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getTags | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRouteAttributes | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\Actions; |
| 3 | |
| 4 | use Apie\Core\Actions\ActionInterface; |
| 5 | use Apie\Core\Actions\ActionResponse; |
| 6 | use Apie\Core\Actions\ActionResponseStatus; |
| 7 | use Apie\Core\Actions\ActionResponseStatusList; |
| 8 | use Apie\Core\Actions\ApieFacadeInterface; |
| 9 | use Apie\Core\Attributes\Description; |
| 10 | use Apie\Core\BoundedContext\BoundedContextId; |
| 11 | use Apie\Core\Context\ApieContext; |
| 12 | use Apie\Core\ContextConstants; |
| 13 | use Apie\Core\Datalayers\Search\QuerySearch; |
| 14 | use Apie\Core\Dto\ListOf; |
| 15 | use Apie\Core\Entities\EntityInterface; |
| 16 | use Apie\Core\Exceptions\InvalidTypeException; |
| 17 | use Apie\Core\Lists\StringList; |
| 18 | use LogicException; |
| 19 | use ReflectionClass; |
| 20 | |
| 21 | /** |
| 22 | * Action to get a list of resources. |
| 23 | */ |
| 24 | final class GetListAction implements ActionInterface |
| 25 | { |
| 26 | public function __construct(private readonly ApieFacadeInterface $apieFacade) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | public static function isAuthorized(ApieContext $context, bool $runtimeChecks, bool $throwError = false): bool |
| 31 | { |
| 32 | $refl = new ReflectionClass($context->getContext(ContextConstants::RESOURCE_NAME, $throwError)); |
| 33 | return $context->appliesToContext($refl, $runtimeChecks, $throwError ? new LogicException("Class can not be accessed") : null); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param array<string|int, mixed> $rawContents |
| 38 | */ |
| 39 | public function __invoke(ApieContext $context, array $rawContents): ActionResponse |
| 40 | { |
| 41 | $context->withContext(ContextConstants::APIE_ACTION, __CLASS__)->checkAuthorization(); |
| 42 | $resourceClass = $context->getContext(ContextConstants::RESOURCE_NAME); |
| 43 | if (!is_a($resourceClass, EntityInterface::class, true)) { |
| 44 | throw new InvalidTypeException($resourceClass, 'EntityInterface'); |
| 45 | } |
| 46 | $resource = $this->apieFacade->all( |
| 47 | $resourceClass, |
| 48 | new BoundedContextId($context->getContext(ContextConstants::BOUNDED_CONTEXT_ID)) |
| 49 | ); |
| 50 | $result = $resource->toPaginatedResult(QuerySearch::fromArray($rawContents, $context)); |
| 51 | return ActionResponse::createRunSuccess($this->apieFacade, $context, $result, $resource); |
| 52 | } |
| 53 | |
| 54 | public static function getInputType(ReflectionClass $class): ReflectionClass |
| 55 | { |
| 56 | return $class; |
| 57 | } |
| 58 | |
| 59 | public static function getOutputType(ReflectionClass $class): ListOf |
| 60 | { |
| 61 | return new ListOf($class); |
| 62 | } |
| 63 | |
| 64 | public static function getPossibleActionResponseStatuses(): ActionResponseStatusList |
| 65 | { |
| 66 | return new ActionResponseStatusList([ |
| 67 | ActionResponseStatus::SUCCESS |
| 68 | ]); |
| 69 | } |
| 70 | |
| 71 | public static function getDescription(ReflectionClass $class): string |
| 72 | { |
| 73 | $description = 'Gets a list of resource that are an instance of ' . $class->getShortName(); |
| 74 | |
| 75 | foreach ($class->getAttributes(Description::class) as $attribute) { |
| 76 | $description .= '. ' . $attribute->newInstance()->description; |
| 77 | } |
| 78 | |
| 79 | return $description; |
| 80 | } |
| 81 | |
| 82 | public static function getTags(ReflectionClass $class): StringList |
| 83 | { |
| 84 | return new StringList([$class->getShortName(), 'resource']); |
| 85 | } |
| 86 | |
| 87 | public static function getRouteAttributes(ReflectionClass $class): array |
| 88 | { |
| 89 | return [ |
| 90 | ContextConstants::GET_ALL_OBJECTS => true, |
| 91 | ContextConstants::GET_OBJECT => true, |
| 92 | ContextConstants::RESOURCE_NAME => $class->name, |
| 93 | ]; |
| 94 | } |
| 95 | } |