From b7c2c0ae3d449647fc84b6011156ffa198f905aa Mon Sep 17 00:00:00 2001 From: Beth Jacobson Date: Tue, 28 Jan 2025 16:10:52 -0500 Subject: [PATCH] Reduced complexity, added doc comment. --- .../dkan_js_frontend/src/Controller/Page.php | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/modules/dkan_js_frontend/src/Controller/Page.php b/modules/dkan_js_frontend/src/Controller/Page.php index a7da28d37e..6b097d71d3 100644 --- a/modules/dkan_js_frontend/src/Controller/Page.php +++ b/modules/dkan_js_frontend/src/Controller/Page.php @@ -21,6 +21,9 @@ class Page extends ControllerBase implements ContainerInjectionInterface { */ private MetastoreService $metastoreService; + /** + * The request stack. + */ protected RequestStack $requestStack; /** @@ -56,6 +59,21 @@ public function __construct(MetastoreService $service, CurrentPathStack $current * Returns a render-able array. */ public function content() { + // Checking for 404 prevents an infinite loop. + if ($this->requestStack->getCurrentRequest()->query->get('_exception_statuscode') !== 404) { + $this->handleInvalidDatasetId(); + } + + return [ + '#theme' => 'page__dkan_js_frontend', + ]; + } + + /** + * If a dataset with an invalid ID is being requested, throw a 404 error. + */ + protected function handleInvalidDatasetId() + { // Path should always have leading slash. // @see \Symfony\Component\HttpFoundation\Request::getPathInfo() // Dataset path is /dataset/[ID]/data. @@ -65,23 +83,14 @@ public function content() { $path = $this->currentPath->getPath(); - - if ($this->requestStack->getCurrentRequest()->query->get('_exception_statuscode') !== 404) { - - if (preg_match($dataset_data_path_match, $path, $matches) - || preg_match($dataset_path_match, $path, $matches)) { - - try { - $this->metastoreService->get('dataset', $matches['id']); - } catch (MissingObjectException $exception) { - throw new NotFoundHttpException(); - } - } - } - - return [ - '#theme' => 'page__dkan_js_frontend', - ]; + if (preg_match($dataset_data_path_match, $path, $matches) + || preg_match($dataset_path_match, $path, $matches)) { + try { + $this->metastoreService->get('dataset', $matches['id']); + } catch (MissingObjectException $exception) { + throw new NotFoundHttpException(); + } + } } }