Skip to content

Commit

Permalink
Applied coding standards
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Richer [email protected] <[email protected]>
  • Loading branch information
visto9259 committed Aug 20, 2024
1 parent f5a3735 commit 26f2fd4
Show file tree
Hide file tree
Showing 56 changed files with 438 additions and 514 deletions.
8 changes: 4 additions & 4 deletions src/Assertion/AssertionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
/**
* Plugin manager to create assertions
*
* @author Aeneas Rekkas
* @licence MIT
* @deprecated Use AssertionPluginManager
*
* @licence MIT
* @codeCoverageIgnore
*/
final class AssertionContainer extends AbstractPluginManager implements AssertionContainerInterface
{
protected $instanceOf = AssertionInterface::class;

/**
* @inheritdoc
* @inheritDoc
*/
public function get($name, array $options = null): AssertionInterface
public function get($name, ?array $options = null): AssertionInterface
{
return parent::get($name);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Assertion/AssertionContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
/**
* Factory to create a assertion plugin manager
*
* @author Aeneas Rekkas
* @licence MIT
* @deprecated Use AssertionPluginManagerFactory
*
* @licence MIT
* @codeCoverageIgnore
*/
class AssertionContainerFactory
Expand Down
5 changes: 1 addition & 4 deletions src/Assertion/AssertionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@
/**
* Interface that you can implement for dynamic assertions
*
* @author Michaël Gallego <[email protected]>
* @author Aeneas Rekkas
* @author Daniel Gimenes <[email protected]>
* @licence MIT
*/
interface AssertionInterface
{
public function assert(
PermissionInterface|string $permission,
IdentityInterface $identity = null,
?IdentityInterface $identity = null,
mixed $context = null
): bool;
}
19 changes: 9 additions & 10 deletions src/Assertion/AssertionPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,31 @@

use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

use function sprintf;

class AssertionPluginManager extends AbstractPluginManager implements AssertionPluginManagerInterface
{
/**
* @param mixed $instance
* @return void
*/
public function validate(mixed $instance): void
{
if ($instance instanceof AssertionInterface) {
return;
}
throw new InvalidServiceException(sprintf(
'Assertions must implement "Lmc\Rbac\Assertion\AssertionInterface", but "%s" was given',
get_class($instance)
$instance::class
));
}

/**
* @param $name
* @param string $name
* @param array|null $options
* @return AssertionInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function get($name, array $options = null): AssertionInterface
public function get($name, ?array $options = null): AssertionInterface
{
return parent::get($name, $options);
}
Expand Down
40 changes: 25 additions & 15 deletions src/Assertion/AssertionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,39 @@
use Lmc\Rbac\Identity\IdentityInterface;
use Lmc\Rbac\Permission\PermissionInterface;

use function count;
use function gettype;
use function is_array;
use function is_callable;
use function is_object;
use function is_string;
use function sprintf;

class AssertionSet implements AssertionInterface
{
/**
* Condition constants
*/
public const CONDITION_OR = 'condition_or';
public const CONDITION_OR = 'condition_or';
public const CONDITION_AND = 'condition_and';

/**
* @var array
*/
/** @var array */
private array $assertions;

private $condition = self::CONDITION_AND;
private string $condition = self::CONDITION_AND;

/**
* @param AssertionPluginManagerInterface $assertionPluginManager
* @param array $assertions
*/
public function __construct(
private readonly AssertionPluginManagerInterface $assertionPluginManager,
array $assertions
) {
if (isset($assertions['condition'])) {
if ($assertions['condition'] !== AssertionSet::CONDITION_AND
&& $assertions['condition'] !== AssertionSet::CONDITION_OR) {
if (
$assertions['condition'] !== self::CONDITION_AND
&& $assertions['condition'] !== self::CONDITION_OR
) {
throw new Exception\InvalidArgumentException('Invalid assertion condition given.');
}

Expand All @@ -62,8 +69,11 @@ public function __construct(
$this->assertions = $assertions;
}

public function assert(PermissionInterface|string $permission, IdentityInterface $identity = null, $context = null): bool
{
public function assert(
PermissionInterface|string $permission,
?IdentityInterface $identity = null,
mixed $context = null
): bool {
if (empty($this->assertions)) {
return false;
}
Expand All @@ -85,23 +95,23 @@ public function assert(PermissionInterface|string $permission, IdentityInterface
break;
case is_array($assertion):
$this->assertions[$index] = $assertion = new AssertionSet($this->assertionPluginManager, $assertion);
$asserted = $assertion->assert($permission, $identity, $context);
$asserted = $assertion->assert($permission, $identity, $context);
break;
default:
throw new Exception\InvalidArgumentException(sprintf(
'Assertion must be callable, string, array or implement Lmc\Rbac\Assertion\AssertionInterface, "%s" given',
is_object($assertion) ? get_class($assertion) : gettype($assertion)
is_object($assertion) ? $assertion::class : gettype($assertion)
));
}

switch ($this->condition) {
case AssertionSet::CONDITION_AND:
case self::CONDITION_AND:
if (false === $asserted) {
return false;
}

break;
case AssertionSet::CONDITION_OR:
case self::CONDITION_OR:
if (true === $asserted) {
return true;
}
Expand All @@ -111,7 +121,7 @@ public function assert(PermissionInterface|string $permission, IdentityInterface
$assertedCount++;
}

if (AssertionSet::CONDITION_AND === $this->condition && count($this->assertions) === $assertedCount) {
if (self::CONDITION_AND === $this->condition && count($this->assertions) === $assertedCount) {
return true;
}

Expand Down
36 changes: 26 additions & 10 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@

namespace Lmc\Rbac;

use Laminas\ServiceManager\Factory\InvokableFactory;
use Lmc\Rbac\Assertion\AssertionPluginManager;
use Lmc\Rbac\Assertion\AssertionPluginManagerFactory;
use Lmc\Rbac\Assertion\AssertionPluginManagerInterface;
use Lmc\Rbac\Options\ModuleOptions;
use Lmc\Rbac\Options\ModuleOptionsFactory;
use Lmc\Rbac\Rbac;
use Lmc\Rbac\Role\InMemoryRoleProvider;
use Lmc\Rbac\Role\InMemoryRoleProviderFactory;
use Lmc\Rbac\Role\ObjectRepositoryRoleProvider;
use Lmc\Rbac\Role\ObjectRepositoryRoleProviderFactory;
use Lmc\Rbac\Service\AuthorizationServiceFactory;
use Lmc\Rbac\Service\AuthorizationServiceInterface;
use Lmc\Rbac\Service\RoleServiceFactory;
use Lmc\Rbac\Service\RoleServiceInterface;

/**
* The configuration provider for the LmcRbac module
*
Expand All @@ -32,24 +48,24 @@ public function __invoke(): array
{
return [
'dependencies' => $this->getDependencyConfig(),
'lmc_rbac' => $this->getModuleConfig(),
'lmc_rbac' => $this->getModuleConfig(),
];
}

public function getDependencyConfig(): array
{
return [
'aliases' => [
\Lmc\Rbac\Assertion\AssertionPluginManagerInterface::class => \Lmc\Rbac\Assertion\AssertionPluginManager::class,
'aliases' => [
AssertionPluginManagerInterface::class => AssertionPluginManager::class,
],
'factories' => [
\Lmc\Rbac\Assertion\AssertionPluginManager::class => \Lmc\Rbac\Assertion\AssertionPluginManagerFactory::class,
\Lmc\Rbac\Options\ModuleOptions::class => \Lmc\Rbac\Options\ModuleOptionsFactory::class,
\Lmc\Rbac\Role\InMemoryRoleProvider::class => \Lmc\Rbac\Role\InMemoryRoleProviderFactory::class,
\Lmc\Rbac\Role\ObjectRepositoryRoleProvider::class => \Lmc\Rbac\Role\ObjectRepositoryRoleProviderFactory::class,
\Lmc\Rbac\Service\AuthorizationServiceInterface::class => \Lmc\Rbac\Service\AuthorizationServiceFactory::class,
\Lmc\Rbac\Service\RoleServiceInterface::class => \Lmc\Rbac\Service\RoleServiceFactory::class,
\Lmc\Rbac\Rbac::class => \Laminas\ServiceManager\Factory\InvokableFactory::class,
AssertionPluginManager::class => AssertionPluginManagerFactory::class,
ModuleOptions::class => ModuleOptionsFactory::class,
InMemoryRoleProvider::class => InMemoryRoleProviderFactory::class,
ObjectRepositoryRoleProvider::class => ObjectRepositoryRoleProviderFactory::class,
AuthorizationServiceInterface::class => AuthorizationServiceFactory::class,
RoleServiceInterface::class => RoleServiceFactory::class,
Rbac::class => InvokableFactory::class,
],
];
}
Expand Down
2 changes: 0 additions & 2 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
/**
* Base exception interface for LmcRbac
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
interface ExceptionInterface
{
Expand Down
3 changes: 0 additions & 3 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

/**
* InvalidArgumentException
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class InvalidArgumentException extends BaseInvalidArgumentException implements ExceptionInterface
{
Expand Down
7 changes: 1 addition & 6 deletions src/Exception/RoleNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,9 @@

/**
* Exception that is thrown when a role cannot be found (for instance from a provider)
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class RoleNotFoundException extends BaseRuntimeException implements ExceptionInterface
{
/**
* @var string
*/
/** @var string */
protected $message = 'No role could be found';
}
2 changes: 0 additions & 2 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
/**
* RuntimeException
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class RuntimeException extends BaseRuntimeException implements ExceptionInterface
{
Expand Down
2 changes: 0 additions & 2 deletions src/Identity/IdentityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
/**
* Interface for an identity
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
interface IdentityInterface
{
Expand Down
3 changes: 1 addition & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ final class Module
public function getConfig(): array
{
$provider = new ConfigProvider();

return [
'service_manager' => $provider->getDependencyConfig(),
'lmc_rbac' => $provider->getModuleConfig(),
'lmc_rbac' => $provider->getModuleConfig(),
];
}
}
13 changes: 2 additions & 11 deletions src/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@
namespace Lmc\Rbac\Options;

use Laminas\Stdlib\AbstractOptions;
use Lmc\Rbac\Role\InMemoryRoleProvider;

/**
* Options for LmcRbac module
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class ModuleOptions extends AbstractOptions
{
/**
* Guest role (used when no identity is found)
*
* @var string
*/
protected string $guestRole = 'guest';

Expand All @@ -52,7 +49,7 @@ class ModuleOptions extends AbstractOptions
* @var array
*/
protected array $roleProvider = [
'Lmc\Rbac\Role\InMemoryRoleProvider' => [],
InMemoryRoleProvider::class => [],
];

/**
Expand All @@ -71,7 +68,6 @@ public function __construct($options = null)
* Set the assertions options
*
* @param array $assertionMap
* @return void
*/
public function setAssertionMap(array $assertionMap): void
{
Expand All @@ -90,9 +86,6 @@ public function getAssertionMap(): array

/**
* Set the guest role (used when no identity is found)
*
* @param string $guestRole
* @return void
*/
public function setGuestRole(string $guestRole): void
{
Expand All @@ -101,8 +94,6 @@ public function setGuestRole(string $guestRole): void

/**
* Get the guest role (used when no identity is found)
*
* @return string
*/
public function getGuestRole(): string
{
Expand Down
4 changes: 0 additions & 4 deletions src/Options/ModuleOptionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@

namespace Lmc\Rbac\Options;

use Lmc\Rbac\Options\ModuleOptions;
use Psr\Container\ContainerInterface;

/**
* Factory for the module options
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class ModuleOptionsFactory
{
Expand Down
Loading

0 comments on commit 26f2fd4

Please sign in to comment.