Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( 45ef44...10db15 )
by Mark
02:54
created

Frontpage::publish()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 4
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Marky
5
 * Date: 31/12/2017
6
 * Time: 00:12.
7
 */
8
9
namespace App\Classes\Library\PageLoader;
10
11
use App\Model\Page;
12
use App\Model\Role;
13
use Illuminate\Http\Response;
14
use Illuminate\Support\Collection;
15
use App\Http\Controllers\ErrorController;
16
use App\Classes\Repositories\MenuRepository;
17
use Illuminate\Contracts\Routing\ResponseFactory;
18
19
/**
20
 * Class Frontpage
21
 *
22
 * @package App\Classes\Library\PageLoader
23
 */
24
class Frontpage
25
{
26
    /**
27
     * @var Response
28
     */
29
    private $response;
30
31
    /**
32
     * @var Webpage
33
     */
34
    private $webpage;
35
36
    /**
37
     * @var Page
38
     */
39
    private $model;
40
41
    /**
42
     * Frontpage constructor.
43
     * @param Page $model
44
     * @param Collection $navigationRepository
45
     */
46
    public function __construct(Page $model, Collection $navigationRepository)
47
    {
48
        $this->response = app(ResponseFactory::class);
49
50
        $this->webpage = new Webpage($this->model = $model, $navigationRepository);
51
    }
52
53
    /**
54
     * @param string|null $template
55
     * @param bool $override
56
     * @param int $status
57
     * @param bool $errorResponse
58
     * @return Response
59
     */
60
    public function publish(string $template = null, bool $override = true, int $status = 200, bool $errorResponse = false)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
61
    {
62
        if (!$errorResponse) {
63
            if ($this->isMaintenanceMode() && ! $this->canBypassMaintenance()) {
64
                return ErrorController::maintenance();
65
            }
66
67
            if ($this->isDisabledPage($this->model)) {
68
                return ErrorController::disabled();
69
            }
70
        }
71
72
        return $this->response->view($this->makeBlade($template, $override), ['webpage' => $this->webpage], $status);
73
    }
74
75
    /**
76
     * A user can disable a webpage from being viewed.
77
     *
78
     * @param Page $page
79
     * @return bool
80
     */
81
    private function isDisabledPage(Page $page)
82
    {
83
        return $page->enabled == false;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
84
    }
85
86
    /**
87
     * Check if the website is in maintenance mode
88
     * which is set by the user on the dashboard.
89
     *
90
     * @return bool
91
     */
92
    public function isMaintenanceMode()
93
    {
94
        return settings()->getValue('maintenance_mode');
95
    }
96
97
    /**
98
     * Check if the current logged in user if exists,
99
     * can bypass the maintenance and view it offline.
100
     *
101
     * @return bool
102
     */
103
    public function canBypassMaintenance()
104
    {
105
        return auth()->check() == true;
0 ignored issues
show
Bug introduced by
The method check does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
106
    }
107
108
    /**
109
     * @param string|null $template
110
     * @param bool $override
111
     * @return string
112
     */
113
    private function makeBlade(string $template = null, bool $override = true)
114
    {
115
        if ($template == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $template of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
116
            $template = currentURI() == 'index' ? 'website::index' : 'website::page';
117
        }
118
119
        if ($override && view()->exists("website::plugin.{$this->model->slug}")) {
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...
120
            $template = "website::plugin.{$this->model->slug}";
121
        }
122
123
        return $template;
124
    }
125
126
    /**
127
     * @param string $title
128
     * @param string $description
129
     * @param string $template
130
     * @param int $response
131
     * @return Response
132
     */
133
    public static function build(string $title, string $description, string $template, int $response)
134
    {
135
        $page = new Page(['seo_title' => $title, 'seo_description' => $description]);
136
137
        $navigation = app(MenuRepository::class)->allParentsWithChildren();
138
139
        return (new self($page, $navigation))->publish("errors::{$template}", false, $response, true);
140
    }
141
142
    /**
143
     * Return a drafted page without response headers.
144
     *
145
     * @return Webpage
146
     */
147
    public function draft()
148
    {
149
        return $this->webpage;
150
    }
151
}
152