diff --git a/application/src/Controller/DefaultController.php b/application/src/Controller/DefaultController.php index ab57868..40ccf1e 100644 --- a/application/src/Controller/DefaultController.php +++ b/application/src/Controller/DefaultController.php @@ -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); diff --git a/application/src/Domain/UserManagement/UserIdentityManager.php b/application/src/Domain/UserManagement/UserIdentityManager.php new file mode 100644 index 0000000..5fd78b1 --- /dev/null +++ b/application/src/Domain/UserManagement/UserIdentityManager.php @@ -0,0 +1,68 @@ +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; + } +} diff --git a/application/src/Domain/UserManagement/UserIdentityManagerInterface.php b/application/src/Domain/UserManagement/UserIdentityManagerInterface.php new file mode 100644 index 0000000..406210a --- /dev/null +++ b/application/src/Domain/UserManagement/UserIdentityManagerInterface.php @@ -0,0 +1,13 @@ +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); + } +}