Skip to content

Commit

Permalink
feat(PageService): Clean subpageOrder from orphans before adding new ids
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Oct 25, 2023
1 parent 1f4a789 commit faf1716
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
44 changes: 43 additions & 1 deletion lib/Service/PageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,28 @@ public function getFolder(int $collectiveId, int $fileId, string $userId): Folde
return $file->getParent();
}

/**
* @param int $collectiveId
* @param File $file
* @param string $userId
*
* @return array
* @throws MissingDependencyException
* @throws NotFoundException
* @throws NotPermittedException
*/
private function getSubpagesFromFile(int $collectiveId, File $file, string $userId): array {
if (!NodeHelper::isIndexPage($file)) {
return [];
}

$parentId = $file->getId();
$folder = $this->getFolder($collectiveId, $parentId, $userId);
return array_filter($this->getPagesFromFolder($collectiveId, $folder, $userId), static function (PageInfo $pageInfo) use ($parentId) {
return $pageInfo->getParentId() === $parentId;
});
}

/**
* @param File $file
* @param Node|null $parent
Expand Down Expand Up @@ -902,6 +924,24 @@ public function setSubpageOrder(int $collectiveId, int $id, ?string $subpageOrde
return $pageInfo;
}

/**
* @param int $collectiveId
* @param PageInfo $pageInfo
* @param string $userId
*
* @return string
* @throws MissingDependencyException
* @throws NotFoundException
* @throws NotPermittedException
*/
private function cleanSubpageOrder(int $collectiveId, PageInfo $pageInfo, string $userId): string {
$pageFile = $this->getPageFile($collectiveId, $pageInfo->getId(), $userId);
$childIds = array_map(static function (PageInfo $pageInfo) {
return $pageInfo->getId();
}, $this->getSubpagesFromFile($collectiveId, $pageFile, $userId));
return SubpageOrderService::clean($pageInfo->getSubpageOrder(), $childIds);
}

/**
* @param int $collectiveId
* @param int $pageId
Expand All @@ -918,7 +958,9 @@ private function addToSubpageOrder(int $collectiveId, int $pageId, int $addId, i
$file = $this->nodeHelper->getFileById($collectiveFolder, $pageId);
$pageInfo = $this->getPageByFile($file);

$newSubpageOrder = SubpageOrderService::add($pageInfo->getSubpageOrder(), $addId, $index);
$subpageOrder = $pageInfo->getSubpageOrder();
$cleanedSubpageOrder = $this->cleanSubpageOrder($collectiveId, $pageInfo, $userId);
$newSubpageOrder = SubpageOrderService::add($cleanedSubpageOrder, $addId, $index);

$pageInfo->setSubpageOrder($newSubpageOrder);
$this->updatePage($collectiveId, $pageInfo->getId(), $userId, null, $newSubpageOrder);
Expand Down
19 changes: 19 additions & 0 deletions lib/Service/SubpageOrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ public static function verify(?string $subpageOrder): void {
}
}

/**
* @param string|null $subpageOrder
* @param array $childIds
*
* @return string
* @throws NotPermittedException
*/
public static function clean(?string $subpageOrder, array $childIds): string {
$subpageOrderArray = self::toArray($subpageOrder);
$cleanedSubpageOrderArray = [];
foreach ($subpageOrderArray as $pageId) {
if (in_array($pageId, $childIds, true)) {
$cleanedSubpageOrderArray[] = $pageId;
}
}

return self::fromArray($cleanedSubpageOrderArray);
}

/**
* @param string|null $subpageOrder
* @param int $pageId
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Service/SubpageOrderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public function testVerifyInvalid3(): void {
SubpageOrderService::verify('{a: b}');
}

public function testClean(): void {
$subpageOrder = '[1,2,3,5]';
self::assertEquals('[1,3,5]', SubpageOrderService::clean($subpageOrder, [1,3,4,5]));
self::assertEquals('[1,3]', SubpageOrderService::clean($subpageOrder, [1,3]));
}

public function testAdd(): void {
$subpageOrder = '[1,2,3]';
self::assertEquals('[0,1,2,3]', SubpageOrderService::add($subpageOrder, 0));
Expand Down

0 comments on commit faf1716

Please sign in to comment.