Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
CreateResourceAction
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createFor
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUrl
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 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%
3 / 3
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\CreateResourceActionDefinition;
6use Apie\Common\ActionDefinitions\ReplaceResourceActionDefinition;
7use Apie\Core\Context\ApieContext;
8use Apie\Core\Entities\EntityInterface;
9use Apie\Core\Metadata\MetadataFactory;
10use Apie\HtmlBuilders\Configuration\CurrentConfiguration;
11use Apie\HtmlBuilders\Enums\ActionDefinitionVariant;
12use Apie\HtmlBuilders\Utils\FieldUtils;
13use ReflectionClass;
14
15class 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}