Passed
Push — master ( 099e30...c4c7c1 )
by Karel
13:30
created

PageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 21
c 0
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 9

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
7
use Chuckbe\Chuckcms\Models\Page;
8
use Chuckbe\Chuckcms\Models\PageBlock;
9
use Chuckbe\Chuckcms\Models\Resource;
10
use Chuckbe\Chuckcms\Models\Redirect;
11
use Chuckbe\Chuckcms\Models\Repeater;
12
use Chuckbe\Chuckcms\Models\Site;
13
use Chuckbe\Chuckcms\Models\Template;
14
use Chuckbe\Chuckcms\Models\User;
15
16
use Illuminate\Http\Request;
17
use Illuminate\Foundation\Bus\DispatchesJobs;
18
use Illuminate\Routing\Controller as BaseController;
19
use Illuminate\Foundation\Validation\ValidatesRequests;
20
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
21
22
class PageController extends BaseController
23
{
24
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
25
26
    private $page;
27
    private $pageblock;
28
    private $pageBlockRepository;
29
    private $redirect;
30
    private $resource;
31
    private $repeater;
32
    private $site;
33
    private $template;
34
    private $user;
35
    /**
36
     * Create a new controller instance.
37
     *
38
     * @return void
39
     */
40
    public function __construct(
41
        Page $page, 
42
        PageBlock $pageblock, 
43
        PageBlockRepository $pageBlockRepository, 
44
        Redirect $redirect, 
45
        Resource $resource,
46
        Repeater $repeater, 
47
        Site $site, 
48
        Template $template,
49
        User $user)
50
    {
51
        $this->page = $page;
52
        $this->pageblock = $pageblock;
53
        $this->pageBlockRepository = $pageBlockRepository;
54
        $this->redirect = $redirect;
55
        $this->resource = $resource;
56
        $this->repeater = $repeater;
57
        $this->site = $site;
58
        $this->template = $template;
59
        $this->user = $user;
60
        $this->middleware('auth');
61
    }
62
63
    /**
64
     * Show the dashboard -> pages.
65
     *
66
     * @return \Illuminate\View\View
67
     */
68
    public function index()
69
    {
70
        $pages = $this->page->ordered()->get();
71
        
72
        return view('chuckcms::backend.pages.index', compact('pages'));
73
    }
74
75
    /**
76
     * Show the dashboard -> page edit.
77
     *
78
     * @return \Illuminate\View\View
79
     */
80
    public function edit($page_id)
81
    {
82
        $templates = $this->template->where('active', 1)->where('type', 'default')->get();
83
        $page = $this->page->getByIdWithBlocks($page_id);
84
        $pageViews = $this->template->getPageViews();
85
        return view('chuckcms::backend.pages.edit', compact('templates', 'page', 'pageViews'));
86
    }
87
88
    /**
89
     * Show the dashboard -> page create.
90
     *
91
     * @return \Illuminate\View\View
92
     */
93
    public function create()
94
    {
95
        $template = $this->template->where('active', 1)->where('type', 'admin')->first();
96
        $templates = $this->template->where('active', 1)->where('type', 'default')->get();
97
        $pageViews = $this->template->getPageViews();
98
        return view('chuckcms::backend.pages.create', compact('template', 'templates', 'page', 'pageViews'));
99
    }
100
101
    /**
102
     * Show the dashboard -> page edit.
103
     *
104
     * @return \Illuminate\Http\RedirectResponse
105
     */
106
    public function save(Request $request)
107
    {
108
        $this->validate(request(), [//@todo create custom Request class for page validation
109
            'page_title' => 'max:185',
110
        ]);
111
        if($request['create']){
112
            $this->page->create($request);
113
        } if($request['update']){
114
            $this->page->updatePage($request);
115
        }
116
        return redirect()->route('dashboard.pages');
117
    }
118
119
    /**
120
     * Delete the page and pageblocks.
121
     *
122
     * @return string $status
123
     */
124
    public function delete(Request $request)
125
    {
126
        $this->validate(request(), [//@todo create custom Request class for page validation
127
            'page_id' => 'required',
128
        ]);
129
        
130
        $status = $this->page->deleteById($request->get('page_id'));
131
        return $status;
132
    }
133
134
    /**
135
     * Move up.
136
     *
137
     * @return \Illuminate\Http\RedirectResponse
138
     */
139
    public function moveUp($page_id)
140
    {
141
        $page = $this->page->getById($page_id);
142
        $page->moveOrderUp();
143
        $page->save();
144
        return redirect()->route('dashboard.pages');
145
    }
146
147
    /**
148
     * Move first.
149
     *
150
     * @return \Illuminate\Http\RedirectResponse
151
     */
152
    public function moveFirst($page_id)
153
    {
154
        $page = $this->page->getById($page_id);
155
        $page->moveToStart();
156
        $page->save();
157
        return redirect()->route('dashboard.pages');
158
    }
159
160
    /**
161
     * Move down.
162
     *
163
     * @return \Illuminate\Http\RedirectResponse
164
     */
165
    public function moveDown($page_id)
166
    {
167
        $page = $this->page->getById($page_id);
168
        $page->moveOrderDown();
169
        $page->save();
170
        return redirect()->route('dashboard.pages');
171
    }
172
173
    /**
174
     * Move last.
175
     *
176
     * @return \Illuminate\Http\RedirectResponse
177
     */
178
    public function moveLast($page_id)
179
    {
180
        $page = $this->page->getById($page_id);
181
        $page->moveToEnd();
182
        $page->save();
183
        return redirect()->route('dashboard.pages');
184
    }
185
186
    /**
187
     * Show the dashboard -> page edit page builder.
188
     *
189
     * @return \Illuminate\View\View
190
     */
191
    public function builderIndex($page_id)
192
    {
193
        $page = $this->page->getByIdWithBlocks($page_id);
194
        $template = $this->template->where('id', $page->template_id)->where('type', 'default')->first();
195
        $pageblocks = $this->pageBlockRepository->getRenderedByPageBlocks($this->pageblock->getAllByPageId($page->id));
196
197
        $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...
Bug introduced by
It seems like scandir('chuckbe/' . $template->slug . '/blocks') can also be of type false; however, parameter $array of array_slice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

197
        $block_dir = array_slice(/** @scrutinizer ignore-type */ scandir('chuckbe/' . $template->slug . '/blocks'), 2);        
Loading history...
198
        $blocks = $this->dirToArray($template->path . '/blocks');
199
        
200
        return view('chuckcms::backend.pages.pagebuilder.index', compact('template', 'page', 'pageblocks', 'blocks'));
201
    }
202
203
    public function dirToArray($dir) { 
204
   
205
        $result = array(); 
206
207
        $cdir = scandir($dir); 
208
        foreach ($cdir as $key => $value) 
209
        { 
210
            if (!in_array($value, array(".", ".."))) { 
211
                if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) { 
212
                    $result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
213
                } 
214
                else { 
215
                    if ($value !== '.DS_Store' && (strpos($value, '.html') !== false)) {
216
                        $blockKey = str_replace('.html', '', $value);
217
                        $blockName = str_replace('-', ' ', $blockKey);
218
                        if (file_exists($dir . DIRECTORY_SEPARATOR . $blockKey . '.jpg')) {
219
                            $blockImage = $dir . DIRECTORY_SEPARATOR . $blockKey . '.jpg';
220
                        } elseif (file_exists($dir . DIRECTORY_SEPARATOR . $blockKey . '.jpeg')) {
221
                            $blockImage = $dir . DIRECTORY_SEPARATOR . $blockKey . '.jpeg';
222
                        } elseif (file_exists($dir . DIRECTORY_SEPARATOR . $blockKey . '.png')) {
223
                            $blockImage = $dir . DIRECTORY_SEPARATOR . $blockKey . '.png';
224
                        } else {
225
                            $blockImage = 'https://ui-avatars.com/api/?length=5&size=150&name=BLOCK&background=0D8ABC&color=fff&font-size=0.2';
226
                        }
227
                        $result[$blockKey] = array(
228
                            'name' => $blockName,
229
                            'location' => $dir . DIRECTORY_SEPARATOR . $value,
230
                            'img' => $blockImage
231
                        );
232
                    }
233
                } 
234
            } 
235
        } 
236
237
        return $result; 
238
    }
239
240
    /**
241
     * Return the raw page - ready for the builder
242
     *
243
     * @return \Illuminate\View\View
244
     */
245
    public function builderRaw($page_id)
246
    {
247
        $page = $this->page->getByIdWithBlocks($page_id);
248
        $template = $this->template->where('id', $page->template_id)->where('type', 'default')->first();
249
        $pageblocks = $this->pageBlockRepository->getRenderedByPageBlocks($this->pageblock->getAllByPageId($page->id));
250
251
        return view('chuckcms::backend.pages.pagebuilder.core', compact('template', 'page', 'pageblocks'));
252
    }
253
}
254