Introduced class MethodPrefixType

This commit is contained in:
Kevin Frantz 2019-05-23 22:57:02 +02:00
parent 6378c47f51
commit d2b0cba30b
6 changed files with 50 additions and 37 deletions

View File

@ -3,13 +3,13 @@
namespace Infinito\Domain\DomManagement; namespace Infinito\Domain\DomManagement;
use Infinito\Entity\EntityInterface; use Infinito\Entity\EntityInterface;
use Infinito\Domain\RightManagement\RightTransformerService;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Infinito\Domain\RequestManagement\Entity\RequestedEntityServiceInterface; use Infinito\Domain\RequestManagement\Entity\RequestedEntityServiceInterface;
use Infinito\DBAL\Types\Meta\Right\LayerType; use Infinito\DBAL\Types\Meta\Right\LayerType;
use Infinito\Domain\LayerManagement\LayerInterfaceMap; use Infinito\Domain\LayerManagement\LayerInterfaceMap;
use FOS\UserBundle\Model\UserInterface; use FOS\UserBundle\Model\UserInterface;
use Infinito\Exception\Core\NotCorrectInstanceCoreException; use Infinito\Exception\Core\NotCorrectInstanceCoreException;
use Infinito\Domain\MethodManagement\MethodPrefixType;
/** /**
* This class is not ready and not tested! * This class is not ready and not tested!
@ -22,16 +22,6 @@ use Infinito\Exception\Core\NotCorrectInstanceCoreException;
*/ */
final class EntityDomService implements EntityDomServiceInterface final class EntityDomService implements EntityDomServiceInterface
{ {
/**
* @var string
*/
private const GET_METHOD = RightTransformerService::GET_PREFIX;
/**
* @var string
*/
private const HAS_METHOD = RightTransformerService::HAS_PREFIX;
/** /**
* @var RequestedEntityServiceInterface * @var RequestedEntityServiceInterface
*/ */
@ -102,11 +92,11 @@ final class EntityDomService implements EntityDomServiceInterface
*/ */
private function getPropertyValue(string $propertyName) private function getPropertyValue(string $propertyName)
{ {
$hasMethod = $this->getMethodName(self::HAS_METHOD, $propertyName); $hasMethod = $this->getMethodName(MethodPrefixType::HAS, $propertyName);
if ($this->methodExist($hasMethod) && !$this->getMethodResult($hasMethod)) { if ($this->methodExist($hasMethod) && !$this->getMethodResult($hasMethod)) {
return null; return null;
} }
$getMethod = $this->getMethodName(self::GET_METHOD, $propertyName); $getMethod = $this->getMethodName(MethodPrefixType::GET, $propertyName);
$result = $this->getMethodResult($getMethod); $result = $this->getMethodResult($getMethod);
return $result; return $result;
@ -178,7 +168,7 @@ final class EntityDomService implements EntityDomServiceInterface
*/ */
private function isPropertyAccessible(string $propertyName): bool private function isPropertyAccessible(string $propertyName): bool
{ {
$getMethod = $this->getMethodName(self::GET_METHOD, $propertyName); $getMethod = $this->getMethodName(MethodPrefixType::GET, $propertyName);
return $this->entityReflectionClass->hasMethod($getMethod); return $this->entityReflectionClass->hasMethod($getMethod);
} }

View File

@ -10,6 +10,7 @@ use Infinito\Entity\Meta\LawInterface;
use Infinito\Domain\RightManagement\RightChecker; use Infinito\Domain\RightManagement\RightChecker;
use Infinito\Entity\Source\SourceInterface; use Infinito\Entity\Source\SourceInterface;
use Infinito\Domain\SourceManagement\SourceMemberInformation; use Infinito\Domain\SourceManagement\SourceMemberInformation;
use Infinito\Domain\MethodManagement\MethodPrefixType;
/** /**
* @todo Implement checking by operation sources * @todo Implement checking by operation sources
@ -35,7 +36,7 @@ final class LawPermissionChecker implements LawPermissionCheckerInterface
{ {
$result = new ArrayCollection(); $result = new ArrayCollection();
foreach ($rights as $right) { foreach ($rights as $right) {
if ($right->{'get'.$attribut}() === $value) { if ($right->{MethodPrefixType::GET.$attribut}() === $value) {
$result->add($right); $result->add($right);
} }
} }

View File

@ -0,0 +1,34 @@
<?php
namespace Infinito\Domain\MethodManagement;
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
/**
* Contains the possible prefixes for Methods.
*
* @author kevinfrantz
*/
final class MethodPrefixType extends AbstractEnumType
{
/**
* @var string Prefix for setter functions
*/
public const SET = 'set';
/**
* @var string Prefix for getter functions
*/
public const GET = 'get';
/**
* @var string Prefix for has functions
*/
public const HAS = 'has';
protected static $choices = [
self::GET,
self::HAS,
self::SET,
];
}

View File

@ -5,27 +5,13 @@ namespace Infinito\Domain\RightManagement;
use Infinito\Entity\Meta\RightInterface; use Infinito\Entity\Meta\RightInterface;
use Infinito\Domain\RequestManagement\Right\RequestedRightInterface; use Infinito\Domain\RequestManagement\Right\RequestedRightInterface;
use Infinito\Entity\Meta\Right; use Infinito\Entity\Meta\Right;
use Infinito\Domain\MethodManagement\MethodPrefixType;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
final class RightTransformerService implements RightTransformerServiceInterface final class RightTransformerService implements RightTransformerServiceInterface
{ {
/**
* @var string Prefix for setter functions
*/
public const SET_PREFIX = 'set';
/**
* @var string Prefix for getter functions
*/
public const GET_PREFIX = 'get';
/**
* @var string Prefix for has functions
*/
public const HAS_PREFIX = 'has';
/** /**
* @param string $method * @param string $method
* *
@ -43,7 +29,7 @@ final class RightTransformerService implements RightTransformerServiceInterface
*/ */
private function isNameSetter(string $name): bool private function isNameSetter(string $name): bool
{ {
return self::GET_PREFIX === substr($name, 0, 3); return MethodPrefixType::GET === substr($name, 0, 3);
} }
/** /**
@ -119,9 +105,9 @@ final class RightTransformerService implements RightTransformerServiceInterface
*/ */
private function isAttributeGetPossible(string $attribute, object $object) private function isAttributeGetPossible(string $attribute, object $object)
{ {
$has = $this->createMethod(self::HAS_PREFIX, $attribute); $has = $this->createMethod(MethodPrefixType::HAS, $attribute);
if ($this->hasMethod($has, $object)) { if ($this->hasMethod($has, $object)) {
$get = $this->createMethod(self::GET_PREFIX, $attribute); $get = $this->createMethod(MethodPrefixType::GET, $attribute);
return $object->{$get}(); return $object->{$get}();
} }
@ -140,8 +126,8 @@ final class RightTransformerService implements RightTransformerServiceInterface
$attributes = $this->getAttributesExistInBoth($right, $requestedRight); $attributes = $this->getAttributesExistInBoth($right, $requestedRight);
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
if ($this->isAttributeGetPossible($attribute, $requestedRight)) { if ($this->isAttributeGetPossible($attribute, $requestedRight)) {
$get = $this->createMethod(self::GET_PREFIX, $attribute); $get = $this->createMethod(MethodPrefixType::GET, $attribute);
$set = $this->createMethod(self::SET_PREFIX, $attribute); $set = $this->createMethod(MethodPrefixType::SET, $attribute);
$origine = $requestedRight->{$get}(); $origine = $requestedRight->{$get}();
$right->{$set}($origine); $right->{$set}($origine);
} }

View File

@ -6,6 +6,7 @@ use Infinito\Entity\Meta\RightInterface;
use Infinito\Entity\Source\SourceInterface; use Infinito\Entity\Source\SourceInterface;
use Infinito\Domain\LawManagement\LawPermissionChecker; use Infinito\Domain\LawManagement\LawPermissionChecker;
use Infinito\Exception\Permission\NoSourcePermissionException; use Infinito\Exception\Permission\NoSourcePermissionException;
use Infinito\Domain\MethodManagement\MethodPrefixType;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -32,7 +33,7 @@ final class SecureSourceChecker implements SecureSourceCheckerInterface
*/ */
private function isGetter(string $methodName): bool private function isGetter(string $methodName): bool
{ {
return 'get' === substr($methodName, 0, 3); return MethodPrefixType::GET === substr($methodName, 0, 3);
} }
/** /**

View File

@ -10,6 +10,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Infinito\Exception\Attribut\AllreadyDefinedAttributException; use Infinito\Exception\Attribut\AllreadyDefinedAttributException;
use Infinito\Exception\Collection\ContainsElementException; use Infinito\Exception\Collection\ContainsElementException;
use Infinito\Exception\Collection\NotSetElementException; use Infinito\Exception\Collection\NotSetElementException;
use Infinito\Domain\MethodManagement\MethodPrefixType;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -37,7 +38,7 @@ final class SourceRightManager implements SourceRightManagerInterface
$attributes = ['source', 'law']; $attributes = ['source', 'law'];
foreach ($attributes as $attribut) { foreach ($attributes as $attribut) {
try { try {
$right->{'get'.ucfirst($attribut)}(); $right->{MethodPrefixType::GET.ucfirst($attribut)}();
throw new AllreadyDefinedAttributException("The attribut \"$attribut\" is allready defined!"); throw new AllreadyDefinedAttributException("The attribut \"$attribut\" is allready defined!");
} catch (\Error $error) { } catch (\Error $error) {
//Expected //Expected