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