ActionUpdate   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B update() 0 40 9
1
<?php
2
3
namespace Apps\Controller\Admin\Feedback;
4
5
use Apps\ActiveRecord\FeedbackAnswer;
6
use Apps\ActiveRecord\FeedbackPost;
7
use Apps\Model\Admin\Feedback\FormUpdate;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\View;
10
use Ffcms\Core\Exception\NotFoundException;
11
use Ffcms\Core\Helper\Type\Any;
12
use Ffcms\Core\Network\Request;
13
use Ffcms\Core\Network\Response;
14
15
/**
16
 * Trait ActionUpdate
17
 * @package Apps\Controller\Admin\Feedback
18
 * @property Request $request
19
 * @property Response $response
20
 * @property View $view
21
 */
22
trait ActionUpdate
23
{
24
    /**
25
     * Edit feedback post or answer
26
     * @param string $type
27
     * @param string $id
28
     * @return string
29
     * @throws NotFoundException
30
     * @throws \Ffcms\Core\Exception\SyntaxException
31
     */
32
    public function update(string $type, string $id): ?string
33
    {
34
        if (!Any::isInt($id) || $id < 1) {
35
            throw new NotFoundException('Bad id format');
36
        }
37
        // get active record based on type (post or answer for post)
38
        $record = null;
39
        $postId = $id;
40
        switch ($type) {
41
            case 'post':
42
                $record = FeedbackPost::find($id);
43
                break;
44
            case 'answer':
45
                $record = FeedbackAnswer::find($id);
46
                if ($record) {
47
                    $postId = (int)$record->post->id;
48
                }
49
                break;
50
        }
51
52
        // try what we got
53
        if (!$record) {
54
            throw new NotFoundException(__('Feedback item is not founded'));
55
        }
56
57
        // initialize model
58
        $model = new FormUpdate($record);
59
        if ($model->send()) {
60
            if ($model->validate()) {
61
                $model->make();
62
                App::$Session->getFlashBag()->add('success', __('Feedback item are successful changed'));
63
                $this->response->redirect('feedback/read/' . $postId);
64
            } else {
65
                App::$Session->getFlashBag()->add('danger', __('Updating is failed'));
66
            }
67
        }
68
69
        // render output view
70
        return $this->view->render('feedback/update', [
71
            'model' => $model
72
        ]);
73
    }
74
}
75