Passed
Push — master ( 957889...d2fe6b )
by Karel
08:00
created

FrontEndController::getView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Chuckbe\Chuckcms\Controllers;
4
5
use Chuckbe\Chuckcms\Chuck\PageBlockRepository;
6
7
use Chuckbe\Chuckcms\Models\Page;
8
use Chuckbe\Chuckcms\Models\PageBlock;
9
use Chuckbe\Chuckcms\Models\Redirect;
10
use Chuckbe\Chuckcms\Models\Repeater;
11
use Chuckbe\Chuckcms\Models\Template;
12
13
use Spatie\Permission\Models\Role;
14
use Spatie\Permission\Exceptions\UnauthorizedException;
15
16
use Illuminate\Http\Request;
17
use Illuminate\Foundation\Bus\DispatchesJobs;
18
use Illuminate\Routing\Controller as BaseController;
19
use Illuminate\Foundation\Validation\ValidatesRequests;
20
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
21
use Illuminate\Support\Facades\Auth;
22
23
class FrontEndController extends BaseController
24
{
25
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
26
27
    private $page;
28
    private $pageblock;
29
    private $pageBlockRepository;
30
    private $redirect;
31
    private $repeater;
32
    private $role;
0 ignored issues
show
introduced by
The private property $role is not used, and could be removed.
Loading history...
33
    private $template;
34
    /**
35
     * Create a new controller instance.
36
     *
37
     * @return void
38
     */
39
    public function __construct(
40
        Page $page, 
41
        PageBlock $pageblock, 
42
        PageBlockRepository $pageBlockRepository, 
43
        Redirect $redirect, 
44
        Repeater $repeater, 
45
        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

45
        /** @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...
46
        Template $template)
47
    {
48
        $this->page = $page;
49
        $this->pageblock = $pageblock;
50
        $this->pageBlockRepository = $pageBlockRepository;
51
        $this->redirect = $redirect;
52
        $this->repeater = $repeater;
53
        $this->template = $template;
54
    }
55
56
    public function index($slug = null)
57
    {
58
        if($slug == null){
59
            $page = $this->page->where('isHp', 1)->firstOrFail();
60
        } elseif($slug !== null){
61
            
62
            $redirect = $this->redirect->where('slug', $slug)->first();
63
            if($redirect !== null){
64
                return redirect($redirect->to, $redirect->type);
65
            }
66
67
            $repeater = $this->repeater->where('url', $slug)->first();
68
            if($repeater !== null){
69
                $templateHintpath = explode('::', (string)$repeater->page)[0];
70
                $template = $this->template->where('active', 1)->where('hintpath', $templateHintpath)->first();
71
                return view((string)$repeater->page, compact('template', 'repeater'));
72
            }
73
74
            $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

74
            $page = $this->page->where('slug->'.app()->/** @scrutinizer ignore-call */ getLocale(), $slug)->first();
Loading history...
75
            if($page == null){
76
                foreach(\LaravelLocalization::getSupportedLocales() as $localeCode => $properties){
77
                    $page = $this->page->where('slug->'.$localeCode, $slug)->first();
78
                    if($page !== null && $localeCode == app()->getLocale()) {
79
                        break;
80
                    }
81
82
                    if($page !== null && $localeCode !== app()->getLocale()){
83
                        //dd(app()->getLocale());
84
                        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

84
                        app()->/** @scrutinizer ignore-call */ setLocale($localeCode); 
Loading history...
85
                        \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...
86
                        
87
                        return redirect($localeCode.'/'.$slug);
88
                    } 
89
                } 
90
            }
91
92
            if($page == null) {
93
              abort(404);  
94
            } 
95
        }
96
97
        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...
98
            return $this->authorizedIndex($page);
99
        }
100
101
        return $this->getView($page);
102
    }
103
104
    public function authorizedIndex(Page $page)
105
    {
106
        if(!Auth::check()) {
107
            return redirect()->guest('login');
108
        }
109
        $roles = Role::whereIn('id', explode('|', $page->roles))->select('name')->get()->toArray();
110
        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

110
        if(!Auth::user()->/** @scrutinizer ignore-call */ hasAnyRole($roles)) {
Loading history...
111
            throw UnauthorizedException::forRoles($roles);
112
        }
113
114
        return $this->getView($page);
115
    }
116
117
    public function getView(Page $page)
118
    {
119
        $ogpageblocks = $this->pageblock->getAllByPageId($page->id);
120
        $pageblocks = $this->pageBlockRepository->getRenderedByPageBlocks($ogpageblocks);
121
        
122
        if($page->page !== null) {
123
            $template = $this->template->where('active', 1)->where('hintpath', explode('::', $page->page)[0])->first();
124
125
            return view($page->page, compact('template', 'page', 'pageblocks'));
126
        }
127
128
        $template = $this->template->where('active', 1)->where('id', $page->template_id)->first();
129
        
130
        return view($template->hintpath . '::templates.' . $template->slug . '.page', compact('template', 'page', 'pageblocks'));
131
    }
132
}
133