PublicController::throw404IfNotFound()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
1
<?php namespace Modules\Page\Http\Controllers;
2
3
use Illuminate\Contracts\Foundation\Application;
4
use Modules\Core\Http\Controllers\BasePublicController;
5
use Modules\Page\Repositories\PageRepository;
6
7
class PublicController extends BasePublicController
8
{
9
    /**
10
     * @var PageRepository
11
     */
12
    private $page;
13
    /**
14
     * @var Application
15
     */
16
    private $app;
17
18
    public function __construct(PageRepository $page, Application $app)
19
    {
20
        parent::__construct();
21
        $this->page = $page;
22
        $this->app = $app;
23
    }
24
25
    /**
26
     * @param $slug
27
     * @return \Illuminate\View\View
28
     */
29 View Code Duplication
    public function uri($slug)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $page = $this->page->findBySlug($slug);
32
33
        $this->throw404IfNotFound($page);
34
35
        $template = $this->getTemplateForPage($page);
36
37
        return view($template, compact('page'));
38
    }
39
40
    /**
41
     * @return \Illuminate\View\View
42
     */
43 View Code Duplication
    public function homepage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $page = $this->page->findHomepage();
46
47
        $this->throw404IfNotFound($page);
48
49
        $template = $this->getTemplateForPage($page);
50
51
        return view($template, compact('page'));
52
    }
53
54
    /**
55
     * Return the template for the given page
56
     * or the default template if none found
57
     * @param $page
58
     * @return string
59
     */
60
    private function getTemplateForPage($page)
61
    {
62
        return (view()->exists($page->template)) ? $page->template : 'default';
63
    }
64
65
    /**
66
     * Throw a 404 error page if the given page is not found
67
     * @param $page
68
     */
69
    private function throw404IfNotFound($page)
70
    {
71
        if (is_null($page)) {
72
            $this->app->abort('404');
73
        }
74
    }
75
}
76