Passed
Push — master ( 7c331b...3c78de )
by Jonathan
18:06
created

TreeController::children()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 20
rs 9.9666
cc 3
nc 2
nop 3
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Uccello\Core\Models\Domain;
7
use Uccello\Core\Models\Module;
8
9
class TreeController extends Controller
10
{
11
    protected $viewName = 'tree.main';
12
13
    /**
14
     * Check user permissions
15
     */
16
    protected function checkPermissions()
17
    {
18
        $this->middleware('uccello.permissions:retrieve');
19
    }
20
21
    /**
22
     * @inheritDoc
23
     */
24
    public function process(?Domain $domain, Module $module, Request $request)
25
    {
26
        // Pre-process
27
        $this->preProcess($domain, $module, $request);
28
29
        if (!app('uccello')->isTreeModule($module)) {
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        if (!/** @scrutinizer ignore-call */ app('uccello')->isTreeModule($module)) {
Loading history...
30
            abort(404);
0 ignored issues
show
Bug introduced by
The function abort was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
            /** @scrutinizer ignore-call */ 
31
            abort(404);
Loading history...
31
        }
32
33
        // Get model class
34
        $modelClass = $module->model_class;
35
36
        // Total count
37
        $totalCount = $modelClass::inDomain($domain, $this->request->session()->get('descendants'))->count();
38
39
        return $this->autoView(compact('totalCount'));
40
    }
41
42
    /**
43
     * Returns all root records where the user can access
44
     *
45
     * @return array
46
     */
47
    public function root(?Domain $domain, Module $module, Request $request)
48
    {
49
        // Pre-process
50
        $this->preProcess($domain, $module, $request);
51
52
        // Get model class
53
        $modelClass = $module->model_class;
54
55
        // Get all roots records (according to the current domain)
56
        $rootRecords = $modelClass::getRoots()
57
            ->inDomain($domain, $this->request->session()->get('descendants'))
58
            ->get();
59
60
        $roots = collect();
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $roots = /** @scrutinizer ignore-call */ collect();
Loading history...
61
        foreach ($rootRecords as $record) {
62
            $roots[] = $this->getFormattedRecordToAdd($record);
63
        }
64
65
        return $roots;
66
    }
67
68
    /**
69
     * Returns all record's children.
70
     *
71
     * @return array
72
     */
73
    public function children(?Domain $domain, Module $module, Request $request)
74
    {
75
        // Pre-process
76
        $this->preProcess($domain, $module, $request);
77
78
        // Get model class
79
        $modelClass = $module->model_class;
80
81
        // Get parent record (according to the current domain)
82
        $parentRecord = $modelClass::inDomain($domain, $this->request->session()->get('descendants'))
83
            ->find(request('id'));
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            ->find(/** @scrutinizer ignore-call */ request('id'));
Loading history...
84
85
        $children = collect();
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $children = /** @scrutinizer ignore-call */ collect();
Loading history...
86
        if ($parentRecord) {
87
            foreach ($parentRecord->children()->get() as $record) {
88
                $children[] = $this->getFormattedRecordToAdd($record);
89
            }
90
        }
91
92
        return $children;
93
    }
94
95
    /**
96
     * Get formatted record to add to the tree
97
     *
98
     * @param mixed $record
99
     * @return array|null
100
     */
101
    protected function getFormattedRecordToAdd($record)
102
    {
103
        return [
104
            "id" => $record->getKey(),
105
            "text" => $record->recordLabel,
106
            "children" => $record->children->count() > 0,
107
            "a_attr" => [
108
                "href" => ucroute('uccello.detail', $this->domain, $this->module, [ 'id' => $record->getKey() ])
109
            ]
110
        ];
111
    }
112
}
113