Prevent double flush when editing domain redirects

This commit is contained in:
Alejandro Celaya 2021-08-03 09:55:21 +02:00
parent b78660c685
commit 6860855c71
2 changed files with 31 additions and 16 deletions

View file

@ -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;
}

View file

@ -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