mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 14:07:25 +01:00
Implemented RightTransformerService
This commit is contained in:
parent
c995778264
commit
412b26d639
@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\RightManagement;
|
||||
|
||||
use App\Domain\RequestManagement\Right\RequestedRight;
|
||||
use App\Entity\Meta\RightInterface;
|
||||
use App\Domain\RequestManagement\Right\RequestedRightInterface;
|
||||
use App\Entity\Meta\Right;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
final class RightTransformerService implements RightTransformerServiceInterface
|
||||
{
|
||||
const SET_PREFIX = 'set';
|
||||
|
||||
const GET_PREFIX = 'get';
|
||||
|
||||
const HAS_PREFIX = 'has';
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getAttributByMethodName(string $method): string
|
||||
{
|
||||
return substr($method, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isNameSetter(string $name): bool
|
||||
{
|
||||
return self::GET_PREFIX === substr($name, 0, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RightInterface|RequestedRightInterface $right
|
||||
*
|
||||
* @return array|string[]
|
||||
*/
|
||||
private function getAllAttributes($right): array
|
||||
{
|
||||
$attributes = [];
|
||||
$reflection = new \ReflectionClass($right);
|
||||
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||
/**
|
||||
* @var \ReflectionMethod
|
||||
*/
|
||||
foreach ($methods as $method) {
|
||||
$name = $method->getName();
|
||||
if ($this->isNameSetter($name)) {
|
||||
$attributes[] = $this->getAttributByMethodName($name);
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RightInterface $right
|
||||
* @param RequestedRightInterface $requestedRight
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getAttributesExistInBoth(RightInterface $right, RequestedRightInterface $requestedRight): array
|
||||
{
|
||||
$attributes = [];
|
||||
$requestedRightAttributes = $this->getAllAttributes($requestedRight);
|
||||
$rightAttributes = $this->getAllAttributes($right);
|
||||
foreach ($requestedRightAttributes as $requestedRightAttribut) {
|
||||
if (in_array($requestedRightAttribut, $rightAttributes)) {
|
||||
$attributes[] = $requestedRightAttribut;
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param object $object
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasMethod(string $name, object $object): bool
|
||||
{
|
||||
$reflection = new \ReflectionClass($object);
|
||||
|
||||
return $reflection->hasMethod($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prefix
|
||||
* @param string $attribute
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function createMethod(string $prefix, string $attribute): string
|
||||
{
|
||||
return $prefix.ucfirst($attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param object $object
|
||||
*/
|
||||
private function isAttributeGetPossible(string $attribute, object $object)
|
||||
{
|
||||
$has = $this->createMethod(self::HAS_PREFIX, $attribute);
|
||||
if ($this->hasMethod($has, $object)) {
|
||||
$get = $this->createMethod(self::GET_PREFIX, $attribute);
|
||||
|
||||
return $object->{$get}();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \App\Domain\RightManagement\RightTransformerServiceInterface::transform()
|
||||
*/
|
||||
public function transform(RequestedRight $requestedRight): RightInterface
|
||||
{
|
||||
$right = new Right();
|
||||
$attributes = $this->getAttributesExistInBoth($right, $requestedRight);
|
||||
foreach ($attributes as $attribute) {
|
||||
if ($this->isAttributeGetPossible($attribute, $requestedRight)) {
|
||||
$get = $this->createMethod(self::GET_PREFIX, $attribute);
|
||||
$set = $this->createMethod(self::SET_PREFIX, $attribute);
|
||||
$origine = $requestedRight->{$get}();
|
||||
$right->{$set}($origine);
|
||||
}
|
||||
}
|
||||
|
||||
return $right;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\RightManagement;
|
||||
|
||||
use App\Entity\Meta\RightInterface;
|
||||
use App\Domain\RequestManagement\Right\RequestedRight;
|
||||
|
||||
/**
|
||||
* Allows to transform an Requested Right to a Entity Right.
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
interface RightTransformerServiceInterface
|
||||
{
|
||||
/**
|
||||
* @param RequestedRight $requestedRight
|
||||
*
|
||||
* @return RightInterface
|
||||
*/
|
||||
public function transform(RequestedRight $requestedRight): RightInterface;
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace tests\Unit\Domain\RightManagement;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use App\Domain\RequestManagement\Right\RequestedRight;
|
||||
use App\DBAL\Types\Meta\Right\CRUDType;
|
||||
use App\DBAL\Types\Meta\Right\LayerType;
|
||||
use App\Entity\Source\SourceInterface;
|
||||
use App\Domain\RightManagement\RightTransformerService;
|
||||
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
class RightTransformerServiceTest extends TestCase
|
||||
{
|
||||
public function testTransformation(): void
|
||||
{
|
||||
$source = $this->createMock(SourceInterface::class);
|
||||
$crud = CRUDType::READ;
|
||||
$layer = LayerType::SOURCE;
|
||||
$reciever = $this->createMock(SourceInterface::class);
|
||||
$requestedEntity = $this->createMock(RequestedEntityInterface::class);
|
||||
$requestedEntity->method('hasId')->willReturn(123);
|
||||
$requestedEntity->method('getEntity')->willReturn($source);
|
||||
$requestedEntity->method('hasRequestedRight')->willReturn(true);
|
||||
$requestedRight = new RequestedRight();
|
||||
$requestedRight->setCrud($crud);
|
||||
$requestedRight->setLayer($layer);
|
||||
$requestedRight->setRequestedEntity($requestedEntity);
|
||||
$requestedRight->setReciever($reciever);
|
||||
$transformer = new RightTransformerService();
|
||||
$right = $transformer->transform($requestedRight);
|
||||
$this->assertEquals($crud, $right->getCrud());
|
||||
$this->assertEquals($layer, $right->getLayer());
|
||||
$this->assertEquals($reciever, $right->getReciever());
|
||||
$this->assertEquals($source, $right->getSource());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user