PublicController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 28.99 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 1
cbo 1
dl 20
loc 69
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A uri() 10 10 1
A homepage() 10 10 1
A getTemplateForPage() 0 4 2
A throw404IfNotFound() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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