From 1f4a7893226eaa73badaf3e723ebaee4b1353b04 Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 24 Oct 2023 01:15:10 +0200 Subject: [PATCH] refactor(PageService): Rename `recurseFolder()` to `getPagesFromFolder()` Also add support to not recurse through the folder. Signed-off-by: Jonas --- lib/Service/PageService.php | 9 +++++---- tests/Unit/Service/PageServiceTest.php | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/Service/PageService.php b/lib/Service/PageService.php index 3d325ebf17..d30f1121c3 100644 --- a/lib/Service/PageService.php +++ b/lib/Service/PageService.php @@ -394,13 +394,14 @@ private function getIndexPageFile(Folder $folder): File { * @param int $collectiveId * @param Folder $folder * @param string $userId + * @param bool $recurse * * @return array * @throws FilesNotFoundException * @throws NotFoundException * @throws NotPermittedException */ - public function recurseFolder(int $collectiveId, Folder $folder, string $userId): array { + public function getPagesFromFolder(int $collectiveId, Folder $folder, string $userId, bool $recurse = false): array { // Find index page or create it if we have subpages, but it doesn't exist try { $indexPage = $this->getPageByFile($this->getIndexPageFile($folder), $folder); @@ -416,8 +417,8 @@ public function recurseFolder(int $collectiveId, Folder $folder, string $userId) foreach ($folder->getDirectoryListing() as $node) { if ($node instanceof File && NodeHelper::isPage($node) && !NodeHelper::isIndexPage($node)) { $pageInfos[] = $this->getPageByFile($node, $folder); - } elseif ($node instanceof Folder) { - array_push($pageInfos, ...$this->recurseFolder($collectiveId, $node, $userId)); + } elseif ($recurse && $node instanceof Folder) { + array_push($pageInfos, ...$this->getPagesFromFolder($collectiveId, $node, $userId, true)); } } @@ -436,7 +437,7 @@ public function recurseFolder(int $collectiveId, Folder $folder, string $userId) public function findAll(int $collectiveId, string $userId): array { $folder = $this->getCollectiveFolder($collectiveId, $userId); try { - return $this->recurseFolder($collectiveId, $folder, $userId); + return $this->getPagesFromFolder($collectiveId, $folder, $userId, true); } catch (NotPermittedException $e) { throw new NotFoundException($e->getMessage(), 0, $e); } diff --git a/tests/Unit/Service/PageServiceTest.php b/tests/Unit/Service/PageServiceTest.php index 51c5493190..1ea5578e4b 100644 --- a/tests/Unit/Service/PageServiceTest.php +++ b/tests/Unit/Service/PageServiceTest.php @@ -141,7 +141,7 @@ public function testInitSubFolder(): void { self::assertEquals($subFolder, $this->service->initSubFolder($otherFile)); } - public function testRecurseFolder(): void { + public function testGetPagesFromFolder(): void { $filesNotJustMd = []; $filesJustMd = []; $pageInfos = []; @@ -224,8 +224,8 @@ public function testRecurseFolder(): void { $filesNotJustMd, ); - self::assertEquals($pageInfos, $this->service->recurseFolder($this->collectiveId, $folder, $this->userId)); - self::assertEquals($pageInfos, $this->service->recurseFolder($this->collectiveId, $folder, $this->userId)); + self::assertEquals($pageInfos, $this->service->getPagesFromFolder($this->collectiveId, $folder, $this->userId, true)); + self::assertEquals($pageInfos, $this->service->getPagesFromFolder($this->collectiveId, $folder, $this->userId, true)); } public function testGetPageLink(): void {