TreeController::root()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
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 19
rs 9.9666
cc 2
nc 2
nop 3
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Illuminate\Http\Request;
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 method isTreeModule() does not exist on Uccello\Core\Facades\Uccello. ( 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 (!app('uccello')->/** @scrutinizer ignore-call */ isTreeModule($module)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
            abort(404);
31
        }
32
33
        // Get model class
34
        $modelClass = $module->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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();
61
        foreach ($rootRecords as $record) {
62
            $roots[] = $this->getFormattedRecordToAdd($record);
63
        }
64
65
        return $roots;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $roots returns the type Illuminate\Support\Collection which is incompatible with the documented return type array.
Loading history...
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;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
80
81
        // Get parent record (according to the current domain)
82
        $parentRecord = $modelClass::inDomain($domain, $this->request->session()->get('descendants'))
83
            ->find(request('id'));
84
85
        $children = collect();
86
        if ($parentRecord) {
87
            foreach ($parentRecord->children()->get() as $record) {
88
                $children[] = $this->getFormattedRecordToAdd($record);
89
            }
90
        }
91
92
        return $children;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $children returns the type Illuminate\Support\Collection which is incompatible with the documented return type array.
Loading history...
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