Implemented RightLayerCombination

This commit is contained in:
Kevin Frantz 2019-01-12 15:40:54 +01:00
parent 06540ce030
commit 04b156b8fc
4 changed files with 177 additions and 0 deletions

View File

@ -6,6 +6,8 @@ use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
/**
* @author kevinfrantz
*
* @todo Implement more layers and refactor!
*/
final class LayerType extends AbstractEnumType
{

View File

@ -0,0 +1,85 @@
<?php
namespace App\Domain\RightManagement;
use App\DBAL\Types\Meta\Right\LayerType;
use App\DBAL\Types\Meta\Right\CRUDType;
/**
* @author kevinfrantz
*/
final class RightLayerCombinationService implements RightLayerCombinationServiceInterface
{
const EXLUDED_RIGHTS_BY_LAYER = [
LayerType::HEREDITY => [
CRUDType::CREATE,
CRUDType::DELETE,
],
LayerType::LAW => [
CRUDType::CREATE,
CRUDType::DELETE,
],
];
/**
* @var array[] Array of string arrays
*/
private $possibleCombinations = [];
public function __construct()
{
$this->setCombination();
}
/**
* @param string $layer
* @param string $crud
*
* @return bool
*/
private function checkIfExcluded(string $layer, string $crud): bool
{
return array_key_exists($layer, self::EXLUDED_RIGHTS_BY_LAYER) && in_array($crud, self::EXLUDED_RIGHTS_BY_LAYER[$layer]);
}
private function setCombination(): void
{
foreach (LayerType::getChoices() as $layer) {
foreach (CRUDType::getChoices() as $crud) {
if (!array_key_exists($layer, $this->possibleCombinations)) {
$this->possibleCombinations[$layer] = [];
}
if (!$this->checkIfExcluded($layer, $crud)) {
$this->possibleCombinations[$layer][] = $crud;
}
}
}
}
/**
* {@inheritdoc}
*
* @see \App\Domain\RightManagement\RightLayerCombinationServiceInterface::getPossibleCruds()
*/
public function getPossibleCruds(string $layer): array
{
return $this->possibleCombinations[$layer];
}
/**
* {@inheritdoc}
*
* @see \App\Domain\RightManagement\RightLayerCombinationServiceInterface::getPossibleLayers()
*/
public function getPossibleLayers(string $crudType): array
{
$possibleLayers = [];
foreach ($this->possibleCombinations as $layer => $possibleCombination) {
if (in_array($crudType, $possibleCombination)) {
$possibleLayers[] = $layer;
}
}
return $possibleLayers;
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Domain\RightManagement;
use App\DBAL\Types\Meta\Right\LayerType;
use App\DBAL\Types\Meta\Right\CRUDType;
/**
* @author kevinfrantz
*/
interface RightLayerCombinationServiceInterface
{
/**
* For layer parameter see:.
*
* @see LayerType::getChoices()
*
* @param string $layer
*
* @return array The cruds which exist for a layer
*/
public function getPossibleCruds(string $layer): array;
/**
* For layer parameter see:.
*
* @see CRUDType::getChoices()
*
* @param string $crud
*
* @return array The layers which exist for a right
*/
public function getPossibleLayers(string $crudType): array;
}

View File

@ -0,0 +1,56 @@
<?php
namespace tests\Unit\Domain\RightManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\RightManagement\RightLayerCombinationServiceInterface;
use App\Domain\RightManagement\RightLayerCombinationService;
use App\DBAL\Types\Meta\Right\CRUDType;
use App\DBAL\Types\Meta\Right\LayerType;
/**
* @author kevinfrantz
*/
class RightLayerCombinationServiceTest extends TestCase
{
/**
* @var RightLayerCombinationServiceInterface
*/
public $rightLayerCombinationService;
/**
* {@inheritdoc}
*
* @see \PHPUnit\Framework\TestCase::setUp()
*/
public function setUp()
{
$this->rightLayerCombinationService = new RightLayerCombinationService();
}
public function testBySource(): void
{
foreach (CRUDType::getChoices() as $crudType) {
$layers = $this->rightLayerCombinationService->getPossibleLayers($crudType);
$this->assertContains(LayerType::SOURCE, $layers);
$sourceCruds = $this->rightLayerCombinationService->getPossibleCruds(LayerType::SOURCE);
$this->assertContains($crudType, $sourceCruds);
}
}
public function testByLaw(): void
{
foreach ([CRUDType::DELETE, CRUDType::CREATE] as $crudType) {
$layers = $this->rightLayerCombinationService->getPossibleLayers($crudType);
$this->assertNotContains(LayerType::LAW, $layers);
$sourceCruds = $this->rightLayerCombinationService->getPossibleCruds(LayerType::LAW);
$this->assertNotContains($crudType, $sourceCruds);
}
foreach ([CRUDType::READ, CRUDType::UPDATE] as $crudType) {
$layers = $this->rightLayerCombinationService->getPossibleLayers($crudType);
$this->assertContains(LayerType::LAW, $layers);
$sourceCruds = $this->rightLayerCombinationService->getPossibleCruds(LayerType::LAW);
$this->assertContains($crudType, $sourceCruds);
}
}
}