Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| ModifyResourceAction | |
100.00% |
13 / 13 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createForEntity | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUrl | |
100.00% |
4 / 4 |
|
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\ModifyResourceActionDefinition; |
| 6 | use Apie\Core\Context\ApieContext; |
| 7 | use Apie\Core\Entities\EntityInterface; |
| 8 | use Apie\Core\Metadata\MetadataFactory; |
| 9 | use Apie\HtmlBuilders\Configuration\CurrentConfiguration; |
| 10 | use Apie\HtmlBuilders\Enums\ActionDefinitionVariant; |
| 11 | use Apie\HtmlBuilders\Utils\FieldUtils; |
| 12 | use ReflectionClass; |
| 13 | |
| 14 | class ModifyResourceAction implements SingleResourceActionInterface |
| 15 | { |
| 16 | public function __construct( |
| 17 | private readonly EntityInterface $entity, |
| 18 | private readonly ModifyResourceActionDefinition $actionDefinition |
| 19 | ) { |
| 20 | } |
| 21 | |
| 22 | public static function createForEntity(EntityInterface $entity, ReflectionClass $entityClass, ActionDefinitionInterface $actionDefinition): ?self |
| 23 | { |
| 24 | if ($actionDefinition instanceof ModifyResourceActionDefinition) { |
| 25 | return $actionDefinition->getResourceName()->name === $entityClass->name ? new self($entity, $actionDefinition) : null; |
| 26 | } |
| 27 | |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | public function getName(): string |
| 32 | { |
| 33 | return 'Edit'; |
| 34 | } |
| 35 | |
| 36 | public function getUrl(CurrentConfiguration $currentConfiguration): string |
| 37 | { |
| 38 | $id = $this->entity->getId()->toNative(); |
| 39 | return $currentConfiguration->getContextUrl( |
| 40 | 'resource/edit/' . $this->actionDefinition->getResourceName()->getShortName() . '/' . $id |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public function getVariant(): ActionDefinitionVariant |
| 45 | { |
| 46 | return ActionDefinitionVariant::PRIMARY; |
| 47 | } |
| 48 | |
| 49 | public function isSmallPage(?ApieContext $apieContext = null): bool |
| 50 | { |
| 51 | $apieContext ??= new ApieContext(); |
| 52 | $metadata = MetadataFactory::getModificationMetadata(new ReflectionClass($this->entity), $apieContext); |
| 53 | return FieldUtils::countAmountOfFields($metadata, $apieContext) < 4; |
| 54 | } |
| 55 | } |