/**
* Updates affected redirects in order to avoid redundant or circular redirections.
*
* @param RedirectInterface $newRedirect
* @throws Exception if creating the redirect would cause conflicts
* @return void
*/
protected function updateDependingRedirects(RedirectInterface $newRedirect)
{
/** @var $existingRedirectForSourceUriPath Redirect */
$existingRedirectForSourceUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost($newRedirect->getSourceUriPath(), $newRedirect->getHost(), false);
if ($existingRedirectForSourceUriPath !== null) {
$this->removeAndLog($existingRedirectForSourceUriPath, sprintf('Existing redirect for the source URI path "%s" removed.', $newRedirect->getSourceUriPath()));
$this->routerCachingService->flushCachesForUriPath($existingRedirectForSourceUriPath->getSourceUriPath());
}
/** @var $existingRedirectForTargetUriPath Redirect */
$existingRedirectForTargetUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost($newRedirect->getTargetUriPath(), $newRedirect->getHost(), false);
if ($existingRedirectForTargetUriPath !== null) {
$this->removeAndLog($existingRedirectForTargetUriPath, sprintf('Existing redirect for the target URI path "%s" removed.', $newRedirect->getTargetUriPath()));
$this->routerCachingService->flushCachesForUriPath($existingRedirectForTargetUriPath->getSourceUriPath());
}
$obsoleteRedirectInstances = $this->redirectRepository->findByTargetUriPathAndHost($newRedirect->getSourceUriPath(), $newRedirect->getHost());
/** @var $obsoleteRedirect Redirect */
foreach ($obsoleteRedirectInstances as $obsoleteRedirect) {
if ($obsoleteRedirect->getSourceUriPath() === $newRedirect->getTargetUriPath()) {
$this->redirectRepository->remove($obsoleteRedirect);
} else {
$obsoleteRedirect->setTargetUriPath($newRedirect->getTargetUriPath());
$this->redirectRepository->update($obsoleteRedirect);
}
}
}