Passed
Push — master ( 340202...35f943 )
by Jonathan
19:55
created

Controller::getDomainsTreeHtml()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 20
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 37
rs 9.6
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
use Illuminate\Support\Facades\Cache;
18
19
abstract class Controller extends BaseController
20
{
21
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
22
23
    /**
24
     * @var \Uccello\Core\Models\Domain
25
     */
26
    protected $domain;
27
28
    /**
29
     * @var \Uccello\Core\Models\Module
30
     */
31
    protected $module;
32
33
    /**
34
     * @var \Illuminate\Http\Request
35
     */
36
    protected $request;
37
38
    /**
39
     * @var string
40
     */
41
    protected $viewName;
42
43
    /**
44
     * Create a new controller instance.
45
     *
46
     * @return void
47
     */
48
    public function __construct()
49
    {
50
        $this->middleware('auth');
51
52
        // Check user permissions
53
        $this->checkPermissions();
54
    }
55
56
    /**
57
     * Process and display asked page
58
     * @param Domain|null $domain
59
     * @param Module $module
60
     * @param Request $request
61
     *
62
     * @return \Illuminate\Http\Response
63
     */
64
    public function process(?Domain $domain, Module $module, Request $request)
65
    {
66
        // Pre-process
67
        $this->preProcess($domain, $module, $request);
68
69
        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...
70
    }
71
72
    /**
73
     * Instantiance variables and check permissions
74
     * @param Domain|null $domain
75
     * @param Module $module
76
     * @param boolean $forBlade
77
     */
78
    protected function preProcess(?Domain &$domain, Module $module, Request $request, $forBlade=true)
79
    {
80
        // If we don't use multi domains, find the first one
81
        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...
82
            $domain = Domain::firstOrFail();
83
        }
84
85
        // Get domain
86
        $this->domain = $domain;
87
88
        // Get module
89
        $this->module = $module;
90
91
        // Get request
92
        $this->request = $request;
93
94
        // Save last visited domain by user
95
        $this->saveUserLastVisitedDomain();
96
97
        // Share blade variables
98
        if ($forBlade) {
99
            $this->shareBladeVariables();
100
        }
101
    }
102
103
    /**
104
     * Save last visited domain by user
105
     *
106
     * @return void
107
     */
108
    protected function saveUserLastVisitedDomain()
109
    {
110
        $user = Auth::user();
111
        $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...
112
        $user->save();
113
    }
114
115
    /**
116
     * Share global variables with all Blade views
117
     */
118
    protected function shareBladeVariables()
119
    {
120
        // Selected domain
121
        View::share('domain', $this->domain);
122
123
        // Selected module
124
        View::share('module', $this->module);
125
126
        // All modules
127
        View::share('modules', $this->getAllModules(true));
128
129
        // Admin environment
130
        View::share('admin_env', $this->module->isAdminModule());
131
132
        // Menu
133
        $menuGenerator = new MenuGenerator();
134
        $menuGenerator->makeMenu($this->domain, $this->module);
135
        $menu = $menuGenerator->getMenu();
136
        View::share('menu', $menu);
137
    }
138
139
    /**
140
     * Check user permissions
141
     */
142
    protected function checkPermissions()
143
    {
144
        //
145
    }
146
147
    /**
148
     * Get all modules from database
149
     * @param boolean $getAdminModules
150
     * @return Module[]
151
     */
152
    protected function getAllModules($getAdminModules = false)
153
    {
154
        $keyName = $getAdminModules ? 'modules_all' : 'modules_not_admin';
155
156
        return Cache::rememberForever($keyName, function () use($getAdminModules) {
157
            $modules = [ ];
158
159
            $allModules = Module::all();
160
161
            if ($getAdminModules) {
162
                $modules = $allModules;
163
            } else {
164
                foreach ($allModules as $module) {
165
                    if (!$module->isAdminModule()) {
166
                        $modules[ ] = $module;
167
                    }
168
                }
169
            }
170
171
            return $modules;
172
        });
173
    }
174
175
    /**
176
     * Retrieve record instance if "$key" param is defined or return a new empty instance.
177
     *
178
     * @param string $key
179
     * @return mixed|null
180
     */
181
    protected function getRecordFromRequest($key = 'id')
182
    {
183
        if (empty($this->module->model_class)) {
184
            return null;
185
        }
186
187
        // Retrieve model class
188
        $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...
189
190
        // An id is defined, retrieve the record from the database fail (404)
191
        if ($this->request->has($key)) {
192
            $recordId = (int)$this->request->input($key);
193
            $record = $modelClass::findOrFail($recordId);
194
        }
195
        // Make a new empty instance
196
        else {
197
            $record = new $modelClass();
198
199
            // Set domain if column exists
200
            if (Schema::hasColumn($record->getTable(), 'domain_id')) {
201
                $record->domain_id = $this->domain->id;
202
            }
203
        }
204
205
        return $record;
206
    }
207
208
    /**
209
     * Detects which view it must use and returns the evaluated view contents.
210
     *
211
     * @param  array   $data
212
     * @param  array   $mergeData
213
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
214
     */
215
    protected function autoView($data = [ ], $mergeData = [ ])
216
    {
217
        $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...
218
219
        return view($viewToUse, $data, $mergeData);
220
    }
221
}
222