Implemented UserIdentityManager

This commit is contained in:
Kevin Frantz 2019-01-04 20:41:37 +01:00
parent a074932fb5
commit e335544808
4 changed files with 125 additions and 10 deletions

View File

@ -6,11 +6,12 @@ use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use App\DBAL\Types\SystemSlugType;
use App\Entity\Source\AbstractSource;
use App\Entity\User;
use App\Domain\SecureLoadManagement\SecureSourceLoader;
use App\Entity\Meta\Right;
use App\DBAL\Types\LayerType;
use App\DBAL\Types\RightType;
use App\Domain\UserManagement\UserIdentityManager;
use Doctrine\ORM\EntityManagerInterface;
/**
* This controller offers the standart routes for the template.
@ -23,16 +24,10 @@ class DefaultController extends AbstractEntityController
* @todo Optimize function!
* @Route("/imprint", name="imprint")
*/
public function imprint(): Response
public function imprint(EntityManagerInterface $entityManager): Response
{
if ($this->getUser()) {
$user = $this->getUser();
} else {
$user = new User();
$user->setSource($this->getDoctrine()
->getRepository(AbstractSource::class)
->findOneBySlug(SystemSlugType::GUEST_USER));
}
$userIdentityManager = new UserIdentityManager($entityManager, $this->getUser());
$user = $userIdentityManager->getUser();
$requestedSource = new class() extends AbstractSource {
};
$requestedSource->setSlug(SystemSlugType::IMPRINT);

View File

@ -0,0 +1,68 @@
<?php
namespace App\Domain\UserManagement;
use App\Entity\UserInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\DBAL\Types\SystemSlugType;
use App\Entity\User;
use App\Entity\Source\AbstractSource;
use App\Repository\Source\SourceRepository;
/**
* @author kevinfrantz
*/
final class UserIdentityManager implements UserIdentityManagerInterface
{
/**
* @var UserInterface
*/
private $user;
/**
* @var SourceRepository
*/
private $sourceRepository;
/**
* @param EntityManagerInterface $entityManager
*/
private function setSourceRepository(EntityManagerInterface $entityManager): void
{
$this->sourceRepository = $entityManager->getRepository(AbstractSource::class);
}
/**
* @param UserInterface $user
*/
private function setUser(?UserInterface $user): void
{
if ($user) {
$this->user = $user;
return;
}
$this->user = new User();
$this->user->setSource($this->sourceRepository->findOneBySlug(SystemSlugType::GUEST_USER));
}
/**
* @param EntityManagerInterface $entityManager
* @param UserInterface $user
*/
public function __construct(EntityManagerInterface $entityManager, ?UserInterface $user)
{
$this->setSourceRepository($entityManager);
$this->setUser($user);
}
/**
* {@inheritdoc}
*
* @see \App\Domain\UserManagement\UserIdentityManagerInterface::getUser()
*/
public function getUser(): UserInterface
{
return $this->user;
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace App\Domain\UserManagement;
use App\Entity\UserInterface;
interface UserIdentityManagerInterface
{
/**
* @return UserInterface
*/
public function getUser(): UserInterface;
}

View File

@ -0,0 +1,39 @@
<?php
namespace Tests\Unit\Domain\UserManagement;
use App\Domain\UserManagement\UserIdentityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use App\DBAL\Types\SystemSlugType;
use App\Entity\User;
class UserIdentityManagerTest extends KernelTestCase
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function setUp(): void
{
self::bootKernel();
$this->entityManager = self::$container->get('doctrine.orm.default_entity_manager');
}
public function testGuestUser(): void
{
$origineUser = null;
$userIdentityManager = new UserIdentityManager($this->entityManager, $origineUser);
$expectedUser = $userIdentityManager->getUser();
$this->assertEquals(SystemSlugType::GUEST_USER, $expectedUser->getSource()->getSlug());
}
public function testUser(): void
{
$origineUser = new User();
$userIdentityManager = new UserIdentityManager($this->entityManager, $origineUser);
$expectedUser = $userIdentityManager->getUser();
$this->assertEquals($origineUser, $expectedUser);
}
}