Completed
Push — master ( 5cd775...78bef9 )
by Jonathan
14:29 queued 04:27
created

Controller::getDomainsTreeHtml()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 2
nop 0
dl 0
loc 36
rs 9.2728
c 0
b 0
f 0
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Illuminate\Foundation\Bus\DispatchesJobs;
6
use Illuminate\Routing\Controller as BaseController;
7
use Illuminate\Foundation\Validation\ValidatesRequests;
8
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\View;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Schema;
13
use Uccello\Core\Models\Domain;
14
use Uccello\Core\Models\Module;
15
use Uccello\Core\Support\MenuGenerator;
16
use Illuminate\Database\Eloquent\Collection;
17
18
abstract class Controller extends BaseController
19
{
20
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
21
22
    /**
23
     * @var \Uccello\Core\Models\Domain
24
     */
25
    protected $domain;
26
27
    /**
28
     * @var \Uccello\Core\Models\Module
29
     */
30
    protected $module;
31
32
    /**
33
     * @var \Illuminate\Http\Request
34
     */
35
    protected $request;
36
37
    /**
38
     * @var string
39
     */
40
    protected $viewName;
41
42
    /**
43
     * Create a new controller instance.
44
     *
45
     * @return void
46
     */
47
    public function __construct()
48
    {
49
        $this->middleware('auth');
50
51
        // Check user permissions
52
        $this->checkPermissions();
53
    }
54
55
    /**
56
     * Process and display asked page
57
     * @param Domain|null $domain
58
     * @param Module $module
59
     * @param Request $request
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function process(?Domain $domain, Module $module, Request $request)
64
    {
65
        // Pre-process
66
        $this->preProcess($domain, $module, $request);
67
68
        return $this->autoView();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->autoView() returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
69
    }
70
71
    /**
72
     * Instantiance variables and check permissions
73
     * @param Domain|null $domain
74
     * @param Module $module
75
     */
76
    protected function preProcess(?Domain &$domain, Module $module, Request $request)
77
    {
78
        // If we don't use multi domains, find the first one
79
        if (!uccello()->useMultiDomains()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of uccello() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
80
            $domain = Domain::firstOrFail();
81
        }
82
83
        // Get domain
84
        $this->domain = $domain;
85
86
        // Get module
87
        $this->module = $module;
88
89
        // Get request
90
        $this->request = $request;
91
92
        // Save last visited domain by user
93
        $this->saveUserLastVisitedDomain();
94
95
        // Share blade variables
96
        $this->shareBladeVariables();
97
    }
98
99
    /**
100
     * Save last visited domain by user
101
     *
102
     * @return void
103
     */
104
    protected function saveUserLastVisitedDomain()
105
    {
106
        $user = Auth::user();
107
        $user->last_domain_id = $this->domain->id;
0 ignored issues
show
Bug introduced by
Accessing last_domain_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
108
        $user->save();
109
    }
110
111
    /**
112
     * Share global variables with all Blade views
113
     */
114
    protected function shareBladeVariables()
115
    {
116
        // Selected domain
117
        View::share('domain', $this->domain);
118
119
        // Selected module
120
        View::share('module', $this->module);
121
122
        // All modules
123
        View::share('modules', $this->getAllModules(true));
124
125
        // Admin environment
126
        View::share('admin_env', $this->module->isAdminModule());
127
128
        // Menu
129
        $menuGenerator = new MenuGenerator();
130
        $menuGenerator->makeMenu($this->domain, $this->module);
131
        View::share('menu', $menuGenerator->getMenu());
132
133
        // Domain tree
134
        $domainsTreeHtml = $this->getDomainsTreeHtml();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $domainsTreeHtml is correct as $this->getDomainsTreeHtml() targeting Uccello\Core\Http\Contro...r::getDomainsTreeHtml() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
135
        View::share('domainsTreeHtml', $domainsTreeHtml);
136
    }
137
138
    /**
139
     * Check user permissions
140
     */
141
    protected function checkPermissions()
142
    {
143
        //
144
    }
145
146
    /**
147
     * Get all modules from database
148
     * @param boolean $getAdminModules
149
     * @return Module[]
150
     */
151
    protected function getAllModules($getAdminModules = false)
152
    {
153
        $modules = [ ];
154
155
        $allModules = Module::all();
156
157
        if ($getAdminModules) {
158
            $modules = $allModules;
159
        } else {
160
            foreach ($allModules as $module) {
161
                if (!$module->isAdminModule()) {
162
                    $modules[ ] = $module;
163
                }
164
            }
165
        }
166
167
        return $modules;
168
    }
169
170
    /**
171
     * Retrieve record instance if "id" param is defined or return a new empty instance.
172
     *
173
     * @return mixed|null
174
     */
175
    protected function getRecordFromRequest()
176
    {
177
        if (empty($this->module->model_class)) {
178
            return null;
179
        }
180
181
        // Retrieve model class
182
        $modelClass = $this->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...
183
184
        // An id is defined, retrieve the record from the database fail (404)
185
        if ($this->request->has('id')) {
186
            $recordId = (int)$this->request->input('id');
187
            $record = $modelClass::findOrFail($recordId);
188
        }
189
        // Make a new empty instance
190
        else {
191
            $record = new $modelClass();
192
193
            // Set domain if column exists
194
            if (Schema::hasColumn($record->getTable(), 'domain_id')) {
195
                $record->domain_id = $this->domain->id;
196
            }
197
        }
198
199
        return $record;
200
    }
201
202
    /**
203
     * Get HTML code for domains tree, according to user permissions.
204
     *
205
     * @return void
206
     */
207
    protected function getDomainsTreeHtml()
208
    {
209
        $domainsTreeHtml = '<ul class="tree tree-level-0">';
210
211
        $rootDomains = app('uccello')->getRootDomains();
0 ignored issues
show
Bug introduced by
The method getRootDomains() 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

211
        $rootDomains = app('uccello')->/** @scrutinizer ignore-call */ getRootDomains();

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...
212
        foreach($rootDomains as $root) {
213
214
            $descendants = $root->findDescendants()->get();
215
216
            $tree = $root->buildTree($descendants);
217
218
            $html = $tree->render(
219
                'ul',
220
                function ($node) {
221
                    if (auth()->user()->hasRoleOnDomain($node)) {
222
                        $currentClass = '';
223
                        if ($node->id === $this->domain->id) {
224
                            $currentClass = 'class="green-text"';
225
                        }
226
                        return '<li><a href="'.ucroute('uccello.home', $node).'" '.$currentClass.'>'.$node->name.'</a>{sub-tree}</li>';
227
                    }
228
                    elseif (auth()->user()->hasRoleOnDescendantDomain($node)) {
229
                        return '<li>'.$node->name.'{sub-tree}</li>';
230
                    } else {
231
                        return '';
232
                    }
233
                },
234
                TRUE
235
            );
236
237
            $domainsTreeHtml .= preg_replace('`^<ul class="tree tree-level-0">(.+?)</ul>$`', '$1', $html);
238
        }
239
240
        $domainsTreeHtml .= '</ul>';
241
242
        return $domainsTreeHtml;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $domainsTreeHtml returns the type string which is incompatible with the documented return type void.
Loading history...
243
    }
244
245
    /**
246
     * Detects which view it must use and returns the evaluated view contents.
247
     *
248
     * @param  array   $data
249
     * @param  array   $mergeData
250
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
251
     */
252
    protected function autoView($data = [ ], $mergeData = [ ])
253
    {
254
        $viewToUse = uccello()->view($this->module->package, $this->module, $this->viewName);
0 ignored issues
show
Bug introduced by
Are you sure the usage of uccello() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
255
256
        return view($viewToUse, $data, $mergeData);
257
    }
258
}
259