Completed
Push — 2.0 ( 3f307a...012e6b )
by Nicolas
03:48
created

PublicController::findPageForSlug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Modules\Page\Http\Controllers;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Modules\Core\Http\Controllers\BasePublicController;
7
use Modules\Menu\Repositories\MenuItemRepository;
8
use Modules\Page\Entities\Page;
9
use Modules\Page\Repositories\PageRepository;
10
11
class PublicController extends BasePublicController
12
{
13
    /**
14
     * @var PageRepository
15
     */
16
    private $page;
17
    /**
18
     * @var Application
19
     */
20
    private $app;
21
22
    public function __construct(PageRepository $page, Application $app)
23
    {
24
        parent::__construct();
25
        $this->page = $page;
26
        $this->app = $app;
27
    }
28
29
    /**
30
     * @param $slug
31
     * @return \Illuminate\View\View
32
     */
33 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...
34
    {
35
        $page = $this->findPageForSlug($slug);
36
37
        $this->throw404IfNotFound($page);
38
39
        $template = $this->getTemplateForPage($page);
40
41
        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 41 which is incompatible with the return type documented by Modules\Page\Http\Contro...s\PublicController::uri of type Illuminate\View\View.
Loading history...
42
    }
43
44
    /**
45
     * @return \Illuminate\View\View
46
     */
47 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...
48
    {
49
        $page = $this->page->findHomepage();
50
51
        $this->throw404IfNotFound($page);
52
53
        $template = $this->getTemplateForPage($page);
54
55
        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 55 which is incompatible with the return type documented by Modules\Page\Http\Contro...licController::homepage of type Illuminate\View\View.
Loading history...
56
    }
57
58
    /**
59
     * Find a page for the given slug.
60
     * The slug can be a 'composed' slug via the Menu
61
     * @param string $slug
62
     * @return Page
63
     */
64
    private function findPageForSlug($slug)
65
    {
66
        $menuItem = app(MenuItemRepository::class)->findByUriInLanguage($slug, locale());
67
68
        if ($menuItem) {
69
            return $this->page->find($menuItem->page_id);
70
        }
71
72
        return $this->page->findBySlug($slug);
73
    }
74
75
    /**
76
     * Return the template for the given page
77
     * or the default template if none found
78
     * @param $page
79
     * @return string
80
     */
81
    private function getTemplateForPage($page)
82
    {
83
        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...
84
    }
85
86
    /**
87
     * Throw a 404 error page if the given page is not found
88
     * @param $page
89
     */
90
    private function throw404IfNotFound($page)
91
    {
92
        if (is_null($page)) {
93
            $this->app->abort('404');
94
        }
95
    }
96
}
97