diff --git a/module/Core/src/Domain/DomainService.php b/module/Core/src/Domain/DomainService.php index 708984b5..807c6dce 100644 --- a/module/Core/src/Domain/DomainService.php +++ b/module/Core/src/Domain/DomainService.php @@ -65,7 +65,37 @@ class DomainService implements DomainServiceInterface return $repo->findOneByAuthority($authority, $apiKey); } + /** + * @throws DomainNotFoundException + */ public function getOrCreate(string $authority, ?ApiKey $apiKey = null): Domain + { + $domain = $this->getPersistedDomain($authority, $apiKey); + $this->em->flush(); + + return $domain; + } + + /** + * @throws DomainNotFoundException + */ + public function configureNotFoundRedirects( + string $authority, + NotFoundRedirects $notFoundRedirects, + ?ApiKey $apiKey = null + ): Domain { + $domain = $this->getPersistedDomain($authority, $apiKey); + $domain->configureNotFoundRedirects($notFoundRedirects); + + $this->em->flush(); + + return $domain; + } + + /** + * @throws DomainNotFoundException + */ + private function getPersistedDomain(string $authority, ?ApiKey $apiKey): Domain { $domain = $this->findByAuthority($authority, $apiKey); if ($domain === null && $apiKey?->hasRole(Role::DOMAIN_SPECIFIC)) { @@ -74,22 +104,7 @@ class DomainService implements DomainServiceInterface } $domain = $domain ?? Domain::withAuthority($authority); - $this->em->persist($domain); - $this->em->flush(); - - return $domain; - } - - public function configureNotFoundRedirects( - string $authority, - NotFoundRedirects $notFoundRedirects, - ?ApiKey $apiKey = null - ): Domain { - $domain = $this->getOrCreate($authority, $apiKey); - $domain->configureNotFoundRedirects($notFoundRedirects); - - $this->em->flush(); return $domain; } diff --git a/module/Core/test/Domain/DomainServiceTest.php b/module/Core/test/Domain/DomainServiceTest.php index 6e91b425..060b24ab 100644 --- a/module/Core/test/Domain/DomainServiceTest.php +++ b/module/Core/test/Domain/DomainServiceTest.php @@ -197,7 +197,7 @@ class DomainServiceTest extends TestCase self::assertEquals('baz.com', $result->invalidShortUrlRedirect()); $getRepo->shouldHaveBeenCalledOnce(); $persist->shouldHaveBeenCalledOnce(); - $flush->shouldHaveBeenCalledTimes(2); + $flush->shouldHaveBeenCalledOnce(); } public function provideFoundDomains(): iterable