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