PageController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 10
dl 0
loc 23
rs 9.9
c 1
b 1
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Chuckbe\Chuckcms\Controllers;
4
5
use Chuckbe\Chuckcms\Chuck\PageBlockRepository;
6
use Chuckbe\Chuckcms\Chuck\PageRepository;
7
use Chuckbe\Chuckcms\Models\Page;
8
use Chuckbe\Chuckcms\Models\PageBlock;
9
use Chuckbe\Chuckcms\Models\Redirect;
10
use Chuckbe\Chuckcms\Models\Repeater;
11
use Chuckbe\Chuckcms\Models\Resource;
12
use Chuckbe\Chuckcms\Models\Site;
13
use Chuckbe\Chuckcms\Models\Template;
14
use Chuckbe\Chuckcms\Models\User;
15
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
16
use Illuminate\Foundation\Bus\DispatchesJobs;
17
use Illuminate\Foundation\Validation\ValidatesRequests;
18
use Illuminate\Http\Request;
19
use Illuminate\Routing\Controller as BaseController;
20
use Illuminate\Support\Facades\URL;
21
use Spatie\Permission\Models\Role;
22
23
class PageController extends BaseController
24
{
25
    use AuthorizesRequests;
26
    use DispatchesJobs;
27
    use ValidatesRequests;
28
29
    private $page;
30
    private $pageRepository;
31
    private $pageblock;
32
    private $pageBlockRepository;
33
    private $redirect;
34
    private $resource;
35
    private $repeater;
36
    private $site;
37
    private $template;
38
    private $user;
39
40
    /**
41
     * Create a new controller instance.
42
     *
43
     * @return void
44
     */
45
    public function __construct(
46
        Page $page,
47
        PageRepository $pageRepository,
48
        PageBlock $pageblock,
49
        PageBlockRepository $pageBlockRepository,
50
        Redirect $redirect,
51
        Resource $resource,
52
        Repeater $repeater,
53
        Site $site,
54
        Template $template,
55
        User $user
56
    ) {
57
        $this->page = $page;
58
        $this->pageRepository = $pageRepository;
59
        $this->pageblock = $pageblock;
60
        $this->pageBlockRepository = $pageBlockRepository;
61
        $this->redirect = $redirect;
62
        $this->resource = $resource;
63
        $this->repeater = $repeater;
64
        $this->site = $site;
65
        $this->template = $template;
66
        $this->user = $user;
67
        $this->middleware('auth');
68
    }
69
70
    /**
71
     * Show the dashboard -> pages.
72
     *
73
     * @return \Illuminate\View\View
74
     */
75
    public function index()
76
    {
77
        $pages = $this->page->ordered()->get();
78
79
        return view('chuckcms::backend.pages.index', compact('pages'));
80
    }
81
82
    /**
83
     * Show the dashboard -> page create.
84
     *
85
     * @return \Illuminate\View\View
86
     */
87
    public function create()
88
    {
89
        $templates = $this->template->where('active', 1)->get();
90
        $pageViews = $this->template->getPageViews();
91
92
        return view('chuckcms::backend.pages.create', compact('templates', 'pageViews'));
93
    }
94
95
    /**
96
     * Show the dashboard -> page edit.
97
     *
98
     * @return \Illuminate\View\View
99
     */
100
    public function edit($page_id)
101
    {
102
        $templates = $this->template->where('active', 1)->get();
103
        $page = $this->page->getByIdWithBlocks($page_id);
104
        $pageViews = $this->template->getPageViews();
105
        $roles = Role::all();
106
107
        return view('chuckcms::backend.pages.edit', compact('templates', 'page', 'pageViews', 'roles'));
108
    }
109
110
    /**
111
     * Show the dashboard -> page edit.
112
     *
113
     * @return \Illuminate\Http\RedirectResponse
114
     */
115
    public function save(Request $request)
116
    {
117
        $this->validate(request(), [//@todo create custom Request class for page validation
118
            'page_title' => 'max:185',
119
        ]);
120
        if ($request['create']) {
121
            $this->pageRepository->create($request);
122
        }
123
        if ($request['update']) {
124
            $this->pageRepository->updatePage($request);
125
        }
126
127
        return redirect()->route('dashboard.pages');
128
    }
129
130
    /**
131
     * Delete the page and pageblocks.
132
     *
133
     * @return string $status
134
     */
135
    public function delete(Request $request)
136
    {
137
        $this->validate(request(), [//@todo create custom Request class for page validation
138
            'page_id' => 'required',
139
        ]);
140
141
        $status = $this->page->deleteById($request->get('page_id'));
142
143
        return $status;
144
    }
145
146
    /**
147
     * Move up.
148
     *
149
     * @return \Illuminate\Http\RedirectResponse
150
     */
151
    public function moveUp($page_id)
152
    {
153
        $page = $this->page->getById($page_id);
154
        $page->moveOrderUp();
155
        $page->save();
156
157
        return redirect()->route('dashboard.pages');
158
    }
159
160
    /**
161
     * Move first.
162
     *
163
     * @return \Illuminate\Http\RedirectResponse
164
     */
165
    public function moveFirst($page_id)
166
    {
167
        $page = $this->page->getById($page_id);
168
        $page->moveToStart();
169
        $page->save();
170
171
        return redirect()->route('dashboard.pages');
172
    }
173
174
    /**
175
     * Move down.
176
     *
177
     * @return \Illuminate\Http\RedirectResponse
178
     */
179
    public function moveDown($page_id)
180
    {
181
        $page = $this->page->getById($page_id);
182
        $page->moveOrderDown();
183
        $page->save();
184
185
        return redirect()->route('dashboard.pages');
186
    }
187
188
    /**
189
     * Move last.
190
     *
191
     * @return \Illuminate\Http\RedirectResponse
192
     */
193
    public function moveLast($page_id)
194
    {
195
        $page = $this->page->getById($page_id);
196
        $page->moveToEnd();
197
        $page->save();
198
199
        return redirect()->route('dashboard.pages');
200
    }
201
202
    /**
203
     * Show the dashboard -> page edit page builder.
204
     *
205
     * @return \Illuminate\View\View
206
     */
207
    public function builderIndex(Request $request, $page_id)
208
    {
209
        if ($request->has('lang')) {
210
            app()->setLocale($request->get('lang'));
0 ignored issues
show
introduced by
The method setLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

210
            app()->/** @scrutinizer ignore-call */ setLocale($request->get('lang'));
Loading history...
211
        } else {
212
            return redirect()->to(URL::current().'?lang='.app()->getLocale());
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

212
            return redirect()->to(URL::current().'?lang='.app()->/** @scrutinizer ignore-call */ getLocale());
Loading history...
Bug Best Practice introduced by
The expression return redirect()->to(Il...' . app()->getLocale()) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
213
        }
214
        $page = $this->page->getByIdWithBlocks($page_id);
215
        $template = $this->template->where('id', $page->template_id)->first();
216
        $pageblocks = $this->pageBlockRepository->getRenderedByPageBlocks($this->pageblock->getAllByPageId($page->id));
217
218
        $block_dir = array_slice(scandir('chuckbe/'.$template->slug.'/blocks'), 2);
0 ignored issues
show
Unused Code introduced by
The assignment to $block_dir is dead and can be removed.
Loading history...
219
        $blocks = $this->dirToArray($template->path.'/blocks');
220
221
        return view('chuckcms::backend.pages.pagebuilder.index', compact('template', 'page', 'pageblocks', 'blocks'));
222
    }
223
224
    public function dirToArray($dir)
225
    {
226
        $result = [];
227
228
        $cdir = scandir($dir);
229
        foreach ($cdir as $key => $value) {
230
            if (!in_array($value, ['.', '..'])) {
231
                if (is_dir($dir.DIRECTORY_SEPARATOR.$value)) {
232
                    $result[$value] = $this->dirToArray($dir.DIRECTORY_SEPARATOR.$value);
233
                } else {
234
                    if ($value !== '.DS_Store' && (strpos($value, '.html') !== false)) {
235
                        $blockKey = str_replace('.html', '', $value);
236
                        $blockName = str_replace('-', ' ', $blockKey);
237
                        if (file_exists($dir.DIRECTORY_SEPARATOR.$blockKey.'.jpg')) {
238
                            $blockImage = $dir.DIRECTORY_SEPARATOR.$blockKey.'.jpg';
239
                        } elseif (file_exists($dir.DIRECTORY_SEPARATOR.$blockKey.'.jpeg')) {
240
                            $blockImage = $dir.DIRECTORY_SEPARATOR.$blockKey.'.jpeg';
241
                        } elseif (file_exists($dir.DIRECTORY_SEPARATOR.$blockKey.'.png')) {
242
                            $blockImage = $dir.DIRECTORY_SEPARATOR.$blockKey.'.png';
243
                        } else {
244
                            $blockImage = 'https://ui-avatars.com/api/?length=5&size=150&name=BLOCK&background=0D8ABC&color=fff&font-size=0.2';
245
                        }
246
                        $result[$blockKey] = [
247
                            'name'     => $blockName,
248
                            'location' => $dir.DIRECTORY_SEPARATOR.$value,
249
                            'img'      => $blockImage,
250
                        ];
251
                    }
252
                }
253
            }
254
        }
255
256
        return $result;
257
    }
258
259
    /**
260
     * Return the raw page - ready for the builder.
261
     *
262
     * @return \Illuminate\View\View
263
     */
264
    public function builderRaw(Request $request, $page_id)
265
    {
266
        if ($request->has('lang')) {
267
            app()->setLocale($request->get('lang'));
268
        } else {
269
            return redirect()->to(URL::current().'?lang='.app()->getLocale());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->to(Il...' . app()->getLocale()) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
270
        }
271
        $page = $this->page->getByIdWithBlocks($page_id);
272
        $template = $this->template->where('id', $page->template_id)->first();
273
        $pageblocks = $this->pageBlockRepository->getRenderedByPageBlocks($this->pageblock->getAllByPageId($page->id));
274
275
        return view('chuckcms::backend.pages.pagebuilder.core', compact('template', 'page', 'pageblocks'));
276
    }
277
}
278