Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.89% |
16 / 18 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
GlobalMethodResourceAction | |
88.89% |
16 / 18 |
|
83.33% |
5 / 6 |
10.14 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createFor | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
5.20 | |||
getUrl | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getVariant | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isSmallPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\HtmlBuilders\ResourceActions; |
3 | |
4 | use Apie\Common\ActionDefinitions\ActionDefinitionInterface; |
5 | use Apie\Common\ActionDefinitions\RunGlobalMethodDefinition; |
6 | use Apie\Core\Context\ApieContext; |
7 | use Apie\Core\Utils\ConverterUtils; |
8 | use Apie\HtmlBuilders\Configuration\CurrentConfiguration; |
9 | use Apie\HtmlBuilders\Enums\ActionDefinitionVariant; |
10 | use Apie\TypeConverter\Exceptions\CanNotConvertObjectException; |
11 | use ReflectionClass; |
12 | |
13 | class GlobalMethodResourceAction implements ResourceActionInterface |
14 | { |
15 | public function __construct(private readonly RunGlobalMethodDefinition $actionDefinition) |
16 | { |
17 | } |
18 | |
19 | public function getName(): string |
20 | { |
21 | return $this->actionDefinition->getMethod()->name; |
22 | } |
23 | |
24 | public static function createFor(ReflectionClass $entityClass, ActionDefinitionInterface $actionDefinition): ?self |
25 | { |
26 | if ($actionDefinition instanceof RunGlobalMethodDefinition) { |
27 | $method = $actionDefinition->getMethod(); |
28 | try { |
29 | $class = ConverterUtils::toReflectionClass($method); |
30 | if ($class?->name === $entityClass->name) { |
31 | return new self($actionDefinition); |
32 | } |
33 | $class = ConverterUtils::toReflectionClass($method->getReturnType()); |
34 | if ($class?->name === $entityClass->name) { |
35 | return new self($actionDefinition); |
36 | } |
37 | } catch (CanNotConvertObjectException) { |
38 | } |
39 | } |
40 | |
41 | return null; |
42 | } |
43 | |
44 | public function getUrl(CurrentConfiguration $currentConfiguration): string |
45 | { |
46 | $method = $this->actionDefinition->getMethod(); |
47 | return $currentConfiguration->getContextUrl( |
48 | 'action/' . $method->getDeclaringClass()->getShortName() . '/' . $method->getName() |
49 | ); |
50 | } |
51 | |
52 | /** |
53 | * Used for resource action buttons. For example a remove action is 'danger'. A create resource action is 'primary' |
54 | */ |
55 | public function getVariant(): ActionDefinitionVariant |
56 | { |
57 | return ActionDefinitionVariant::SECONDARY; |
58 | } |
59 | |
60 | /** |
61 | * Can be used by the layout to render small pages/form in a sidebar instead. |
62 | */ |
63 | public function isSmallPage(?ApieContext $apieContext = null): bool |
64 | { |
65 | return false; |
66 | } |
67 | } |