Passed
Push — master ( 81f334...c86e8c )
by Mihail
03:35
created

ActionCategoryDelete::categoryDelete()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 4
nop 1
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Admin\Content;
4
5
use Apps\ActiveRecord\ContentCategory;
6
use Apps\Model\Admin\Content\FormCategoryDelete;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Helper\Type\Any;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
14
/**
15
 * Trait ActionCategoryDelete
16
 * @package Apps\Controller\Admin\Content
17
 * @property Request $request
18
 * @property Response $response
19
 * @property View $view
20
 */
21
trait ActionCategoryDelete
22
{
23
    /**
24
     * Delete category action
25
     * @param string $id
26
     * @return string
27
     * @throws \Exception
28
     */
29
    public function categoryDelete(string $id): ?string
30
    {
31
        // check id
32
        if (!Any::isInt($id) || $id < 2) {
33
            throw new ForbiddenException();
34
        }
35
36
        // get object relation
37
        $record = ContentCategory::find($id);
38
        if (!$record) {
39
            throw new ForbiddenException();
40
        }
41
42
        // init model with object relation
43
        $model = new FormCategoryDelete($record);
44
45
        // check if delete is submited
46
        if ($model->send() && $model->validate()) {
47
            $model->make();
48
            App::$Session->getFlashBag()->add('success', __('Category is successful removed'));
49
            $this->response->redirect('content/categories');
50
        }
51
52
        // draw view
53
        return $this->view->render('category_delete', [
54
            'model' => $model
55
        ]);
56
    }
57
}
58