Lines 30.43% 7 / 23
Methods 0.00% 0 / 2
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 transform 36.84% 7 / 19 0.00% 0 / 1 8.03
 getSymbol 0.00% 0 / 4 0.00% 0 / 1 6
28final class AdminCategoryTreePresenter
29{
30    /**
31     * @param array<int, array<string, mixed>> $categoryName Map id => row
32     * @param array<int, array<int, array<string, mixed>>> $childrenMap Parent => [childId => row]
33     * @return array<int, array<string, mixed>> linear list of entries
34     */
35    public function transform(TreeBuilder $treeBuilder, array $categoryName, array $childrenMap, int $categoryId): array
36    {
37        $entries = [];
38
39        $category = $categoryName[$categoryId] ?? [];
40        $parentId = (int) ($category['parent_id'] ?? 0);
41        $children = $childrenMap[$categoryId] ?? [];
42
43        if ($categoryId > 0 && $category !== []) {
44            $entry = $category;
45            $entry['level'] = $treeBuilder->computeLevel($categoryName, $categoryId);
46            $entry['children'] = array_keys($children);
47            $entry['tree'] = $treeBuilder->buildTree($categoryName, $childrenMap, $categoryId);
48            $entry['symbol'] = $this->getSymbol($childrenMap, $categoryId, $parentId);
49            $entries[] = $entry;
50        }
51
52        foreach (array_keys($children) as $childId) {
53            $entries = array_merge($entries, $this->transform(
54                $treeBuilder,
55                $categoryName,
56                $childrenMap,
57                (int) $childId,
58            ));
59        }
60
61        return $entries;
62    }
63
64    /**
65     * Returns the symbol for a category based on its position among its siblings.
66     *
67     * @param array<int, array<int, mixed>> $childrenMap
68     */
69    private function getSymbol(array $childrenMap, int $categoryId, int $parentId): string
70    {
71        $siblings = $childrenMap[$parentId] ?? [];
72        $keys = array_keys($siblings);
73        $last = end($keys);
74        return $categoryId === $last ? 'angle' : 'medium';
75    }
76}