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

BackendController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 205
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A edit() 0 3 1
A store() 0 9 1
A reorder() 0 11 2
B save() 0 37 3
A show() 0 2 1
A destroy() 0 17 3
A update() 0 11 1
A index() 0 3 1
A create() 0 3 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Mark
5
 * Date: 13/03/2016
6
 * Time: 14:55.
7
 */
8
9
namespace App\Plugins\Menus;
10
11
use App\Model\Link;
12
use App\Model\Menu;
13
use App\Model\Activity;
14
use Illuminate\Http\Request;
15
use App\Plugins\PluginEngine;
16
use Illuminate\Validation\Rule;
17
use Illuminate\Support\Facades\DB;
18
use App\Classes\Repositories\LinkRepository;
19
use App\Classes\Repositories\MenuRepository;
20
use App\Classes\Repositories\PageRepository;
21
22
/**
23
 * Class Controller.
24
 */
25
class BackendController extends PluginEngine
26
{
27
    /**
28
     * @var MenuRepository
29
     */
30
    private $menus;
31
32
    /**
33
     * @var PageRepository
34
     */
35
    private $pages;
36
37
    /**
38
     * @var LinkRepository
39
     */
40
    private $links;
41
42
    /**
43
     * BackendController constructor.
44
     *
45
     * @param MenuRepository $menus
46
     * @param PageRepository $pages
47
     */
48
    public function __construct(MenuRepository $menus, PageRepository $pages, LinkRepository $links)
49
    {
50
        $this->menus = $menus;
51
52
        $this->pages = $pages;
53
54
        $this->links = $links;
55
    }
56
57
    /**
58
     * Display a listing of the resource.
59
     *
60
     * @param int $group_id
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function index($group_id = 1)
64
    {
65
        return $this->make('index')->with('menus', $this->menus->whereTopLevel())->with('list', $this->menus->whereParent($group_id));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->make('inde...whereParent($group_id)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
66
    }
67
68
    /**
69
     * Show the form for creating a new resource.
70
     *
71
     * @return \Illuminate\Http\Response
72
     */
73
    public function create()
74
    {
75
        return $this->make('create')->with('parents', $this->menus->whereTopLevelEditable())->with('linkableObjects', $this->links->allLinkableObjects());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->make('crea...->allLinkableObjects()) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
76
    }
77
78
    /**
79
     * Store a newly created resource in storage.
80
     *
81
     * External or Internal Hyperlinks and connections.
82
     *
83
     * @param Menu $menu
84
     * @param  \Illuminate\Http\Request $request
85
     * @return \Illuminate\Http\RedirectResponse
86
     */
87
    public function store(Request $request, Menu $menu)
88
    {
89
        $request->validate(['title' => 'min:3|max:255|unique:menus,title,NULL,id,deleted_at,NULL|required']);
90
91
        $this->save($request, $menu);
92
93
        account()->record(Activity::$created, $menu);
0 ignored issues
show
Bug introduced by
The method record() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        account()->/** @scrutinizer ignore-call */ record(Activity::$created, $menu);
Loading history...
94
95
        return redirect()->route('admin.menus.index');
96
    }
97
98
    /**
99
     * Display the specified resource.
100
     *
101
     * @param  int $id
102
     * @return void
103
     */
104
    public function show($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

104
    public function show(/** @scrutinizer ignore-unused */ $id)

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

Loading history...
105
    {
106
        //
107
    }
108
109
    /**
110
     * Show the form for editing the specified resource.
111
     *
112
     * @param MenuRepository $repository
113
     * @param  int $id
114
     * @return \Illuminate\Http\Response
115
     */
116
    public function edit($id)
117
    {
118
        return $this->make('edit')->with('menu', $this->menus->whereID($id))->with('parents', $this->menus->whereTopLevelEditable())->with('linkableObjects', $this->links->allLinkableObjects());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->make('edit...->allLinkableObjects()) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
119
    }
120
121
    /**
122
     * Update the specified resource in storage.
123
     *
124
     * @param Request $request
125
     * @param MenuRepository $repository
126
     * @param int $id
127
     * @return \Illuminate\Http\Response
128
     */
129
    public function update(Request $request, $id)
130
    {
131
        $request->validate(['title' => ['min:3|max:255|required', Rule::unique('menus')->ignore($id)]]);
132
133
        $menu = $this->menus->whereID($id);
134
135
        $this->save($request, $menu);
136
137
        account()->record(Activity::$updated, $menu);
138
139
        return redirect(route('admin.menus.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('admin.menus.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
140
    }
141
142
    /**
143
     * Remove the specified resource from storage.
144
     *
145
     * @param  int $id
146
     * @param MenuRepository $repository
147
     * @return bool|null
148
     * @throws \Exception
149
     */
150
    public function destroy($id)
151
    {
152
        $menu = $this->menus->whereID($id);
153
154
        if ($menu->children) {
155
            foreach ($menu->children as $submenu) {
156
                $submenu->link->delete();
157
            }
158
        }
159
160
        $menu->link->delete();
161
162
        account()->record(Activity::$deleted, $menu);
163
164
        $menu->delete();
165
166
        return redirect(route('admin.menus.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('admin.menus.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type null|boolean.
Loading history...
167
    }
168
169
    /**
170
     * Change the order the menu list.
171
     */
172
    public function reorder(Request $request)
173
    {
174
        // store the order increment id.
175
        $increment = 1;
176
177
        for ($i = 0; $i < count($request['data']); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
178
            $menu = $this->menus->whereID($request['data'][$i]);
179
180
            $menu->order = $increment++;
181
182
            $menu->save();
183
        }
184
    }
185
186
    /**
187
     * Save the data for the menu to the database.
188
     *
189
     * @param Request $request
190
     * @param Menu $menu
191
     * @return bool
192
     */
193
    private function save(Request $request, Menu $menu)
194
    {
195
        if (! $request['hyperlinkUrl']) {
196
            $model = json_decode($request['linkable_object']);
197
198
            $menu->hyperlink = null;
199
            $menu->title = $request['title'];
200
            $menu->page_id = $request['page_id'];
201
            $menu->parent_id = $request['menu_id'];
202
            $menu->target = $request['target'];
203
            $menu->status = true;
204
            $menu->creator_id = account()->id;
205
206
            DB::transaction(function () use ($menu, $model) {
207
                $menu->save();
208
209
                if ($menu->link) {
210
                    $menu->link->connect($menu, getMorphedModel($model->class, $model->key))->save();
211
                } else {
212
                    (new Link)->connect($menu, getMorphedModel($model->class, $model->key))->save();
213
                }
214
            }, 5);
215
216
            return true;
217
        } else {
218
            // we expect this to be a hyperlink.
219
            $request->validate(['hyperlinkUrl' => 'required|max:255|active_url']);
220
221
            $menu->page_id = null;
222
            $menu->title = $request['title'];
223
            $menu->parent_id = $request['menu_id'];
224
            $menu->hyperlink = $request['hyperlinkUrl'];
225
            $menu->target = $request['target'];
226
            $menu->status = true;
227
            $menu->creator_id = account()->id;
228
229
            $menu->save();
230
        }
231
    }
232
}
233