Optimized draft for UserSourceDirector and UserRight

This commit is contained in:
Kevin Frantz 2019-01-13 14:00:16 +01:00
parent 1dc428bcfd
commit 2837df25f8
12 changed files with 108 additions and 100 deletions

View File

@ -21,6 +21,7 @@ final class DefaultController extends AbstractController
{ {
/** /**
* @deprecated Use load via source instead of fixed route * @deprecated Use load via source instead of fixed route
*
* @todo Optimize function! * @todo Optimize function!
* @Route("/imprint.{_format}", defaults={"_format"="json"}, name="imprint") * @Route("/imprint.{_format}", defaults={"_format"="json"}, name="imprint")
*/ */

View File

@ -6,7 +6,6 @@ use Symfony\Component\HttpFoundation\Response;
use App\Entity\UserInterface; use App\Entity\UserInterface;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Meta\RightInterface; use App\Entity\Meta\RightInterface;
use App\Domain\UserManagement\UserIdentityManager;
use Symfony\Component\Security\Core\User\UserInterface as CoreUserInterface; use Symfony\Component\Security\Core\User\UserInterface as CoreUserInterface;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use App\Entity\Source\SourceInterface; use App\Entity\Source\SourceInterface;
@ -41,7 +40,7 @@ final class SourceRESTResponseManagerService implements SourceRESTResponseManage
*/ */
private $user; private $user;
public function __construct(CoreUserInterface $user,SecureSourceReadService $secureSourceRead, EntityManagerInterface $entityManager, RightInterface $requestedRight) public function __construct(CoreUserInterface $user, SecureSourceReadService $secureSourceRead, EntityManagerInterface $entityManager, RightInterface $requestedRight)
{ {
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->user = $user; $this->user = $user;
@ -72,7 +71,8 @@ final class SourceRESTResponseManagerService implements SourceRESTResponseManage
} }
/** /**
* {@inheritDoc} * {@inheritdoc}
*
* @see \App\Domain\ResponseManagement\SourceRESTResponseManagerServiceInterface::getResponse() * @see \App\Domain\ResponseManagement\SourceRESTResponseManagerServiceInterface::getResponse()
*/ */
public function getResponse(ViewHandlerInterface $viewHandler): Response public function getResponse(ViewHandlerInterface $viewHandler): Response

View File

@ -0,0 +1,47 @@
<?php
namespace App\Domain\RightManagement;
use App\Entity\Meta\Right;
use App\Entity\Source\SourceInterface;
use App\Exception\SetNotPossibleException;
use App\Domain\UserManagement\UserSourceDirectorInterface;
/**
* @author kevinfrantz
*/
final class UserRight extends Right implements UserRightInterface
{
/**
* @var UserSourceDirectorInterface
*/
private $userSourceDirector;
/**
* @param UserSourceDirectorInterface $userSourceDirector
*/
public function __construct(UserSourceDirectorInterface $userSourceDirector)
{
$this->userSourceDirector = $userSourceDirector;
}
/**
* {@inheritdoc}
*
* @see \App\Entity\Attribut\RecieverAttributInterface::setReciever()
*/
public function setReciever(SourceInterface $reciever): void
{
throw new SetNotPossibleException('This class doesn\'t allow to set a reciever!');
}
/**
* {@inheritdoc}
*
* @see \App\Entity\Attribut\RecieverAttributInterface::getReciever()
*/
public function getReciever(): SourceInterface
{
return $this->userSourceDirector->getUser()->getSource();
}
}

View File

@ -1,14 +1,14 @@
<?php <?php
namespace App\Domain\RightManagement; namespace App\Domain\RightManagement;
use App\Entity\Meta\RightInterface; use App\Entity\Meta\RightInterface;
/** /**
* Offers a Service for managing the rights * Offers a Service for managing the rights.
* @author kevinfrantz
* *
* @author kevinfrantz
*/ */
interface UserRightServiceInterface extends RightInterface interface UserRightInterface extends RightInterface
{ {
} }

View File

@ -1,41 +0,0 @@
<?php
namespace App\Domain\RightManagement;
use App\Entity\Meta\Right;
use App\Entity\UserInterface as InfinitoUserInterface;
use App\Entity\Source\SourceInterface;
use App\Entity\UserInterface;
use Symfony\Component\Security\Core\Security;
/**
*
* @author kevinfrantz
*
*/
final class UserRightService extends Right implements UserRightServiceInterface
{
/**
*
* @var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* {@inheritDoc}
* @see \App\Entity\Attribut\RecieverAttributInterface::setReciever()
*/
public function setReciever(SourceInterface $reciever):void{
}
public function getReciever():SourceInterface{
return $this->user->getSource();
}
}

View File

@ -1,12 +0,0 @@
<?php
namespace App\Domain\UserManagement;
/**
*
* @author kevinfrantz
*
*/
interface UserIdentityServiceInterface extends UserIdentityManagerInterface
{
}

View File

@ -6,13 +6,12 @@ use App\Entity\UserInterface;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use App\DBAL\Types\SystemSlugType; use App\DBAL\Types\SystemSlugType;
use App\Entity\User; use App\Entity\User;
use App\Entity\Source\AbstractSource;
use App\Repository\Source\SourceRepository; use App\Repository\Source\SourceRepository;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
final class UserIdentityManager implements UserIdentityManagerInterface final class UserSourceDirector implements UserSourceDirectorInterface
{ {
/** /**
* @var UserInterface * @var UserInterface
@ -24,14 +23,6 @@ final class UserIdentityManager implements UserIdentityManagerInterface
*/ */
private $sourceRepository; private $sourceRepository;
/**
* @param EntityManagerInterface $entityManager
*/
private function setSourceRepository(EntityManagerInterface $entityManager): void
{
$this->sourceRepository = $entityManager->getRepository(AbstractSource::class);
}
/** /**
* @param UserInterface $user * @param UserInterface $user
*/ */
@ -50,16 +41,16 @@ final class UserIdentityManager implements UserIdentityManagerInterface
* @param EntityManagerInterface $entityManager * @param EntityManagerInterface $entityManager
* @param UserInterface $user * @param UserInterface $user
*/ */
public function __construct(EntityManagerInterface $entityManager, ?UserInterface $user) public function __construct(SourceRepository $sourceRepository, ?UserInterface $user)
{ {
$this->setSourceRepository($entityManager); $this->sourceRepository = $sourceRepository;
$this->setUser($user); $this->setUser($user);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* @see \App\Domain\UserManagement\UserIdentityManagerInterface::getUser() * @see \App\Domain\UserManagement\UserSourceDirectorInterface::getUser()
*/ */
public function getUser(): UserInterface public function getUser(): UserInterface
{ {

View File

@ -4,7 +4,7 @@ namespace App\Domain\UserManagement;
use App\Entity\UserInterface; use App\Entity\UserInterface;
interface UserIdentityManagerInterface interface UserSourceDirectorInterface
{ {
/** /**
* @return UserInterface * @return UserInterface

View File

@ -1,16 +1,18 @@
<?php <?php
namespace App\Domain\UserManagement; namespace App\Domain\UserManagement;
use App\Entity\UserInterface; use App\Entity\UserInterface;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
use App\Entity\Source\AbstractSource;
/** /**
*
* @author kevinfrantz * @author kevinfrantz
*
* @todo Test! * @todo Test!
*/ */
final class UserIdentityService implements UserIdentityServiceInterface final class UserSourceDirectorService implements UserSourceDirectorServiceInterface
{ {
/** /**
* @var EntityManager * @var EntityManager
@ -22,32 +24,34 @@ final class UserIdentityService implements UserIdentityServiceInterface
*/ */
private $security; private $security;
/**
* @var UserIdentityManagerInterface
*/
private $userIdentityManager;
/** /**
* @param EntityManager $entityManager * @param EntityManager $entityManager
* @param Security $security * @param Security $security
*/ */
public function __construct(EntityManager $entityManager, Security $security){ public function __construct(EntityManager $entityManager, Security $security)
{
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->security = $security; $this->security = $security;
} }
private function setUserIdentityManager():void{ /**
$this->userIdentityManager = new UserIdentityManager($this->entityManager, $this->security->getUser()); * @return UserSourceDirectorInterface
*/
private function getUserSourceDirector(): UserSourceDirectorInterface
{
$sourceRepository = $this->entityManager->getRepository(AbstractSource::class);
$user = $this->security->getUser();
return new UserSourceDirector($sourceRepository, $user);
} }
/** /**
* @todo Optimzed performance! * @todo Optimzed performance!
* {@inheritDoc} *
* @see \App\Domain\UserManagement\UserIdentityManagerInterface::getUser() * @return UserInterface
*/ */
public function getUser(): UserInterface public function getUser(): UserInterface
{ {
$this->setUserIdentityManager(); return $this->getUserSourceDirector()->getUser();
return $this->userIdentityManager->getUser();
} }
} }

View File

@ -0,0 +1,10 @@
<?php
namespace App\Domain\UserManagement;
/**
* @author kevinfrantz
*/
interface UserSourceDirectorServiceInterface extends UserSourceDirectorInterface
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exception;
/**
* @author kevinfrantz
*/
class SetNotPossibleException extends \Exception
{
}

View File

@ -1,4 +1,5 @@
<?php <?php
namespace tests\Unit\Domain\RightManagement; namespace tests\Unit\Domain\RightManagement;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -7,19 +8,16 @@ use App\Entity\Source\SourceInterface;
use App\Domain\RightManagement\UserRightService; use App\Domain\RightManagement\UserRightService;
/** /**
*
* @author kevinfrantz * @author kevinfrantz
*
*/ */
class UserRightServiceTest extends TestCase class UserRightServiceTest extends TestCase
{ {
public function testUserSet():void{ public function testUserSet(): void
{
$user = new User(); $user = new User();
$source = $this->createMock(SourceInterface::class); $source = $this->createMock(SourceInterface::class);
$user->setSource($source); $user->setSource($source);
$userRight = new UserRightService($user); $userRight = new UserRightService($user);
$this->assertEquals($source, $userRight->getReciever()); $this->assertEquals($source, $userRight->getReciever());
} }
} }