ActionRestore   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 13
dl 0
loc 33
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A restore() 0 24 6
1
<?php
2
3
namespace Apps\Controller\Admin\Content;
4
5
use Apps\ActiveRecord\Content as ContentEntity;
6
use Apps\Model\Admin\Content\FormContentRestore;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\NotFoundException;
10
use Ffcms\Core\Helper\Type\Any;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
14
/**
15
 * Trait ActionRestore
16
 * @package Apps\Controller\Admin\Content
17
 * @property Request $request
18
 * @property Response $response
19
 * @property View $view
20
 */
21
trait ActionRestore
22
{
23
    /**
24
     * Restore deleted content
25
     * @param $id
26
     * @return string
27
     * @throws NotFoundException
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     */
30
    public function restore(string $id): ?string
31
    {
32
        if (!Any::isInt($id) || $id < 1) {
33
            throw new NotFoundException();
34
        }
35
36
        // get removed object
37
        $record = ContentEntity::onlyTrashed()->find($id);
38
        if (!$record) {
39
            throw new NotFoundException();
40
        }
41
42
        // init model
43
        $model = new FormContentRestore($record);
44
        // check if action is send
45
        if ($model->send() && $model->validate()) {
46
            $model->make();
47
            App::$Session->getFlashBag()->add('success', __('Content are successful recovered'));
48
            $this->response->redirect('content/index');
49
        }
50
51
        // draw response
52
        return $this->view->render('content/content_restore', [
53
            'model' => $model
54
        ]);
55
    }
56
}
57