From 8f0a9ae51fdc92e7c33f7a48b9d84da6c6b196e1 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 11 Oct 2017 17:08:45 +0200 Subject: [PATCH] split walking the tree from operating on it so walking it is reusable Signed-off-by: Arthur Schiwon --- lib/private/Comments/Manager.php | 39 ++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index a6b2102da6..3b5eb23844 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -340,12 +340,12 @@ class Manager implements ICommentsManager { public function getActorsInTree($id) { $tree = $this->getTree($id); $actors = []; - $this->extractActor($tree, $actors); + $this->walkTree($tree, $actors, [$this, 'extractActor']); return $actors; } /** - * @param array $node + * @param IComment $comment * @param array &$actors * * build an array that looks like: @@ -362,23 +362,34 @@ class Manager implements ICommentsManager { * ] * */ - protected function extractActor(array $node, array &$actors) { + protected function extractActor(IComment $comment, &$actors) { + if(!isset($actors[$comment->getActorType()])) { + $actors[$comment->getActorType()] = []; + } + if(!isset($actors[$comment->getActorType()][$comment->getActorId()])) { + $actors[$comment->getActorType()][$comment->getActorId()] = 1; + } else { + $actors[$comment->getActorType()][$comment->getActorId()] += 1; + } + } + + /** + * walks through a comment tree (as returned by getTree() and invokes a callback + * with the current IComment instance (and optionally custom parameters) + * + * @param array $node + * @param array &$results + * @param callable $callback + * @param array|null $parameters + */ + protected function walkTree($node, array &$results, callable $callback, array $parameters = null) { if(isset($node['replies'])) { foreach ($node['replies'] as $subNode) { - $this->extractActor($subNode, $actors); + $this->walkTree($subNode, $results, $callback, $parameters); } } if(isset($node['comment']) && $node['comment'] instanceof IComment) { - /** @var IComment $comment */ - $comment = $node['comment']; - if(!isset($actors[$comment->getActorType()])) { - $actors[$comment->getActorType()] = []; - } - if(!isset($actors[$comment->getActorType()][$comment->getActorId()])) { - $actors[$comment->getActorType()][$comment->getActorId()] = 1; - } else { - $actors[$comment->getActorType()][$comment->getActorId()] += 1; - } + $callback($node['comment'], $results, $parameters); } }