diff --git a/application/symfony/src/Domain/RightManagement/RightTransformerService.php b/application/symfony/src/Domain/RightManagement/RightTransformerService.php new file mode 100644 index 0000000..02cfcf8 --- /dev/null +++ b/application/symfony/src/Domain/RightManagement/RightTransformerService.php @@ -0,0 +1,144 @@ +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; + } +} diff --git a/application/symfony/src/Domain/RightManagement/RightTransformerServiceInterface.php b/application/symfony/src/Domain/RightManagement/RightTransformerServiceInterface.php new file mode 100644 index 0000000..494c6fe --- /dev/null +++ b/application/symfony/src/Domain/RightManagement/RightTransformerServiceInterface.php @@ -0,0 +1,21 @@ +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()); + } +}