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