BasePageController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 36
c 0
b 0
f 0
dl 0
loc 130
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A show404() 0 7 2
A pagerender() 0 3 1
A paginate() 0 3 1
A setAdminPrefs() 0 3 1
A adminBasePage() 0 9 1
A __construct() 0 26 2
A adminrender() 0 3 1
A isPostBack() 0 3 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Category;
6
use App\Models\Settings;
7
use App\Models\User;
8
use Illuminate\Http\Request;
9
use Illuminate\Pagination\LengthAwarePaginator;
10
use Illuminate\Support\Facades\Auth;
11
use Illuminate\View\View;
12
13
class BasePageController extends Controller
14
{
15
    public \Illuminate\Support\Collection $settings;
16
17
    public string $title = '';
18
19
    public string $content = '';
20
21
    public string $meta_keywords = '';
22
23
    public string $meta_title = '';
24
25
    public string $meta_description = '';
26
27
    /**
28
     * Current page the user is browsing. ie browse.
29
     */
30
    public string $page = '';
31
32
    public User $userdata;
33
34
    /**
35
     * User's theme.
36
     */
37
    protected string $theme = 'Gentele';
38
39
    /**
40
     * View data array for Blade templates
41
     */
42
    protected array $viewData = [];
43
44
    /**
45
     * BasePageController constructor.
46
     *
47
     * @throws \Exception
48
     */
49
    public function __construct()
50
    {
51
        $this->middleware(['auth', 'web', '2fa'])->except('api', 'contact', 'showContactForm', 'callback', 'btcPayCallback', 'getNzb', 'terms', 'privacyPolicy', 'capabilities', 'movie', 'apiSearch', 'tv', 'details', 'failed', 'showRssDesc', 'fullFeedRss', 'categoryFeedRss', 'cartRss', 'myMoviesRss', 'myShowsRss', 'trendingMoviesRss', 'trendingShowsRss', 'release', 'reset', 'showLinkRequestForm');
52
53
        // Load settings as collection
54
        $this->settings = Settings::query()->pluck('value', 'name');
55
56
        // Initialize view data FIRST with serverroot
57
        $this->viewData = [
58
            'serverroot' => url('/'),
59
        ];
60
61
        // Then add the converted settings array as 'site'
62
        // Using array assignment instead of constructor assignment to ensure it persists
63
        $this->viewData['site'] = $this->settings->map(function ($value) {
64
            return Settings::convertValue($value);
65
        })->all();
66
67
        // Initialize userdata property for controllers that need it
68
        $this->middleware(function ($request, $next) {
69
            if (Auth::check()) {
70
                $this->userdata = User::find(Auth::id());
71
                $this->userdata->categoryexclusions = User::getCategoryExclusionById(Auth::id());
72
            }
73
74
            return $next($request);
75
        });
76
    }
77
78
    public function paginate($query, $totalCount, $items, $page, $path, $reqQuery): LengthAwarePaginator
79
    {
80
        return new LengthAwarePaginator($query, $totalCount, $items, $page, ['path' => $path, 'query' => $reqQuery]);
81
    }
82
83
    /**
84
     * Check if request is POST.
85
     */
86
    public function isPostBack(Request $request): bool
87
    {
88
        return $request->isMethod('POST');
89
    }
90
91
    /**
92
     * Show 404 page.
93
     *
94
     * @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...
95
     */
96
    public function show404($message = null): View
97
    {
98
        if ($message !== null) {
0 ignored issues
show
introduced by
The condition $message !== null is always false.
Loading history...
99
            return view('errors.404')->with('Message', $message);
100
        }
101
102
        return view('errors.404');
103
    }
104
105
    /**
106
     *  Set admin preferences.
107
     */
108
    public function setAdminPrefs(): void
109
    {
110
        $this->viewData['catClass'] = Category::class;
111
    }
112
113
    /**
114
     * Output the page using Blade views.
115
     */
116
    public function pagerender(): View
117
    {
118
        return view('layouts.main', $this->viewData);
119
    }
120
121
    /**
122
     * Output a page using the admin template.
123
     *
124
     * @throws \Exception
125
     */
126
    public function adminrender(): View
127
    {
128
        return view('layouts.admin', $this->viewData);
129
    }
130
131
    /**
132
     * @throws \Exception
133
     */
134
    public function adminBasePage(): View
135
    {
136
        $this->setAdminPrefs();
137
        $this->viewData = array_merge($this->viewData, [
138
            'meta_title' => 'Admin Home',
139
            'meta_description' => 'Admin home page',
140
        ]);
141
142
        return $this->adminrender();
143
    }
144
}
145