Passed
Push — feature/permissions-management ( 3c8a9f...1cc208 )
by Harings
19:21 queued 10s
created

NestedModuleController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A transformIndexItems() 0 3 1
A indexData() 0 5 1
A getBrowserItems() 0 12 2
A indexItemData() 0 5 2
1
<?php
2
3
namespace A17\Twill\Http\Controllers\Admin;
4
5
abstract class NestedModuleController extends ModuleController
6
{
7
    /**
8
     * Indicates if only parent items should be displayed when browsing for this module
9
     * within a browser field.
10
     *
11
     * @var bool
12
     */
13
    protected $showOnlyParentItemsInBrowsers = true;
14
15
    /**
16
     * The maximum depth allowed for nested items. A value of `1` means parent & child.
17
     *
18
     * @var int
19
     */
20
    protected $nestedItemsDepth = 1;
21
22
    protected function indexData($request)
23
    {
24
        return [
25
            'nested' => true,
26
            'nestedDepth' => $this->nestedItemsDepth,
27
        ];
28
    }
29
30
    protected function transformIndexItems($items)
31
    {
32
        return $items->toTree();
33
    }
34
35
    protected function indexItemData($item)
36
    {
37
        return ($item->children ? [
38
            'children' => $this->getIndexTableData($item->children),
39
        ] : []);
40
    }
41
42
    protected function getBrowserItems($scopes = [])
43
    {
44
        if ($this->showOnlyParentItemsInBrowsers) {
45
            return $this->getIndexItems($scopes, true);
46
        }
47
48
        return $this->repository->get(
49
            $this->indexWith,
50
            $scopes,
51
            $this->orderScope(),
52
            request('offset') ?? $this->perPage ?? 50,
53
            true
54
        );
55
    }
56
}
57