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

ActionDelete::delete()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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