Passed
Push — master ( afc50f...67ec34 )
by Mihail
04:58
created

Content::actionPublish()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 3
Ratio 10.71 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 15
c 1
b 1
f 0
nc 4
nop 0
dl 3
loc 28
rs 8.439
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\FormContentPublish;
12
use Apps\Model\Admin\Content\FormContentRestore;
13
use Apps\Model\Admin\Content\FormContentUpdate;
14
use Apps\Model\Admin\Content\FormSettings;
15
use Extend\Core\Arch\AdminController;
16
use Ffcms\Core\App;
17
use Apps\ActiveRecord\Content as ContentEntity;
18
use Ffcms\Core\Exception\ForbiddenException;
19
use Ffcms\Core\Exception\NativeException;
20
use Ffcms\Core\Exception\NotFoundException;
21
use Ffcms\Core\Exception\SyntaxException;
22
use Ffcms\Core\Helper\FileSystem\Directory;
23
use Ffcms\Core\Helper\HTML\SimplePagination;
24
use Ffcms\Core\Helper\Type\Arr;
25
use Ffcms\Core\Helper\Type\Obj;
26
27
class Content extends AdminController
28
{
29
    const VERSION = 0.1;
30
    const ITEM_PER_PAGE = 10;
31
32
    public $type = 'app';
33
34
    /**
35
     * List content items
36
     * @return string
37
     * @throws \Ffcms\Core\Exception\SyntaxException
38
     * @throws \Ffcms\Core\Exception\NativeException
39
     */
40
    public function actionIndex()
41
    {
42
        // set current page and offset
43
        $page = (int)App::$Request->query->get('page');
44
        $offset = $page * self::ITEM_PER_PAGE;
45
46
        $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...
47
        // get query type (trash, category, all)
48
        $type = App::$Request->query->get('type');
49
        if ($type === 'trash') {
50
            $query = ContentEntity::onlyTrashed();
51
        } elseif ($type === 'moderate') { // only items on moderate
52
            $query = ContentEntity::where('display', '=', 0);
53
        } elseif (Obj::isLikeInt($type)) { // sounds like category id ;)
54
            $query = ContentEntity::where('category_id', '=', (int)$type);
55
        } else {
56
            $query = new ContentEntity();
57
            $type = 'all';
58
        }
59
60
        // build pagination
61
        $pagination = new SimplePagination([
62
            'url' => ['content/index', null, null, ['type' => $type]],
63
            'page' => $page,
64
            'step' => self::ITEM_PER_PAGE,
65
            'total' => $query->count()
0 ignored issues
show
Bug introduced by
The method count does only exist in Ffcms\Core\Arch\ActiveModel, but not in Illuminate\Database\Eloq...se\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...
66
        ]);
67
68
        // build listing objects
69
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
0 ignored issues
show
Bug introduced by
The method orderBy does only exist in Ffcms\Core\Arch\ActiveModel, but not in Illuminate\Database\Eloq...se\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...
70
71
72
        return App::$View->render('index', [
73
            'records' => $records,
74
            'pagination' => $pagination,
75
            'type' => $type
76
        ]);
77
    }
78
79
    /**
80
     * Edit and add content items
81
     * @param $id
82
     * @return string
83
     * @throws \Ffcms\Core\Exception\SyntaxException
84
     * @throws \Ffcms\Core\Exception\NativeException
85
     */
86 View Code Duplication
    public function actionUpdate($id = null)
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...
87
    {
88
        // get item with trashed objects
89
        $record = ContentEntity::withTrashed()->findOrNew($id);
0 ignored issues
show
Bug introduced by
The method findOrNew 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...
90
        $isNew = $record->id === null;
91
92
        // create empty object if its new
93
        if ($isNew === true) {
94
            $record = new ContentEntity();
95
        }
96
97
        // init model
98
        $model = new FormContentUpdate($record);
99
100
        // check if model is submit
101
        if ($model->send() && $model->validate()) {
102
            $model->save();
103
            if ($isNew === true) {
104
                App::$Response->redirect('content/index');
105
            }
106
            App::$Session->getFlashBag()->add('success', __('Content is successful updated'));
107
        }
108
109
        // draw response
110
        return App::$View->render('content_update', [
111
            'model' => $model
112
        ]);
113
    }
114
115
    /**
116
     * Delete content by id
117
     * @param int $id
118
     * @return string
119
     * @throws NotFoundException
120
     * @throws \Ffcms\Core\Exception\SyntaxException
121
     * @throws \Ffcms\Core\Exception\NativeException
122
     */
123 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...
124
    {
125
        if (!Obj::isLikeInt($id) || $id < 1) {
126
            throw new NotFoundException();
127
        }
128
129
        // get content record and check availability
130
        $record = ContentEntity::find($id);
131
        if ($record === null || $record === false) {
132
            throw new NotFoundException();
133
        }
134
135
        // init delete model
136
        $model = new FormContentDelete($record);
0 ignored issues
show
Compatibility introduced by
$record of type object<Ffcms\Core\Arch\ActiveModel> is not a sub-type of object<Apps\ActiveRecord\Content>. It seems like you assume a child class of the class Ffcms\Core\Arch\ActiveModel to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
137
        if ($model->send() && $model->validate()) {
138
            $model->make();
139
            App::$Session->getFlashBag()->add('success', __('Content is successful moved to trash'));
140
            App::$Response->redirect('content/index');
141
        }
142
143
        return App::$View->render('content_delete', [
144
            'model' => $model->export()
145
        ]);
146
    }
147
148
    /**
149
     * Restore deleted content
150
     * @param $id
151
     * @return string
152
     * @throws NotFoundException
153
     * @throws SyntaxException
154
     * @throws \Ffcms\Core\Exception\NativeException
155
     */
156 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...
157
    {
158
        if (!Obj::isLikeInt($id) || $id < 1) {
159
            throw new NotFoundException();
160
        }
161
162
        // get removed object
163
        $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...
164
        if ($record === null || $record === false) {
165
            throw new NotFoundException();
166
        }
167
168
        // init model
169
        $model = new FormContentRestore($record);
170
        // check if action is send
171
        if ($model->send() && $model->validate()) {
172
            $model->make();
173
            App::$Session->getFlashBag()->add('success', __('Content are successful recovered'));
174
            App::$Response->redirect('content/index');
175
        }
176
177
        // draw response
178
        return App::$View->render('content_restore', [
179
            'model' => $model->export()
180
        ]);
181
    }
182
183
    /**
184
     * Clear the trashed items
185
     * @return string
186
     * @throws SyntaxException
187
     * @throws \Ffcms\Core\Exception\SyntaxException
188
     * @throws \Ffcms\Core\Exception\NativeException
189
     */
190
    public function actionClear()
191
    {
192
        // find trashed rows
193
        $records = ContentEntity::onlyTrashed();
194
195
        // init model
196
        $model = new FormContentClear($records->count());
197
        if ($model->send() && $model->validate()) {
198
            // remove all trashed items
199
            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...
200
                $galleryPath = '/upload/gallery/' . (int)$item->id;
201
                if (Directory::exist($galleryPath)) {
202
                    Directory::remove($galleryPath);
203
                }
204
            }
205
            // totally remove rows from db
206
            $records->forceDelete();
207
            App::$Session->getFlashBag()->add('success', __('Trashed content is cleanup'));
208
            App::$Response->redirect('content/index');
209
        }
210
211
        // draw response
212
        return App::$View->render('content_clear', [
213
            'model' => $model->export()
214
        ]);
215
    }
216
217
    /**
218
     * Display category list
219
     */
220
    public function actionCategories()
221
    {
222
        return App::$View->render('category_list');
223
    }
224
225
    /**
226
     * Delete category action
227
     * @param int $id
228
     * @return string
229
     * @throws ForbiddenException
230
     * @throws \Ffcms\Core\Exception\SyntaxException
231
     * @throws \Ffcms\Core\Exception\NativeException
232
     */
233 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...
234
    {
235
        // check id
236
        if (!Obj::isLikeInt($id) || $id < 2) {
237
            throw new ForbiddenException();
238
        }
239
240
        // get object relation
241
        $record = ContentCategory::find($id);
242
        if ($record === null || $record === false) {
243
            throw new ForbiddenException();
244
        }
245
246
        // init model with object relation
247
        $model = new FormCategoryDelete($record);
0 ignored issues
show
Compatibility introduced by
$record of type object<Ffcms\Core\Arch\ActiveModel> is not a sub-type of object<Apps\ActiveRecord\ContentCategory>. It seems like you assume a child class of the class Ffcms\Core\Arch\ActiveModel to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
248
249
        // check if delete is submited
250
        if ($model->send() && $model->validate()) {
251
            $model->make();
252
            App::$Session->getFlashBag()->add('success', __('Category is successful removed'));
253
            App::$Response->redirect('content/categories');
254
        }
255
256
        // draw view
257
        return App::$View->render('category_delete', [
258
            'model' => $model->export()
259
        ]);
260
    }
261
262
    /**
263
     * Show category edit and create
264
     * @param int $id
265
     * @return string
266
     * @throws \Ffcms\Core\Exception\SyntaxException
267
     * @throws \Ffcms\Core\Exception\NativeException
268
     */
269
    public function actionCategoryupdate($id = null)
270
    {
271
        // get owner id for new rows
272
        $parentId = (int)App::$Request->query->get('parent');
273
274
        // get relation and pass to model
275
        $record = ContentCategory::findOrNew($id);
276
        $isNew = $record->id === null;
277
        $model = new FormCategoryUpdate($record, $parentId);
278
279
        // if model is submited
280
        if ($model->send() && $model->validate()) {
281
            $model->save();
282
            // if is new - redirect to list after submit
283
            if ($isNew) {
284
                App::$Response->redirect('content/categories');
285
            }
286
            // show notify message
287
            App::$Session->getFlashBag()->add('success', __('Category is successful updated'));
288
        }
289
290
        // draw response view and pass model properties
291
        return App::$View->render('category_update', [
292
            'model' => $model->export()
293
        ]);
294
    }
295
296
    /**
297
     * Show content global delete
298
     * @return string
299
     * @throws NotFoundException
300
     * @throws SyntaxException
301
     * @throws NativeException
302
     */
303
    public function actionGlobdelete()
304
    {
305
        // get content ids from request
306
        $ids = App::$Request->query->get('selected');
307
308
        // check if input is array
309 View Code Duplication
        if (!Obj::isArray($ids) || count($ids) < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
310
            throw new NotFoundException(__('Nothing to delete is founded'));
311
        }
312
313
        // get all records as object from db
314
        $records = ContentEntity::find($ids);
315
316
        if ($records->count() < 1) {
317
            throw new NotFoundException(__('Nothing to delete is founded'));
318
        }
319
320
        // init model and pass objects
321
        $model = new FormContentGlobDelete($records);
322
323
        // check if delete is submited
324
        if ($model->send() && $model->validate()) {
325
            $model->make();
326
            App::$Session->getFlashBag()->add('success', __('Content are successful removed'));
327
            App::$Response->redirect('content/index');
328
        }
329
330
        // return response
331
        return App::$View->render('content_glob_delete', [
332
            'model' => $model
333
        ]);
334
    }
335
336
    /**
337
     * Publish content on moderate stage
338
     * @return string
339
     * @throws NotFoundException
340
     * @throws SyntaxException
341
     * @throws NativeException
342
     */
343
    public function actionPublish()
344
    {
345
        // get ids as array from GET
346
        $ids = App::$Request->query->get('selected');
347 View Code Duplication
        if (!Obj::isArray($ids) || count($ids) < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
348
            throw new NotFoundException(__('Items to publish is not found'));
349
        }
350
351
        // try to find items in db
352
        $records = ContentEntity::whereIn('id', $ids)->where('display', '=', 0);
353
        if ($records->count() < 1) {
354
            throw new NotFoundException(__('Items to publish is not found'));
355
        }
356
357
        // initialize model and operate submit
358
        $model = new FormContentPublish($records);
359
        if ($model->send() && $model->validate()) {
360
            $model->make();
361
            App::$Session->getFlashBag()->add('success', __('Content is successful published'));
362
            App::$Response->redirect('content/index');
363
        }
364
365
        // draw view output
366
        return App::$View->render('publish', [
367
            'records' => $records->get(),
368
            'model' => $model
369
        ]);
370
    }
371
372
    /**
373
     * Show settings form with prepared model
374
     * @return string
375
     * @throws SyntaxException
376
     * @throws NativeException
377
     */
378 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...
379
    {
380
        // init model with config array data
381
        $model = new FormSettings($this->getConfigs());
382
383
        // check if form is send
384
        if ($model->send()) {
385
            if ($model->validate()) {
386
                $this->setConfigs($model->getAllProperties());
387
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
388
                App::$Response->redirect('content/index');
389
            } else {
390
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
391
            }
392
        }
393
394
        // draw response
395
        return App::$View->render('settings', [
396
            'model' => $model->export()
397
        ]);
398
    }
399
}