diff --git a/Civi/RemoteTools/EntityProfile/AbstractReadOnlyRemoteEntityProfile.php b/Civi/RemoteTools/EntityProfile/AbstractReadOnlyRemoteEntityProfile.php new file mode 100644 index 0000000..4ce9f76 --- /dev/null +++ b/Civi/RemoteTools/EntityProfile/AbstractReadOnlyRemoteEntityProfile.php @@ -0,0 +1,71 @@ +. + */ + +declare(strict_types = 1); + +namespace Civi\RemoteTools\EntityProfile; + +use Civi\RemoteTools\EntityProfile\Authorization\GrantResult; +use Civi\RemoteTools\Form\FormSpec\FormSpec; + +/** + * Abstract implementation for read only access that assumes that internal and + * external fields are the same. (getRemoteFields() and convertToRemoteValues() + * can be reimplemented if necessary.) + * + * @codeCoverageIgnore + * + * @api + */ +abstract class AbstractReadOnlyRemoteEntityProfile extends AbstractRemoteEntityProfile { + + /** + * @inheritDoc + */ + public function getCreateFormSpec(array $arguments, array $entityFields, ?int $contactId): FormSpec { + throw new \BadMethodCallException('Creating entities is not supported'); + } + + /** + * @inheritDoc + */ + public function getUpdateFormSpec(array $entityValues, array $entityFields, ?int $contactId): FormSpec { + throw new \BadMethodCallException('Updating entities is not supported'); + } + + /** + * @inheritDoc + */ + public function isCreateGranted(array $arguments, ?int $contactId): GrantResult { + return GrantResult::newDenied(); + } + + /** + * @inheritDoc + */ + public function isDeleteGranted(array $entityValues, ?int $contactId): GrantResult { + return GrantResult::newDenied(); + } + + /** + * @inheritDoc + */ + public function isUpdateGranted(?array $entityValues, ?int $contactId): GrantResult { + return GrantResult::newDenied(); + } + +} diff --git a/Civi/RemoteTools/EntityProfile/ReadOnlyRemoteEntityProfile.php b/Civi/RemoteTools/EntityProfile/ReadOnlyRemoteEntityProfile.php index 120a156..406bfbd 100644 --- a/Civi/RemoteTools/EntityProfile/ReadOnlyRemoteEntityProfile.php +++ b/Civi/RemoteTools/EntityProfile/ReadOnlyRemoteEntityProfile.php @@ -20,15 +20,13 @@ namespace Civi\RemoteTools\EntityProfile; use Civi\RemoteTools\Api4\Query\ConditionInterface; -use Civi\RemoteTools\EntityProfile\Authorization\GrantResult; -use Civi\RemoteTools\Form\FormSpec\FormSpec; /** * @codeCoverageIgnore * * @api */ -class ReadOnlyRemoteEntityProfile extends AbstractRemoteEntityProfile { +class ReadOnlyRemoteEntityProfile extends AbstractReadOnlyRemoteEntityProfile { private string $entityName; @@ -74,39 +72,4 @@ public function getFilter(string $actionName, ?int $contactId): ?ConditionInterf return NULL; } - /** - * @inheritDoc - */ - public function getCreateFormSpec(array $arguments, array $entityFields, ?int $contactId): FormSpec { - throw new \BadMethodCallException(sprintf('Creating entities is not supported')); - } - - /** - * @inheritDoc - */ - public function getUpdateFormSpec(array $entityValues, array $entityFields, ?int $contactId): FormSpec { - throw new \BadMethodCallException(sprintf('Updating entities is not supported')); - } - - /** - * @inheritDoc - */ - public function isCreateGranted(array $arguments, ?int $contactId): GrantResult { - return GrantResult::newDenied(); - } - - /** - * @inheritDoc - */ - public function isDeleteGranted(array $entityValues, ?int $contactId): GrantResult { - return GrantResult::newDenied(); - } - - /** - * @inheritDoc - */ - public function isUpdateGranted(?array $entityValues, ?int $contactId): GrantResult { - return GrantResult::newDenied(); - } - } diff --git a/Civi/RemoteTools/EntityProfile/Traits/ConstProfileMetadataTrait.php b/Civi/RemoteTools/EntityProfile/Traits/ConstProfileMetadataTrait.php new file mode 100644 index 0000000..d13dbb2 --- /dev/null +++ b/Civi/RemoteTools/EntityProfile/Traits/ConstProfileMetadataTrait.php @@ -0,0 +1,44 @@ +. + */ + +declare(strict_types = 1); + +namespace Civi\RemoteTools\EntityProfile\Traits; + +/** + * Trait that can be used in implementations of RemoteEntityProfileInterface + * that define the metadata in constants. + * + * @see \Civi\RemoteTools\EntityProfile\RemoteEntityProfileInterface + * + * @api + */ +trait ConstProfileMetadataTrait { + + public function getEntityName(): string { + return static::ENTITY_NAME; + } + + public function getName(): string { + return static::NAME; + } + + public function getRemoteEntityName(): string { + return static::REMOTE_ENTITY_NAME; + } + +} diff --git a/tests/phpunit/Civi/RemoteTools/EntityProfile/TestRemoteGroupReadWriteEntityProfile.php b/tests/phpunit/Civi/RemoteTools/EntityProfile/TestRemoteGroupReadWriteEntityProfile.php index a7c4a16..30a1bee 100644 --- a/tests/phpunit/Civi/RemoteTools/EntityProfile/TestRemoteGroupReadWriteEntityProfile.php +++ b/tests/phpunit/Civi/RemoteTools/EntityProfile/TestRemoteGroupReadWriteEntityProfile.php @@ -20,29 +20,20 @@ namespace Civi\RemoteTools\EntityProfile; use Civi\RemoteTools\Api4\Query\ConditionInterface; +use Civi\RemoteTools\EntityProfile\Traits\ConstProfileMetadataTrait; use Civi\RemoteTools\Form\FormSpec\Field\TextField; use Civi\RemoteTools\Form\FormSpec\FormSpec; final class TestRemoteGroupReadWriteEntityProfile extends AbstractRemoteEntityProfile { + use ConstProfileMetadataTrait; + public const NAME = 'readWriteTest'; public const ENTITY_NAME = 'Group'; public const REMOTE_ENTITY_NAME = 'TestRemoteGroup'; - public function getEntityName(): string { - return self::ENTITY_NAME; - } - - public function getName(): string { - return self::NAME; - } - - public function getRemoteEntityName(): string { - return self::REMOTE_ENTITY_NAME; - } - public function getFilter(string $actionName, ?int $contactId): ?ConditionInterface { return NULL; }