Passed
Pull Request — master (#1713)
by Darko
07:01
created

BasePageController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 86
c 1
b 0
f 0
dl 0
loc 227
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUserPreferences() 0 48 5
A middleware() 0 4 1
A pagerender() 0 5 1
A paginate() 0 3 1
A render() 0 3 1
A show404() 0 7 2
A setAdminPrefs() 0 12 1
A adminBasePage() 0 8 1
A __construct() 0 12 2
A adminrender() 0 8 1
A setPreferences() 0 26 2
A isPostBack() 0 3 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Routing\Controllers\HasMiddleware;
6
use Illuminate\Routing\Controllers\Middleware;
7
use App\Events\UserLoggedIn;
8
use App\Models\Category;
9
use App\Models\Settings;
10
use App\Models\User;
11
use Blacklight\Contents;
0 ignored issues
show
Bug introduced by
The type Blacklight\Contents 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...
12
use Illuminate\Http\Request;
13
use Illuminate\Pagination\LengthAwarePaginator;
14
use Illuminate\Support\Arr;
15
use Illuminate\Support\Facades\Auth;
16
use Illuminate\View\View;
17
18
class BasePageController extends Controller implements HasMiddleware
19
{
20
    public Settings $settings;
21
22
    public string $title = '';
23
24
    public string $content = '';
25
26
    public string $meta_keywords = '';
27
28
    public string $meta_title = '';
29
30
    public string $meta_description = '';
31
32
    /**
33
     * Current page the user is browsing. ie browse.
34
     */
35
    public string $page = '';
36
37
    public string $page_template = '';
38
39
    public User $userdata;
40
41
    /**
42
     * User's theme.
43
     */
44
    protected string $theme = 'Gentele';
45
46
    /**
47
     * @var \Illuminate\Foundation\Application|mixed
48
     */
49
    public mixed $smarty;
50
51
    /**
52
     * BasePageController constructor.
53
     *
54
     * @throws \Exception
55
     */
56
    public function __construct()
57
    {
58
        
59
        // Buffer settings/DB connection.
60
        $this->settings = new Settings;
61
        $this->smarty = app('smarty.view');
62
63
        foreach (Arr::get(config('ytake-laravel-smarty'), 'plugins_paths', []) as $plugins) {
64
            $this->smarty->addPluginsDir($plugins);
65
        }
66
67
        $this->smarty->assign('serverroot', url('/'));
68
    }
69
70
    public static function middleware(): array
71
    {
72
        return [
73
            new Middleware(['auth', 'web', '2fa'], except: ['api', 'contact', 'showContactForm', 'callback', 'getNzb', 'terms', 'capabilities', 'movie', 'apiSearch', 'tv', 'details', 'failed', 'showRssDesc', 'fullFeedRss', 'categoryFeedRss', 'cartRss', 'myMoviesRss', 'myShowsRss', 'release', 'reset', 'showLinkRequestForm']),
74
        ];
75
    }
76
77
    public function paginate($query, $totalCount, $items, $page, $path, $reqQuery): LengthAwarePaginator
78
    {
79
        return new LengthAwarePaginator($query, $totalCount, $items, $page, ['path' => $path, 'query' => $reqQuery]);
80
    }
81
82
    /**
83
     * @throws \Exception
84
     */
85
    protected function setPreferences(): void
86
    {
87
        if (Auth::check()) {
88
            $this->userdata = User::find(Auth::id());
89
            $this->setUserPreferences();
90
        } else {
91
            // Tell Smarty which directories to use for templates
92
            $this->smarty->setTemplateDir([
93
                'user' => config('ytake-laravel-smarty.template_path').'/Gentele',
94
                'shared' => config('ytake-laravel-smarty.template_path').'/shared',
95
                'default' => config('ytake-laravel-smarty.template_path').'/Gentele',
96
            ]);
97
98
            $this->smarty->assign(
99
                [
100
                    'isadmin' => false,
101
                    'ismod' => false,
102
                    'loggedin' => false,
103
                ]
104
            );
105
        }
106
107
        $this->smarty->assign(
108
            [
109
                'theme' => 'Gentele',
110
                'site' => $this->settings,
111
            ]
112
        );
113
    }
114
115
    public function isPostBack(Request $request): bool
116
    {
117
        return $request->isMethod('POST');
118
    }
119
120
    /**
121
     * Show 404 page.
122
     *
123
     * @param  null  $message
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $message is correct as it would always require null to be passed?
Loading history...
124
     */
125
    public function show404($message = null): View
126
    {
127
        if ($message !== null) {
0 ignored issues
show
introduced by
The condition $message !== null is always false.
Loading history...
128
            return view('errors.404')->with('Message', $message);
129
        }
130
131
        return view('errors.404');
132
    }
133
134
    public function render(): void
135
    {
136
        $this->smarty->display($this->page_template);
137
    }
138
139
    /**
140
     * @throws \Exception
141
     */
142
    protected function setUserPreferences(): void
143
    {
144
        $this->userdata->categoryexclusions = User::getCategoryExclusionById(Auth::id());
0 ignored issues
show
Bug introduced by
The property categoryexclusions does not seem to exist on App\Models\User.
Loading history...
Bug introduced by
It seems like Illuminate\Support\Facades\Auth::id() can also be of type null and string; however, parameter $userID of App\Models\User::getCategoryExclusionById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

144
        $this->userdata->categoryexclusions = User::getCategoryExclusionById(/** @scrutinizer ignore-type */ Auth::id());
Loading history...
145
146
        // Update last login every 15 mins.
147
        if (now()->subHours(3) > $this->userdata->lastlogin) {
148
            event(new UserLoggedIn($this->userdata));
149
        }
150
151
        $this->smarty->assign('userdata', $this->userdata);
152
        $this->smarty->assign('loggedin', 'true');
153
154
        if ($this->userdata->hasRole('Admin')) {
155
            $this->smarty->assign('isadmin', 'true');
156
        }
157
158
        if ($this->userdata->hasRole('Moderator')) {
159
            $this->smarty->assign('ismod', 'true');
160
        }
161
162
        // Tell Smarty which directories to use for templates
163
        $this->smarty->setTemplateDir([
164
            'user' => config('ytake-laravel-smarty.template_path').'/Gentele',
165
            'shared' => config('ytake-laravel-smarty.template_path').'/shared',
166
            'default' => config('ytake-laravel-smarty.template_path').'/Gentele',
167
        ]);
168
169
        $role = $this->userdata->roles_id ?? User::ROLE_USER;
0 ignored issues
show
Unused Code introduced by
The assignment to $role is dead and can be removed.
Loading history...
170
171
        $content = new Contents;
0 ignored issues
show
Unused Code introduced by
The assignment to $content is dead and can be removed.
Loading history...
172
        $parentcatlist = Category::getForMenu($this->userdata->categoryexclusions);
173
174
        $this->smarty->assign('parentcatlist', $parentcatlist);
175
        $this->smarty->assign('catClass', Category::class);
176
177
        if (\request()->has('t')) {
178
            $this->smarty->assign('header_menu_cat', \request()->input('t'));
179
        } else {
180
            $this->smarty->assign('header_menu_cat', '');
181
        }
182
        $header_menu = $this->smarty->fetch('headermenu.tpl');
183
        $this->smarty->assign('header_menu', $header_menu);
184
        $notification = $this->smarty->fetch('notification.tpl');
185
        $this->smarty->assign('notification', $notification);
186
        $sidebar = $this->smarty->fetch('sidebar.tpl');
187
        $this->smarty->assign('sidebar', $sidebar);
188
        $footer = $this->smarty->fetch('footer.tpl');
189
        $this->smarty->assign('footer', $footer);
190
    }
191
192
    /**
193
     *  Set admin preferences.
194
     */
195
    public function setAdminPrefs(): void
196
    {
197
        // Tell Smarty which directories to use for templates
198
        $this->smarty->setTemplateDir(
199
            [
200
                'admin' => config('ytake-laravel-smarty.template_path').'/admin',
201
                'shared' => config('ytake-laravel-smarty.template_path').'/shared',
202
                'default' => config('ytake-laravel-smarty.template_path').'/admin',
203
            ]
204
        );
205
206
        $this->smarty->assign('catClass', Category::class);
207
    }
208
209
    /**
210
     * Output the page.
211
     */
212
    public function pagerender(): void
213
    {
214
        $this->page_template = 'basepage.tpl';
215
216
        $this->render();
217
    }
218
219
    /**
220
     * Output a page using the admin template.
221
     *
222
     * @throws \Exception
223
     */
224
    public function adminrender(): void
225
    {
226
        $admin_menu = $this->smarty->fetch('adminmenu.tpl');
227
        $this->smarty->assign('admin_menu', $admin_menu);
228
229
        $this->page_template = 'baseadminpage.tpl';
230
231
        $this->render();
232
    }
233
234
    /**
235
     * @throws \Exception
236
     */
237
    public function adminBasePage(): void
238
    {
239
        $this->setAdminPrefs();
240
        $this->smarty->assign([
241
            'meta_title' => 'Admin Home',
242
            'meta_description' => 'Admin home page',
243
        ]);
244
        $this->adminrender();
245
    }
246
}
247