Passed
Push — master ( 5aafeb...af0530 )
by Karel
12:33 queued 05:20
created

FrontEndController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 44
c 1
b 0
f 1
dl 0
loc 84
rs 10
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 0 53 13
A __construct() 0 14 1
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 Illuminate\Http\Request;
14
use Illuminate\Foundation\Bus\DispatchesJobs;
15
use Illuminate\Routing\Controller as BaseController;
16
use Illuminate\Foundation\Validation\ValidatesRequests;
17
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
18
19
class FrontEndController extends BaseController
20
{
21
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
22
23
    private $page;
24
    private $pageblock;
25
    private $pageBlockRepository;
26
    private $redirect;
27
    private $repeater;
28
    private $template;
29
    /**
30
     * Create a new controller instance.
31
     *
32
     * @return void
33
     */
34
    public function __construct(
35
        Page $page, 
36
        PageBlock $pageblock, 
37
        PageBlockRepository $pageBlockRepository, 
38
        Redirect $redirect, 
39
        Repeater $repeater, 
40
        Template $template)
41
    {
42
        $this->page = $page;
43
        $this->pageblock = $pageblock;
44
        $this->pageBlockRepository = $pageBlockRepository;
45
        $this->redirect = $redirect;
46
        $this->repeater = $repeater;
47
        $this->template = $template;
48
    }
49
50
    public function index($slug = null)
51
    {
52
        if($slug == null){
53
            $page = $this->page->where('isHp', 1)->firstOrFail();
54
        } elseif($slug !== null){
55
            
56
            $redirect = $this->redirect->where('slug', $slug)->first();
57
            if($redirect !== null){
58
                return redirect($redirect->to, $redirect->type);
59
            }
60
61
            $repeater = $this->repeater->where('url', $slug)->first();
62
            if($repeater !== null){
63
                $templateHintpath = explode('::', (string)$repeater->page)[0];
64
                $template = $this->template->where('active', 1)->where('hintpath', $templateHintpath)->first();
65
                return view((string)$repeater->page, compact('template', 'repeater'));
66
            }
67
68
            $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

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

78
                        app()->/** @scrutinizer ignore-call */ setLocale($localeCode); 
Loading history...
79
                        \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...
80
                        
81
                        return redirect($localeCode.'/'.$slug);
82
                    } 
83
                } 
84
            }
85
86
            if($page == null) {
87
              abort(404);  
88
            } 
89
        }
90
        
91
        $ogpageblocks = $this->pageblock->getAllByPageId($page->id);
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...
92
        $pageblocks = $this->pageBlockRepository->getRenderedByPageBlocks($ogpageblocks);
93
        
94
        if($page->page !== null) {
95
            $template = $this->template->where('active', 1)->where('hintpath', explode('::', $page->page)[0])->first();
96
97
            return view($page->page, compact('template', 'page', 'pageblocks'));
98
        }
99
100
        $template = $this->template->where('active', 1)->where('id', $page->template_id)->first();
101
        
102
        return view($template->hintpath . '::templates.' . $template->slug . '.page', compact('template', 'page', 'pageblocks'));
103
    }
104
}
105