ActionRead::read()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 6
nop 1
dl 0
loc 30
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Admin\Feedback;
4
5
use Apps\ActiveRecord\FeedbackPost;
6
use Apps\Model\Admin\Feedback\FormAnswerAdd;
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 ActionRead
16
 * @package Apps\Controller\Admin\Feedback
17
 * @property Request $request
18
 * @property Response $response
19
 * @property View $view
20
 */
21
trait ActionRead
22
{
23
    /**
24
     * Read feedback post and answer and add answer to thread post
25
     * @param string $id
26
     * @return string|null
27
     * @throws NotFoundException
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     */
30
    public function read(string $id): ?string
31
    {
32
        if (!Any::isInt($id) || $id < 1) {
33
            throw new NotFoundException('Bad id format');
34
        }
35
        // find feedback post by id
36
        $record = FeedbackPost::with(['user', 'user.profile'])
37
            ->find($id);
38
        if (!$record) {
39
            throw new NotFoundException(__('The feedback message is not founded'));
40
        }
41
42
        // initialize model with answer add if thread is not closed
43
        $model = null;
44
        if (!(bool)$record->closed) {
45
            $model = new FormAnswerAdd($record);
46
            if ($model->send()) {
47
                if ($model->validate()) {
48
                    $model->make();
49
                    App::$Session->getFlashBag()->add('success', __('New answer is successful add'));
50
                } else {
51
                    App::$Session->getFlashBag()->add('error', 'Validation failure');
52
                }
53
            }
54
        }
55
56
        // render view
57
        return $this->view->render('feedback/read', [
58
            'record' => $record,
59
            'model' => $model
60
        ]);
61
    }
62
}
63