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