Skip to content

Commit

Permalink
Beautify code
Browse files Browse the repository at this point in the history
  • Loading branch information
visto9259 committed Aug 16, 2024
1 parent 4fa73a4 commit ebbd97f
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 166 deletions.
17 changes: 12 additions & 5 deletions src/Collector/RbacCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public function collect(MvcEvent $mvcEvent): void
*/
private function collectOptions(ModuleOptions $moduleOptions): void
{
$this->collectedOptions = ['guest_role' => $moduleOptions->getGuestRole(), 'protection_policy' => $moduleOptions->getProtectionPolicy()];
$this->collectedOptions = [
'guest_role' => $moduleOptions->getGuestRole(),
'protection_policy' => $moduleOptions->getProtectionPolicy(),
];
}

/**
Expand Down Expand Up @@ -126,7 +129,10 @@ private function collectIdentityRolesAndPermissions(RoleService $roleService): v
if (empty($role->getChildren())) {
$this->collectedRoles[] = $roleName;
} else {
$iteratorIterator = new RecursiveIteratorIterator(new RecursiveRoleIterator($role->getChildren()), RecursiveIteratorIterator::SELF_FIRST);
$iteratorIterator = new RecursiveIteratorIterator(
new RecursiveRoleIterator($role->getChildren()),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iteratorIterator as $childRole) {
$this->collectedRoles[$roleName][] = $childRole->getName();
$this->collectPermissions($childRole);
Expand All @@ -147,8 +153,8 @@ private function collectPermissions(RoleInterface $role): void
if (method_exists($role, 'getPermissions')) {
$permissions = $role->getPermissions();
} else {
$reflectionProperty = new ReflectionProperty($role, 'permissions');
$permissions = $reflectionProperty->getValue($role);
$reflectionProperty = new ReflectionProperty($role, 'permissions');
$permissions = $reflectionProperty->getValue($role);
}

if ($permissions instanceof Traversable) {
Expand All @@ -166,13 +172,14 @@ private function collectPermissions(RoleInterface $role): void
*/
public function getCollection(): array
{
// Start collect all the data we need!
return [
'guards' => $this->collectedGuards,
'roles' => $this->collectedRoles,
'permissions' => $this->collectedPermissions,
'options' => $this->collectedOptions,
];
} // Start collect all the data we need!
}

// Gather the permissions for the given role. We have to use reflection as
// the RoleInterface does not have "getPermissions" method
Expand Down
19 changes: 12 additions & 7 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?php

declare(strict_types=1);

namespace LmcRbac\Mvc\DevTools;

use Laminas\ServiceManager\Factory\InvokableFactory;
use LmcRbac\Mvc\DevTools\Collector\RbacCollector;

class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
'view_manager' => $this->getViewManagerConfig(),
'dependencies' => $this->getDependencies(),
'view_manager' => $this->getViewManagerConfig(),
'laminas-developer-tools' => $this->getLaminasDeveloperToolsConfig(),
];
}
Expand All @@ -17,7 +22,7 @@ public function getDependencies(): array
{
return [
'factories' => [
\LmcRbac\Mvc\DevTools\Collector\RbacCollector::class => \Laminas\ServiceManager\Factory\InvokableFactory::class,
RbacCollector::class => InvokableFactory::class,
],
];
}
Expand All @@ -26,8 +31,8 @@ public function getViewManagerConfig(): array
{
return [
'template_map' => [
'laminas-developer-tools/toolbar/lmc-rbac' => __DIR__ . '/../view/laminas-developer-tools/toolbar/lmc-rbac.phtml'
]
'laminas-developer-tools/toolbar/lmc-rbac' => __DIR__ . '/../view/laminas-developer-tools/toolbar/lmc-rbac.phtml',
],
];
}

Expand All @@ -36,10 +41,10 @@ public function getLaminasDeveloperToolsConfig(): array
return [
'profiler' => [
'collectors' => [
'lmc_rbac' => \LmcRbac\Mvc\DevTools\Collector\RbacCollector::class,
'lmc_rbac' => RbacCollector::class,
],
],
'toolbar' => [
'toolbar' => [
'entries' => [
'lmc_rbac' => 'laminas-developer-tools/toolbar/lmc-rbac',
],
Expand Down
7 changes: 4 additions & 3 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php

declare(strict_types=1);

namespace LmcRbac\Mvc\DevTools;

class Module
{

public function getConfig(): array
{
$configProvider = new ConfigProvider();
return [
'service_manager' => $configProvider->getDependencies(),
'view_manager' => $configProvider->getViewManagerConfig(),
'service_manager' => $configProvider->getDependencies(),
'view_manager' => $configProvider->getViewManagerConfig(),
'laminas-developer-tools' => $configProvider->getLaminasDeveloperToolsConfig(),
];
}
Expand Down
10 changes: 6 additions & 4 deletions test/Asset/MockRoleWithPermissionMethod.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

declare(strict_types=1);

namespace LmcRbac\Mvc\DevToolsTest\Asset;

use Lmc\Rbac\Permission\PermissionInterface;
use Lmc\Rbac\Role\RoleInterface;

class MockRoleWithPermissionMethod implements RoleInterface
Expand All @@ -15,6 +18,7 @@ public function getName(): string
{
return 'role-with-permission-method';
}

public function hasPermission($permission): bool
{
return false;
Expand All @@ -23,17 +27,15 @@ public function hasPermission($permission): bool
/**
* Add permission to the role.
*/
public function addPermission(string|\Lmc\Rbac\Permission\PermissionInterface $permission): void
public function addPermission(string|PermissionInterface $permission): void
{

}

/**
* Add a child.
*/
public function addChild(RoleInterface $role): void
{

}

/**
Expand All @@ -46,7 +48,7 @@ public function getChildren(): iterable
return [];
}

public function hasChildren() : bool
public function hasChildren(): bool
{
return false;
}
Expand Down
20 changes: 12 additions & 8 deletions test/Asset/MockRoleWithPermissionProperty.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

declare(strict_types=1);

namespace LmcRbac\Mvc\DevToolsTest\Asset;

use Lmc\Rbac\Permission\PermissionInterface;
use Lmc\Rbac\Role\RoleInterface;

class MockRoleWithPermissionProperty implements RoleInterface
Expand All @@ -13,36 +16,37 @@ public function getName(): string
return 'role-with-permission-property';
}

public function hasPermission($permission): bool
public function hasPermission(string|PermissionInterface $permission): bool
{
return false;
}

public function addPermission(string|\Lmc\Rbac\Permission\PermissionInterface $permission): void{
return;
public function addPermission(string|PermissionInterface $permission): void
{
}

/**
* Add a child.
*/
public function addChild(RoleInterface $role): void{
return;
public function addChild(RoleInterface $role): void
{
}

/**
* Get the children roles.
*
* @return RoleInterface[]
*/
public function getChildren(): iterable{
public function getChildren(): iterable
{
return [];
}

/**
* Get the children roles.
*
*/
public function hasChildren(): bool
{
return false;
}}
}
}
24 changes: 13 additions & 11 deletions test/Asset/MockRoleWithPermissionTraversable.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
<?php

declare(strict_types=1);

namespace LmcRbac\Mvc\DevToolsTest\Asset;

use ArrayObject;
use Lmc\Rbac\Permission\PermissionInterface;
use Lmc\Rbac\Role\RoleInterface;


class MockRoleWithPermissionTraversable implements RoleInterface
{
public function getPermissions(): \ArrayObject
public function getPermissions(): ArrayObject
{
return new \ArrayObject(['permission-method-a', 'permission-method-b']);
return new ArrayObject(['permission-method-a', 'permission-method-b']);
}

public function getName(): string
{
return 'role-with-permission-traversable';
}
public function hasPermission($permission): bool

public function hasPermission(string|PermissionInterface $permission): bool
{
return false;
}

public function addPermission(string|\Lmc\Rbac\Permission\PermissionInterface $permission): void{
return;
public function addPermission(string|PermissionInterface $permission): void
{
}

/**
* Add a child.
*/
public function addChild(RoleInterface $role): void{
return;
public function addChild(RoleInterface $role): void
{
}

/**
* Get the children roles.
*
* @return RoleInterface[]
*/
public function getChildren(): iterable{
public function getChildren(): iterable
{
return [];
}

/**
* Get the children roles.
*
* @return bool
*/
public function hasChildren(): bool
{
Expand Down
7 changes: 2 additions & 5 deletions test/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
* and is licensed under the MIT license.
*/

use Laminas\Mvc\Application;
use LmcRbac\Mvc\DevToolsTest\Util\ServiceManagerFactory;

ini_set('error_reporting', E_ALL);

$files = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php'
__DIR__ . '/../../../autoload.php',
];

foreach ($files as $file) {
Expand All @@ -37,8 +36,6 @@
throw new RuntimeException('vendor/autoload.php could not be found. Did you install via composer?');
}

//$loader->add('LmcRbac\\Mvc\\DevToolsTest\\', __DIR__);

$config = require __DIR__ . '/TestConfiguration.php';
ServiceManagerFactory::setApplicationConfig($config);
unset($files, $file, $loader, $configFiles, $configFile, $config);
unset($files, $file, $loader, $config);
Loading

0 comments on commit ebbd97f

Please sign in to comment.