Completed
Pull Request — master (#25)
by
unknown
02:43
created

PublicController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 69
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A uri() 0 10 1
A homepage() 0 10 1
A getTemplateForPage() 0 4 2
A throw404IfNotFound() 0 6 2
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
    public function uri($slug)
30
    {
31
        $page = $this->page->findBySlugInLocale($slug, $this->locale);
32
33
        $this->throw404IfNotFound($page);
34
35
        $template = $this->getTemplateForPage($page);
36
37
        return view($template, compact('page'));
0 ignored issues
show
Bug Compatibility introduced by
The expression view($template, compact('page')); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 37 which is incompatible with the return type documented by Modules\Page\Http\Contro...s\PublicController::uri of type Illuminate\View\View.
Loading history...
38
    }
39
40
    /**
41
     * @return \Illuminate\View\View
42
     */
43
    public function homepage()
44
    {
45
        $page = $this->page->findHomepage();
46
47
        $this->throw404IfNotFound($page);
48
49
        $template = $this->getTemplateForPage($page);
50
51
        return view($template, compact('page'));
0 ignored issues
show
Bug Compatibility introduced by
The expression view($template, compact('page')); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 51 which is incompatible with the return type documented by Modules\Page\Http\Contro...licController::homepage of type Illuminate\View\View.
Loading history...
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';
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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