Skip to content

Commit

Permalink
refactor(PageService): Rename recurseFolder() to `getPagesFromFolde…
Browse files Browse the repository at this point in the history
…r()`

Also add support to not recurse through the folder.

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Oct 25, 2023
1 parent 0dedb8f commit 1f4a789
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
9 changes: 5 additions & 4 deletions lib/Service/PageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
}
}

Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Service/PageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1f4a789

Please sign in to comment.