Passed
Push — master ( 336a24...a4f66d )
by Mihail
14:10
created

Content::actionCategoryupdate()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 26
rs 8.5806
cc 4
eloc 12
nc 3
nop 1
1
<?php
2
3
namespace Apps\Controller\Admin;
4
5
use Apps\ActiveRecord\ContentCategory;
6
use Apps\Model\Admin\Content\FormCategoryDelete;
7
use Apps\Model\Admin\Content\FormCategoryUpdate;
8
use Apps\Model\Admin\Content\FormContentClear;
9
use Apps\Model\Admin\Content\FormContentDelete;
10
use Apps\Model\Admin\Content\FormContentGlobDelete;
11
use Apps\Model\Admin\Content\FormContentRestore;
12
use Apps\Model\Admin\Content\FormContentUpdate;
13
use Apps\Model\Admin\Content\FormSettings;
14
use Extend\Core\Arch\AdminAppController;
15
use Ffcms\Core\App;
16
use Apps\ActiveRecord\Content as ContentEntity;
17
use Ffcms\Core\Exception\ForbiddenException;
18
use Ffcms\Core\Exception\NotFoundException;
19
use Ffcms\Core\Exception\SyntaxException;
20
use Ffcms\Core\Helper\FileSystem\Directory;
21
use Ffcms\Core\Helper\HTML\SimplePagination;
22
use Ffcms\Core\Helper\Type\Arr;
23
use Ffcms\Core\Helper\Type\Obj;
24
25
class Content extends AdminAppController
26
{
27
    const VERSION = 0.1;
28
29
    const ITEM_PER_PAGE = 10;
30
31
    /**
32
     * List content items
33
     * @return string
34
     * @throws \Ffcms\Core\Exception\SyntaxException
35
     * @throws \Ffcms\Core\Exception\NativeException
36
     */
37
    public function actionIndex()
38
    {
39
        // set current page and offset
40
        $page = (int)App::$Request->query->get('page');
41
        $offset = $page * self::ITEM_PER_PAGE;
42
43
        $query = null;
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
        // get query type (trash, category, all)
45
        $type = App::$Request->query->get('type');
46
        if ($type === 'trash') {
47
            $query = ContentEntity::onlyTrashed();
48
        } elseif (Obj::isLikeInt($type)) { // sounds like category id ;)
49
            $query = ContentEntity::where('category_id', '=', (int)$type);
50
        } else {
51
            $query = new ContentEntity();
52
            $type = 'all';
53
        }
54
55
        // build pagination
56
        $pagination = new SimplePagination([
57
            'url' => ['content/index', null, null, ['type' => $type]],
58
            'page' => $page,
59
            'step' => self::ITEM_PER_PAGE,
60
            'total' => $query->count()
61
        ]);
62
63
        // build listing objects
64
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
65
66
67
        return App::$View->render('index', [
68
            'records' => $records,
69
            'pagination' => $pagination,
70
            'type' => $type
71
        ]);
72
    }
73
74
    /**
75
     * Edit and add content items
76
     * @param $id
77
     * @return string
78
     * @throws \Ffcms\Core\Exception\SyntaxException
79
     * @throws \Ffcms\Core\Exception\NativeException
80
     */
81 View Code Duplication
    public function actionUpdate($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...
82
    {
83
        // get item with trashed objects
84
        $record = ContentEntity::withTrashed()->find($id);
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\SoftDeletes.

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...
85
        $isNew = $record->id === null;
86
87
        // create empty object if its new
88
        if ($isNew === true) {
89
            $record = new ContentEntity();
90
        }
91
92
        // init model
93
        $model = new FormContentUpdate($record);
94
95
        // check if model is submit
96
        if ($model->send() && $model->validate()) {
97
            $model->save();
98
            if ($isNew === true) {
99
                App::$Response->redirect('content/index');
100
            }
101
            App::$Session->getFlashBag()->add('success', __('Content is successful updated'));
102
        }
103
104
        // draw response
105
        return App::$View->render('content_update', [
106
            'model' => $model
107
        ]);
108
    }
109
110
    /**
111
     * Delete content by id
112
     * @param int $id
113
     * @return string
114
     * @throws NotFoundException
115
     * @throws \Ffcms\Core\Exception\SyntaxException
116
     * @throws \Ffcms\Core\Exception\NativeException
117
     */
118 View Code Duplication
    public function actionDelete($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...
119
    {
120
        if (!Obj::isLikeInt($id) || $id < 1) {
121
            throw new NotFoundException();
122
        }
123
124
        // get content record and check availability
125
        $record = ContentEntity::find($id);
126
        if ($record === null || $record === false) {
127
            throw new NotFoundException();
128
        }
129
130
        // init delete model
131
        $model = new FormContentDelete($record);
132
        if ($model->send() && $model->validate()) {
133
            $model->make();
134
            App::$Session->getFlashBag()->add('success', __('Content is successful moved to trash'));
135
            App::$Response->redirect('content/index');
136
        }
137
138
        return App::$View->render('content_delete', [
139
            'model' => $model->export()
140
        ]);
141
    }
142
143
    /**
144
     * Restore deleted content
145
     * @param $id
146
     * @return string
147
     * @throws NotFoundException
148
     * @throws SyntaxException
149
     * @throws \Ffcms\Core\Exception\NativeException
150
     */
151 View Code Duplication
    public function actionRestore($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...
152
    {
153
        if (!Obj::isLikeInt($id) || $id < 1) {
154
            throw new NotFoundException();
155
        }
156
157
        // get removed object
158
        $record = ContentEntity::onlyTrashed()->find($id);
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\SoftDeletes.

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...
159
        if ($record === null || $record === false) {
160
            throw new NotFoundException();
161
        }
162
163
        // init model
164
        $model = new FormContentRestore($record);
165
        // check if action is send
166
        if ($model->send() && $model->validate()) {
167
            $model->make();
168
            App::$Session->getFlashBag()->add('success', __('Content are successful recovered'));
169
            App::$Response->redirect('content/index');
170
        }
171
172
        // draw response
173
        return App::$View->render('content_restore', [
174
            'model' => $model->export()
175
        ]);
176
    }
177
178
    /**
179
     * Clear the trashed items
180
     * @return string
181
     * @throws SyntaxException
182
     * @throws \Ffcms\Core\Exception\SyntaxException
183
     * @throws \Ffcms\Core\Exception\NativeException
184
     */
185
    public function actionClear()
186
    {
187
        // find trashed rows
188
        $records = ContentEntity::onlyTrashed();
189
190
        // init model
191
        $model = new FormContentClear($records->count());
192
        if ($model->send() && $model->validate()) {
193
            // remove all trashed items
194
            foreach ($records->get() as $item) {
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\SoftDeletes.

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...
195
                $galleryPath = '/upload/gallery/' . (int)$item->id;
196
                if (Directory::exist($galleryPath)) {
197
                    Directory::remove($galleryPath);
198
                }
199
            }
200
            // totally remove rows from db
201
            $records->forceDelete();
202
            App::$Session->getFlashBag()->add('success', __('Trashed content is cleanup'));
203
            App::$Response->redirect('content/index');
204
        }
205
206
        // draw response
207
        return App::$View->render('content_clear', [
208
            'model' => $model->export()
209
        ]);
210
    }
211
212
    /**
213
     * Display category list
214
     */
215
    public function actionCategories()
216
    {
217
        return App::$View->render('category_list');
218
    }
219
220
    /**
221
     * Delete category action
222
     * @param int $id
223
     * @return string
224
     * @throws ForbiddenException
225
     * @throws \Ffcms\Core\Exception\SyntaxException
226
     * @throws \Ffcms\Core\Exception\NativeException
227
     */
228 View Code Duplication
    public function actionCategorydelete($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...
229
    {
230
        // check id
231
        if (!Obj::isLikeInt($id) || $id < 2) {
232
            throw new ForbiddenException();
233
        }
234
235
        // get object relation
236
        $record = ContentCategory::find($id);
237
        if ($record === null || $record === false) {
238
            throw new ForbiddenException();
239
        }
240
241
        // init model with object relation
242
        $model = new FormCategoryDelete($record);
243
244
        // check if delete is submited
245
        if ($model->send() && $model->validate()) {
246
            $model->make();
247
            App::$Session->getFlashBag()->add('success', __('Category is successful removed'));
248
            App::$Response->redirect('content/categories');
249
        }
250
251
        // draw view
252
        return App::$View->render('category_delete', [
253
            'model' => $model->export()
254
        ]);
255
    }
256
257
    /**
258
     * Show category edit and create
259
     * @param int $id
260
     * @return string
261
     * @throws \Ffcms\Core\Exception\SyntaxException
262
     * @throws \Ffcms\Core\Exception\NativeException
263
     */
264
    public function actionCategoryupdate($id = null)
265
    {
266
        // get owner id for new rows
267
        $parentId = (int)App::$Request->query->get('parent');
268
269
        // get relation and pass to model
270
        $record = ContentCategory::findOrNew($id);
271
        $isNew = $record->id === null;
272
        $model = new FormCategoryUpdate($record, $parentId);
0 ignored issues
show
Documentation introduced by
$record is of type object<Illuminate\Suppor...atabase\Eloquent\Model>, but the function expects a object<Apps\ActiveRecord\ContentCategory>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
273
274
        // if model is submited
275
        if ($model->send() && $model->validate()) {
276
            $model->save();
277
            // if is new - redirect to list after submit
278
            if ($isNew) {
279
                App::$Response->redirect('content/categories');
280
            }
281
            // show notify message
282
            App::$Session->getFlashBag()->add('success', __('Category is successful updated'));
283
        }
284
285
        // draw response view and pass model properties
286
        return App::$View->render('category_update', [
287
            'model' => $model->export()
288
        ]);
289
    }
290
291
    /**
292
     * Show content global delete
293
     * @return string
294
     * @throws NotFoundException
295
     * @throws SyntaxException
296
     */
297
    public function actionGlobdelete()
298
    {
299
        // get content ids from request
300
        $ids = App::$Request->query->get('selectRemove');
301
302
        // check if input is array
303
        if (!Obj::isArray($ids) || count($ids) < 1) {
304
            throw new NotFoundException(__('Nothing to delete is founded'));
305
        }
306
307
        // get all records as object from db
308
        $records = ContentEntity::find($ids);
309
310
        if ($records->count() < 1) {
311
            throw new NotFoundException(__('Nothing to delete is founded'));
312
        }
313
314
        // init model and pass objects
315
        $model = new FormContentGlobDelete($records);
316
317
        // check if delete is submited
318
        if ($model->send() && $model->validate()) {
319
            $model->make();
320
            App::$Session->getFlashBag()->add('success', __('Content are successful removed'));
321
            App::$Response->redirect('content/index');
322
        }
323
324
        // return response
325
        return App::$View->render('content_glob_delete', [
326
            'model' => $model
327
        ]);
328
    }
329
330
    /**
331
     * Show settings form with prepared model
332
     * @return string
333
     * @throws SyntaxException
334
     */
335 View Code Duplication
    public function actionSettings()
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...
336
    {
337
        // init model with config array data
338
        $model = new FormSettings($this->getConfigs());
339
340
        // check if form is send
341
        if ($model->send()) {
342
            if ($model->validate()) {
343
                $this->setConfigs($model->getAllProperties());
344
                App::$Response->redirect('content/index');
345
            } else {
346
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
347
            }
348
        }
349
350
        // draw response
351
        return App::$View->render('settings', [
352
            'model' => $model->export()
353
        ]);
354
    }
355
}