FrontEndController   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 79
dl 0
loc 162
rs 9.92
c 1
b 0
f 1
wmc 31

7 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 0 46 13
A __construct() 0 15 1
A css() 0 6 1
B concept() 0 33 10
A js() 0 6 1
A authorizedIndex() 0 11 3
A getView() 0 14 2
1
<?php
2
3
namespace Chuckbe\Chuckcms\Controllers;
4
5
use Chuckbe\Chuckcms\Chuck\PageBlockRepository;
6
use Chuckbe\Chuckcms\Models\Page;
7
use Chuckbe\Chuckcms\Models\PageBlock;
8
use Chuckbe\Chuckcms\Models\Redirect;
9
use Chuckbe\Chuckcms\Models\Repeater;
10
use Chuckbe\Chuckcms\Models\Template;
11
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
12
use Illuminate\Foundation\Bus\DispatchesJobs;
13
use Illuminate\Foundation\Validation\ValidatesRequests;
14
use Illuminate\Routing\Controller as BaseController;
15
use Illuminate\Support\Facades\Auth;
16
use Spatie\Permission\Exceptions\UnauthorizedException;
17
use Spatie\Permission\Models\Role;
18
19
class FrontEndController extends BaseController
20
{
21
    use AuthorizesRequests;
22
    use DispatchesJobs;
23
    use ValidatesRequests;
24
25
    private $page;
26
    private $pageblock;
27
    private $pageBlockRepository;
28
    private $redirect;
29
    private $repeater;
30
    private $role;
0 ignored issues
show
introduced by
The private property $role is not used, and could be removed.
Loading history...
31
    private $template;
32
33
    /**
34
     * Create a new controller instance.
35
     *
36
     * @return void
37
     */
38
    public function __construct(
39
        Page $page,
40
        PageBlock $pageblock,
41
        PageBlockRepository $pageBlockRepository,
42
        Redirect $redirect,
43
        Repeater $repeater,
44
        Role $role,
0 ignored issues
show
Unused Code introduced by
The parameter $role is not used and could be removed. ( Ignorable by Annotation )

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

44
        /** @scrutinizer ignore-unused */ Role $role,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
        Template $template
46
    ) {
47
        $this->page = $page;
48
        $this->pageblock = $pageblock;
49
        $this->pageBlockRepository = $pageBlockRepository;
50
        $this->redirect = $redirect;
51
        $this->repeater = $repeater;
52
        $this->template = $template;
53
    }
54
55
    public function index($slug = null)
56
    {
57
        if ($slug == null) {
58
            $page = $this->page->where('isHp', 1)->firstOrFail();
59
        } elseif ($slug !== null) {
60
            $redirect = $this->redirect->where('slug', $slug)->first();
61
            if ($redirect !== null) {
62
                return redirect($redirect->to, $redirect->type);
63
            }
64
65
            $repeater = $this->repeater->where('url', $slug)->first();
66
            if ($repeater !== null) {
67
                $templateHintpath = explode('::', (string) $repeater->page)[0];
68
                $template = $this->template->where('active', 1)->where('hintpath', $templateHintpath)->first();
69
70
                return view((string) $repeater->page, compact('template', 'repeater'));
71
            }
72
73
            $page = $this->page->where('slug->'.app()->getLocale(), $slug)->first();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

73
            $page = $this->page->where('slug->'.app()->/** @scrutinizer ignore-call */ getLocale(), $slug)->first();
Loading history...
74
            if ($page == null) {
75
                foreach (\LaravelLocalization::getSupportedLocales() as $localeCode => $properties) {
76
                    $page = $this->page->where('slug->'.$localeCode, $slug)->first();
77
                    if ($page !== null && $localeCode == app()->getLocale()) {
78
                        break;
79
                    }
80
81
                    if ($page !== null && $localeCode !== app()->getLocale()) {
82
                        //dd(app()->getLocale());
83
                        app()->setLocale($localeCode);
0 ignored issues
show
introduced by
The method setLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

83
                        app()->/** @scrutinizer ignore-call */ setLocale($localeCode);
Loading history...
84
                        \LaravelLocalization::setLocale($localeCode);
0 ignored issues
show
Bug introduced by
The type LaravelLocalization 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...
85
86
                        return redirect($localeCode.'/'.$slug);
87
                    }
88
                }
89
            }
90
91
            if ($page == null) {
92
                abort(404);
93
            }
94
        }
95
96
        if ($page->roles !== null) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $page does not seem to be defined for all execution paths leading up to this point.
Loading history...
97
            return $this->authorizedIndex($page);
98
        }
99
100
        return $this->getView($page);
101
    }
102
103
    public function concept($slug = null)
104
    {
105
        if ($slug == null) {
106
            return redirect('/');
107
        }
108
109
        $page = $this->page->where('slug->'.app()->getLocale(), $slug)->first();
110
        if ($page == null) {
111
            foreach (\LaravelLocalization::getSupportedLocales() as $localeCode => $properties) {
112
                $page = $this->page->where('slug->'.$localeCode, $slug)->first();
113
                if ($page !== null && $localeCode == app()->getLocale()) {
114
                    break;
115
                }
116
117
                if ($page !== null && $localeCode !== app()->getLocale()) {
118
                    //dd(app()->getLocale());
119
                    app()->setLocale($localeCode);
120
                    \LaravelLocalization::setLocale($localeCode);
121
122
                    return redirect($localeCode.'/concept/'.$slug);
123
                }
124
            }
125
        }
126
127
        if ($page == null) {
128
            abort(404);
129
        }
130
131
        if ($page->roles !== null) {
132
            return $this->authorizedIndex($page);
133
        }
134
135
        return $this->getView($page);
136
    }
137
138
    public function authorizedIndex(Page $page)
139
    {
140
        if (!Auth::check()) {
141
            return redirect()->guest('login');
142
        }
143
        $roles = Role::whereIn('id', explode('|', $page->roles))->select('name')->get()->toArray();
144
        if (!Auth::user()->hasAnyRole($roles)) {
0 ignored issues
show
Bug introduced by
The method hasAnyRole() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

144
        if (!Auth::user()->/** @scrutinizer ignore-call */ hasAnyRole($roles)) {
Loading history...
145
            throw UnauthorizedException::forRoles($roles);
146
        }
147
148
        return $this->getView($page);
149
    }
150
151
    public function getView(Page $page)
152
    {
153
        $ogpageblocks = $this->pageblock->getAllByPageId($page->id);
154
        $pageblocks = $this->pageBlockRepository->getRenderedByPageBlocks($ogpageblocks);
155
156
        if ($page->page !== null) {
157
            $template = $this->template->where('active', 1)->where('hintpath', explode('::', $page->page)[0])->first();
158
159
            return view($page->page, compact('template', 'page', 'pageblocks'));
160
        }
161
162
        $template = $this->template->where('active', 1)->where('id', $page->template_id)->first();
163
164
        return view($template->hintpath.'::templates.'.$template->slug.'.page', compact('template', 'page', 'pageblocks'));
165
    }
166
167
    public function css(Page $page)
168
    {
169
        $response = response()->make($page->css);
170
        $response->header('Content-Type', 'text/css');
171
172
        return $response;
173
    }
174
175
    public function js(Page $page)
176
    {
177
        $response = response()->make($page->js);
178
        $response->header('Content-Type', 'text/javascript');
179
180
        return $response;
181
    }
182
}
183