Skip to content

Commit

Permalink
Reduced complexity, added doc comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
Beth Jacobson committed Jan 28, 2025
1 parent f586436 commit b7c2c0a
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions modules/dkan_js_frontend/src/Controller/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Page extends ControllerBase implements ContainerInjectionInterface {
*/
private MetastoreService $metastoreService;

/**
* The request stack.
*/
protected RequestStack $requestStack;

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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();
}
}
}

}

0 comments on commit b7c2c0a

Please sign in to comment.