Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.48% covered (success)
93.48%
43 / 46
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetItemAction
93.48% covered (success)
93.48%
43 / 46
77.78% covered (warning)
77.78%
7 / 9
17.08
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
94.12% covered (success)
94.12%
16 / 17
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%
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%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Common\Actions;
3
4use Apie\Common\Other\LockUtil;
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\Entities\EntityInterface;
15use Apie\Core\Exceptions\InvalidTypeException;
16use Apie\Core\IdentifierUtils;
17use Apie\Core\Lists\PermissionList;
18use Apie\Core\Lists\StringList;
19use Apie\Core\Permissions\PermissionInterface;
20use Apie\Core\Permissions\RequiresPermissionsInterface;
21use Apie\Core\Utils\EntityUtils;
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        $lock = LockUtil::createLock(
65            $context,
66            [ContextConstants::BOUNDED_CONTEXT_ID, ContextConstants::RESOURCE_NAME, ContextConstants::RESOURCE_ID]
67        );
68        try {
69            $result = $this->apieFacade->find(
70                IdentifierUtils::idStringToIdentifier($id, $context),
71                new BoundedContextId($context->getContext(ContextConstants::BOUNDED_CONTEXT_ID))
72            );
73            $context = $context->withContext(ContextConstants::RESOURCE, $result);
74            $context->withContext(ContextConstants::APIE_ACTION, __CLASS__)->checkAuthorization();
75        } finally {
76            $lock->release();
77        }
78        return ActionResponse::createRunSuccess($this->apieFacade, $context, $result, $result);
79    }
80
81    /**
82     * @return ReflectionClass<EntityInterface>
83     */
84    public static function getInputType(ReflectionClass $class): ReflectionClass
85    {
86        return $class;
87    }
88
89    /**
90     * @return ReflectionClass<EntityInterface>
91     */
92    public static function getOutputType(ReflectionClass $class): ReflectionClass
93    {
94        return $class;
95    }
96
97    public static function getPossibleActionResponseStatuses(): ActionResponseStatusList
98    {
99        return new ActionResponseStatusList([
100            ActionResponseStatus::SUCCESS,
101            ActionResponseStatus::AUTHORIZATION_ERROR,
102            ActionResponseStatus::NOT_FOUND
103        ]);
104    }
105
106    public static function getDescription(ReflectionClass $class): string
107    {
108        $description = 'Gets a resource of ' . $class->getShortName() . ' with a specific id';
109        foreach ($class->getAttributes(Description::class) as $attribute) {
110            $description .= '. ' . $attribute->newInstance()->description;
111        }
112
113        return $description;
114    }
115    
116    public static function getTags(ReflectionClass $class): StringList
117    {
118        return new StringList([$class->getShortName(), 'resource']);
119    }
120
121    public static function getRouteAttributes(ReflectionClass $class): array
122    {
123        return [
124            ContextConstants::GET_OBJECT => true,
125            ContextConstants::RESOURCE_NAME => $class->name,
126        ];
127    }
128}