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
DropdownOptionsAction
96.88% covered (success)
96.88%
31 / 32
88.89% covered (warning)
88.89%
8 / 9
15
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%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
6.17
 __invoke
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 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%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\CmsApiDropdownOption\Actions;
3
4use Apie\CmsApiDropdownOption\DropdownOptionProvider\DropdownOptionProviderInterface;
5use Apie\CmsApiDropdownOption\Dtos\PartialInput;
6use Apie\CmsApiDropdownOption\Lists\DropdownOptionList;
7use Apie\Core\Actions\ActionInterface;
8use Apie\Core\Actions\ActionResponse;
9use Apie\Core\Actions\ActionResponseStatus;
10use Apie\Core\Actions\ActionResponseStatusList;
11use Apie\Core\Actions\ApieFacadeInterface;
12use Apie\Core\Context\ApieContext;
13use Apie\Core\ContextConstants;
14use Apie\Core\Lists\StringList;
15use Apie\Core\Utils\EntityUtils;
16use LogicException;
17use ReflectionClass;
18use ReflectionMethod;
19
20class DropdownOptionsAction implements ActionInterface
21{
22    public function __construct(private readonly ApieFacadeInterface $apieFacade)
23    {
24    }
25
26    public static function isAuthorized(ApieContext $context, bool $runtimeChecks, bool $throwError = false): bool
27    {
28        if ($context->hasContext(ContextConstants::RESOURCE_NAME)) {
29            $refl = new ReflectionClass($context->getContext(ContextConstants::RESOURCE_NAME, $throwError));
30            if (EntityUtils::isPolymorphicEntity($refl) && $runtimeChecks && $context->hasContext(ContextConstants::RESOURCE)) {
31                $refl = new ReflectionClass($context->getContext(ContextConstants::RESOURCE, $throwError));
32            }
33        } else {
34            $refl = new ReflectionClass($context->getContext(ContextConstants::SERVICE_CLASS));
35        }
36        return $context->appliesToContext($refl, $runtimeChecks, $throwError ? new LogicException('Class is not authorized') : null);
37    }
38
39    public function __invoke(ApieContext $context, array $rawContents): ActionResponse
40    {
41        $dropdownOptionProvider = $context->getContext(DropdownOptionProviderInterface::class);
42        assert($dropdownOptionProvider instanceof DropdownOptionProviderInterface);
43        $result = $dropdownOptionProvider->getList($context, $rawContents['input'] ?? '');
44        return ActionResponse::createRunSuccess(
45            $this->apieFacade,
46            $context,
47            $result,
48            $result
49        );
50    }
51
52    public static function getInputType(ReflectionClass $class): ReflectionClass
53    {
54        return new ReflectionClass(PartialInput::class);
55    }
56
57    /**
58     * @return ReflectionClass<DropdownOptionList>
59     */
60    public static function getOutputType(ReflectionClass $class): ReflectionClass
61    {
62        return new ReflectionClass(DropdownOptionList::class);
63    }
64
65    public static function getPossibleActionResponseStatuses(): ActionResponseStatusList
66    {
67        return new ActionResponseStatusList([
68            ActionResponseStatus::SUCCESS
69        ]);
70    }
71
72    public static function getDescription(ReflectionClass $class): string
73    {
74        return 'Returns dropdown options for a specific input';
75    }
76
77    public static function getTags(ReflectionClass $class): StringList
78    {
79        return new StringList([$class->getShortName()]);
80    }
81
82    public static function getRouteAttributes(ReflectionClass $class, ?ReflectionMethod $method = null): array
83    {
84        if ($method !== null) {
85            return [
86                ContextConstants::SERVICE_CLASS => $class->name,
87                ContextConstants::METHOD_NAME => $method->name,
88            ];
89        }
90        return [
91            ContextConstants::GET_OBJECT => true,
92            ContextConstants::RESOURCE_NAME => $class->name,
93        ];
94    }
95}