Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
CreateResourceAction | |
100.00% |
14 / 14 |
|
100.00% |
6 / 6 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createFor | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUrl | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getVariant | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isSmallPage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\HtmlBuilders\ResourceActions; |
3 | |
4 | use Apie\Common\ActionDefinitions\ActionDefinitionInterface; |
5 | use Apie\Common\ActionDefinitions\CreateResourceActionDefinition; |
6 | use Apie\Common\ActionDefinitions\ReplaceResourceActionDefinition; |
7 | use Apie\Core\Context\ApieContext; |
8 | use Apie\Core\Entities\EntityInterface; |
9 | use Apie\Core\Metadata\MetadataFactory; |
10 | use Apie\HtmlBuilders\Configuration\CurrentConfiguration; |
11 | use Apie\HtmlBuilders\Enums\ActionDefinitionVariant; |
12 | use Apie\HtmlBuilders\Utils\FieldUtils; |
13 | use ReflectionClass; |
14 | |
15 | class CreateResourceAction implements ResourceActionInterface |
16 | { |
17 | /** |
18 | * @param ReflectionClass<EntityInterface> $entityClass |
19 | */ |
20 | public function __construct( |
21 | private readonly ReflectionClass $entityClass, |
22 | private readonly CreateResourceActionDefinition|ReplaceResourceActionDefinition $actionDefinition |
23 | ) { |
24 | } |
25 | |
26 | public static function createFor(ReflectionClass $entityClass, ActionDefinitionInterface $actionDefinition): ?self |
27 | { |
28 | if ($actionDefinition instanceof CreateResourceActionDefinition) { |
29 | return $actionDefinition->getResourceName()->name === $entityClass->name ? new self($entityClass, $actionDefinition) : null; |
30 | } |
31 | if ($actionDefinition instanceof ReplaceResourceActionDefinition) { |
32 | return $actionDefinition->getResourceName()->name === $entityClass->name ? new self($entityClass, $actionDefinition) : null; |
33 | } |
34 | |
35 | return null; |
36 | } |
37 | |
38 | public function getName(): string |
39 | { |
40 | return 'Create ' . $this->actionDefinition->getResourceName()->getShortName(); |
41 | } |
42 | |
43 | public function getUrl(CurrentConfiguration $currentConfiguration): string |
44 | { |
45 | return $currentConfiguration->getContextUrl( |
46 | 'resource/create/' . $this->actionDefinition->getResourceName()->getShortName() |
47 | ); |
48 | } |
49 | |
50 | public function getVariant(): ActionDefinitionVariant |
51 | { |
52 | return ActionDefinitionVariant::PRIMARY; |
53 | } |
54 | |
55 | public function isSmallPage(?ApieContext $apieContext = null): bool |
56 | { |
57 | $apieContext ??= new ApieContext(); |
58 | $metadata = MetadataFactory::getCreationMetadata($this->entityClass, $apieContext); |
59 | return FieldUtils::countAmountOfFields($metadata, $apieContext) < 4; |
60 | } |
61 | } |