Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.31% |
23 / 29 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| RunResourceMethodResourceAction | |
79.31% |
23 / 29 |
|
71.43% |
5 / 7 |
19.56 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createForEntity | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| createFor | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| getUrl | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
2.15 | |||
| getVariant | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
5.26 | |||
| 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\RunResourceMethodDefinition; |
| 6 | use Apie\Core\Context\ApieContext; |
| 7 | use Apie\Core\Entities\EntityInterface; |
| 8 | use Apie\HtmlBuilders\Configuration\CurrentConfiguration; |
| 9 | use Apie\HtmlBuilders\Enums\ActionDefinitionVariant; |
| 10 | use ReflectionClass; |
| 11 | |
| 12 | class RunResourceMethodResourceAction implements ResourceActionInterface, SingleResourceActionInterface |
| 13 | { |
| 14 | public function __construct( |
| 15 | private readonly RunResourceMethodDefinition $actionDefinition, |
| 16 | private readonly ?EntityInterface $entity = null, |
| 17 | ) { |
| 18 | } |
| 19 | |
| 20 | public function getName(): string |
| 21 | { |
| 22 | return $this->actionDefinition->getMethod()->name; |
| 23 | } |
| 24 | |
| 25 | public static function createForEntity(EntityInterface $entity, ReflectionClass $entityClass, ActionDefinitionInterface $actionDefinition): ?self |
| 26 | { |
| 27 | if ($actionDefinition instanceof RunResourceMethodDefinition) { |
| 28 | if ($actionDefinition->getResourceName()->name !== $entityClass->name) { |
| 29 | return null; |
| 30 | } |
| 31 | return $actionDefinition->getMethod()->isStatic() ? null : new self($actionDefinition, $entity); |
| 32 | } |
| 33 | |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | public static function createFor(ReflectionClass $entityClass, ActionDefinitionInterface $actionDefinition): ?self |
| 38 | { |
| 39 | if ($actionDefinition instanceof RunResourceMethodDefinition) { |
| 40 | if ($actionDefinition->getResourceName()->name !== $entityClass->name) { |
| 41 | return null; |
| 42 | } |
| 43 | return $actionDefinition->getMethod()->isStatic() ? new self($actionDefinition) : null; |
| 44 | } |
| 45 | |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | public function getUrl(CurrentConfiguration $currentConfiguration): string |
| 50 | { |
| 51 | $method = $this->actionDefinition->getMethod(); |
| 52 | if ($this->entity) { |
| 53 | $id = $this->entity->getId()->toNative(); |
| 54 | return $currentConfiguration->getContextUrl( |
| 55 | 'resource/action/' . $this->actionDefinition->getResourceName()->getShortName() . '/' . $id . '/' . $method->getName() |
| 56 | ); |
| 57 | } |
| 58 | return $currentConfiguration->getContextUrl( |
| 59 | 'resource/action/' . $this->actionDefinition->getResourceName()->getShortName() . '/' . $method->getName() |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | public function getVariant(): ActionDefinitionVariant |
| 64 | { |
| 65 | if (str_starts_with($this->actionDefinition->getMethod()->name, 'create')) { |
| 66 | return ActionDefinitionVariant::PRIMARY; |
| 67 | } |
| 68 | if (str_starts_with($this->actionDefinition->getMethod()->name, 'remove')) { |
| 69 | return ActionDefinitionVariant::DANGER; |
| 70 | } |
| 71 | if ($this->actionDefinition->getMethod()->isStatic()) { |
| 72 | return ActionDefinitionVariant::PLAIN; |
| 73 | } |
| 74 | return ActionDefinitionVariant::SECONDARY; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Can be used by the layout to render small pages/form in a sidebar instead. |
| 79 | */ |
| 80 | public function isSmallPage(?ApieContext $apieContext = null): bool |
| 81 | { |
| 82 | return false; |
| 83 | } |
| 84 | } |