Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Tubach committed Feb 13, 2024
1 parent a3b60ac commit 7080100
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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();
}

}
39 changes: 1 addition & 38 deletions Civi/RemoteTools/EntityProfile/ReadOnlyRemoteEntityProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 7080100

Please sign in to comment.