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
Pull Request — master (#61)
by Oliver
01:39
created

UniquePageCrudController::setRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Backpack\PageManager\app\Http\Controllers\Admin;
4
5
use App\UniquePages;
6
use Illuminate\Http\Request;
7
use Backpack\PageManager\app\TraitReflections;
8
use Backpack\CRUD\app\Http\Controllers\CrudController;
9
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\SaveActions;
10
11
class UniquePageCrudController extends CrudController
12
{
13
    use SaveActions;
14
    use UniquePages;
15
    use TraitReflections;
16
17
    public function setup()
18
    {
19
        parent::setup();
20
21
        $modelClass = config('backpack.pagemanager.unique_page_model_class', 'Backpack\PageManager\app\Models\Page');
22
23
        $this->checkForTemplatesAndUniquePagesNotDistinct();
24
25
        /*
26
        |--------------------------------------------------------------------------
27
        | BASIC CRUD INFORMATION
28
        |--------------------------------------------------------------------------
29
        */
30
        $this->crud->setModel($modelClass);
31
        // Don't set route or entity names here. These depend on the page you are editing
32
33
        $this->crud->denyAccess(['list', 'create', 'delete']);
34
35
        if (config('backpack.pagemanager.unique_page_revisions')) {
36
            $this->crud->allowAccess('revisions');
37
        }
38
    }
39
40
    /**
41
     * Edit the unique page retrieved by slug.
42
     *
43
     * @param string $slug
44
     * @return Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
45
     */
46
    public function uniqueEdit($slug)
47
    {
48
        $model = $this->crud->model;
49
        $entry = $model::findBySlug($slug);
50
51
        if (! $entry) {
52
            $entry = $this->createMissingPage($slug);
53
        }
54
55
        $this->uniqueSetup($entry);
56
57
        return parent::edit($entry->id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (edit() instead of uniqueEdit()). Are you sure this is correct? If so, you might want to change this to $this->edit().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
58
    }
59
60
    /**
61
     * Update the unique page.
62
     *
63
     * @param string $slug
64
     * @param int $id
65
     * @return \Illuminate\Http\RedirectResponse
66
     */
67 View Code Duplication
    public function update($slug, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
68
    {
69
        $model = $this->crud->model;
70
        $entry = $model::findBySlugOrFail($slug);
71
72
        $this->uniqueSetup($entry);
73
74
        return parent::updateCrud();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (updateCrud() instead of update()). Are you sure this is correct? If so, you might want to change this to $this->updateCrud().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
75
    }
76
77
    /**
78
     * Set the crud route.
79
     *
80
     * @param $slug
81
     */
82
    public function setRoute($slug)
83
    {
84
        $this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug);
85
    }
86
87
    /**
88
     * Populate the update form with basic fields that all pages need.
89
     *
90
     * @param Model $page
91
     */
92
    public function addDefaultPageFields($page)
93
    {
94
        $fields = [
95
            [
96
                'name'  => 'buttons',
97
                'type'  => 'custom_html',
98
                'value' => $this->buttons($page),
99
            ],
100
            [
101
                'name' => 'template',
102
                'type' => 'hidden',
103
            ],
104
            [
105
                'name' => 'name',
106
            ],
107
            [
108
                'name' => 'title',
109
            ],
110
            [
111
                'name' => 'slug',
112
                'type' => 'hidden',
113
            ],
114
        ];
115
116
        $this->crud->addFields($fields);
117
    }
118
119
    /**
120
     * Build the buttons html for the edit form.
121
     *
122
     * @param Model $page
123
     * @return string
124
     */
125
    public function buttons($page)
126
    {
127
        $openButton = $page->getOpenButton();
128
        $revisionsButton = view('crud::buttons.revisions', ['crud' => $this->crud, 'entry' => $page]);
129
130
        return $openButton.' '.$revisionsButton;
131
    }
132
133
    /**
134
     * Create missing unique page by slug.
135
     *
136
     * @param $slug
137
     * @return mixed
138
     */
139
    public function createMissingPage($slug)
140
    {
141
        $slugs = $this->getUniqueSlugs();
142
143
        if (! $slugs->has($slug)) {
144
            abort(404);
145
        }
146
147
        $page = $slugs->pull($slug);
148
        $model = $this->crud->model;
149
150
        return $model::create([
151
            'template' => $page,
152
            'name'     => camel_case($page),
153
            'title'    => camel_case($page),
154
            'slug'     => $slug,
155
        ]);
156
    }
157
158
    /**
159
     * Display the revisions for specified resource.
160
     *
161
     * @param $slug
162
     * @param $id
163
     * @return Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
164
     */
165 View Code Duplication
    public function uniqueRevisions($slug, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
166
    {
167
        $model = $this->crud->model;
168
        $entry = $model::findBySlugOrFail($slug);
169
170
        $this->uniqueSetup($entry);
171
172
        return parent::listRevisions($entry->id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (listRevisions() instead of uniqueRevisions()). Are you sure this is correct? If so, you might want to change this to $this->listRevisions().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
173
    }
174
175
    /**
176
     * Restore a specific revision for the specified resource.
177
     *
178
     * Used via AJAX in the revisions view
179
     *
180
     * @param string $slug
181
     * @param int $id
182
     *
183
     * @return JSON Response containing the new revision that was created from the update
184
     * @return HTTP 500 if the request did not contain the revision ID
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\Illuminate\View\Vi...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
185
     */
186 View Code Duplication
    public function restoreUniqueRevision($slug, $id)
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...
187
    {
188
        $model = $this->crud->model;
189
        $entry = $model::findBySlugOrFail($slug);
190
191
        $this->uniqueSetup($entry);
192
193
        return parent::restoreRevision($id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (restoreRevision() instead of restoreUniqueRevision()). Are you sure this is correct? If so, you might want to change this to $this->restoreRevision().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
194
    }
195
196
    /**
197
     * Setup the controller for the entry.
198
     *
199
     * @param $entry
200
     */
201
    protected function uniqueSetup($entry)
202
    {
203
        $this->crud->entry = $entry;
204
205
        $this->setRoute($entry->slug);
206
207
        $this->addDefaultPageFields($entry);
208
        $this->crud->setEntityNameStrings($this->crud->makeLabel($entry->template), $this->crud->makeLabel($entry->template));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 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...
209
210
        $this->{$entry->template}();
211
    }
212
213
    /*
214
    |--------------------------------------------------------------------------
215
    | SaveActions overrides
216
    |--------------------------------------------------------------------------
217
    */
218
219
    /**
220
     * Overrides trait version to remove 'save_and_back' and 'save_and_new'.
221
     *
222
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
223
     */
224
    public function getSaveAction()
225
    {
226
        $saveCurrent = [
227
            'value' => $this->getSaveActionButtonName('save_and_back'),
228
            'label' => $this->getSaveActionButtonName('save_and_back'),
229
        ];
230
231
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('active' =>... 'options' => array()); (array<string,array>) is incompatible with the return type of the parent method Backpack\CRUD\app\Http\C...ntroller::getSaveAction of type array<string,array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
232
            'active'  => $saveCurrent,
233
            'options' => [],
234
        ];
235
    }
236
237
    /**
238
     * Override trait version to not update the session variable.
239
     * This way we preserve the user chosen save action and don't overwrite with
240
     *
241
     * @param [type] $forceSaveAction [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
242
     */
243
    public function setSaveAction($forceSaveAction = null)
244
    {
245
        // do nothing to preserve session value for other crud
246
    }
247
}
248