Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.11% covered (success)
92.11%
35 / 38
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetItemAction
92.11% covered (success)
92.11%
35 / 38
77.78% covered (warning)
77.78%
7 / 9
16.13
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
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
7.23
 __invoke
91.67% covered (success)
91.67%
11 / 12
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%
5 / 5
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%
4 / 4
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\Entities\EntityInterface;
13use Apie\Core\Exceptions\InvalidTypeException;
14use Apie\Core\Identifiers\IdentifierInterface;
15use Apie\Core\IdentifierUtils;
16use Apie\Core\Lists\PermissionList;
17use Apie\Core\Lists\StringList;
18use Apie\Core\Permissions\PermissionInterface;
19use Apie\Core\Permissions\RequiresPermissionsInterface;
20use Apie\Core\Utils\EntityUtils;
21use Apie\Serializer\Serializer;
22use LogicException;
23use ReflectionClass;
24
25/**
26 * Action to get a single item resource.
27 */
28final class GetItemAction implements ActionInterface
29{
30    public function __construct(private readonly ApieFacadeInterface $apieFacade)
31    {
32    }
33
34    public static function isAuthorized(ApieContext $context, bool $runtimeChecks, bool $throwError = false): bool
35    {
36        $refl = new ReflectionClass($context->getContext(ContextConstants::RESOURCE_NAME, $throwError));
37        $resource = $context->getContext(ContextConstants::RESOURCE, false);
38        if ($resource instanceof RequiresPermissionsInterface) {
39            $requiredPermissions = $resource->getRequiredPermissions();
40            $user = $context->getContext(ContextConstants::AUTHENTICATED_USER, false);
41            if ($user instanceof PermissionInterface) {
42                $hasPermisions = $user->getPermissionIdentifiers();
43                return $hasPermisions->hasOverlap($requiredPermissions);
44            }
45            return $requiredPermissions->hasOverlap(new PermissionList(['']));
46        }
47        if (EntityUtils::isPolymorphicEntity($refl) && $runtimeChecks && $resource) {
48            $refl = new ReflectionClass($resource);
49        }
50        return $context->appliesToContext($refl, $runtimeChecks, $throwError ? new LogicException('Operation is not allowed') : null);
51    }
52
53    /**
54     * @param array<string|int, mixed> $rawContents
55     */
56    public function __invoke(ApieContext $context, array $rawContents): ActionResponse
57    {
58        $context->withContext(ContextConstants::APIE_ACTION, __CLASS__)->checkAuthorization();
59        $resourceClass = new ReflectionClass($context->getContext(ContextConstants::RESOURCE_NAME));
60        $id = $context->getContext(ContextConstants::RESOURCE_ID);
61        if (!$resourceClass->implementsInterface(EntityInterface::class)) {
62            throw new InvalidTypeException($resourceClass->name, 'EntityInterface');
63        }
64        $result = $this->apieFacade->find(
65            IdentifierUtils::idStringToIdentifier($id, $context),
66            new BoundedContextId($context->getContext(ContextConstants::BOUNDED_CONTEXT_ID))
67        );
68        $context = $context->withContext(ContextConstants::RESOURCE, $result);
69        $context->withContext(ContextConstants::APIE_ACTION, __CLASS__)->checkAuthorization();
70        return ActionResponse::createRunSuccess($this->apieFacade, $context, $result, $result);
71    }
72
73    /**
74     * @return ReflectionClass<EntityInterface>
75     */
76    public static function getInputType(ReflectionClass $class): ReflectionClass
77    {
78        return $class;
79    }
80
81    /**
82     * @return ReflectionClass<EntityInterface>
83     */
84    public static function getOutputType(ReflectionClass $class): ReflectionClass
85    {
86        return $class;
87    }
88
89    public static function getPossibleActionResponseStatuses(): ActionResponseStatusList
90    {
91        return new ActionResponseStatusList([
92            ActionResponseStatus::SUCCESS,
93            ActionResponseStatus::AUTHORIZATION_ERROR,
94            ActionResponseStatus::NOT_FOUND
95        ]);
96    }
97
98    public static function getDescription(ReflectionClass $class): string
99    {
100        return 'Gets a resource of ' . $class->getShortName() . ' with a specific id';
101    }
102    
103    public static function getTags(ReflectionClass $class): StringList
104    {
105        return new StringList([$class->getShortName(), 'resource']);
106    }
107
108    public static function getRouteAttributes(ReflectionClass $class): array
109    {
110        return [
111            ContextConstants::GET_OBJECT => true,
112            ContextConstants::RESOURCE_NAME => $class->name,
113        ];
114    }
115}