Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Controller/Api/Comments/ActionAdd.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Apps\Controller\Api\Comments;
4
5
use Apps\ActiveRecord\App as AppRecord;
6
use Apps\Model\Api\Comments\CommentAnswerAdd;
7
use Apps\Model\Api\Comments\CommentPostAdd;
8
use Apps\Model\Api\Comments\EntityCommentData;
9
use Ffcms\Core\App;
10
use Ffcms\Core\Exception\NativeException;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
14
/**
15
 * Trait ActionAdd
16
 * @package Apps\Controller\Api\Comments
17
 * @property Request $request
18
 * @property Response $response
19
 * @method void setJsonHeader()
20
 */
21
trait ActionAdd
22
{
23
    /**
24
     * Add comment or answer via ajax.
25
     * @return string
26
     * @throws NativeException
27
     * @throws \Ffcms\Core\Exception\JsonException
28
     */
29
    public function add(): ?string
30
    {
31
        $this->setJsonHeader();
32
        $configs = AppRecord::getConfigs('widget', 'Comments');
33
34
        $replayTo = (int)$this->request->request->get('replay-to');
35
        $model = null;
36
        // check if its a answer (comment answer type)
37
        if ($replayTo > 0) {
38
            $model = new CommentAnswerAdd($configs);
0 ignored issues
show
It seems like $configs can also be of type null and string; however, parameter $configs of Apps\Model\Api\Comments\...nswerAdd::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            $model = new CommentAnswerAdd(/** @scrutinizer ignore-type */ $configs);
Loading history...
39
            $model->replayTo = $replayTo;
40
        } else { // sounds like new comment row
41
            $model = new CommentPostAdd($configs);
0 ignored issues
show
It seems like $configs can also be of type null and string; however, parameter $configs of Apps\Model\Api\Comments\...tPostAdd::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            $model = new CommentPostAdd(/** @scrutinizer ignore-type */ $configs);
Loading history...
42
            $model->appName = App::$Security->strip_tags($this->request->request->get('app_name'));
43
            $model->appId = (int)$this->request->request->get('app_id');
44
        }
45
46
        // pass general comment params to model
47
        $model->message = App::$Security->secureHtml((string)$this->request->request->get('message'));
48
        $model->guestName = App::$Security->strip_tags($this->request->request->get('guest-name'));
49
50
        // check model conditions before add new row
51
        if (!$model|| !$model->check()) {
52
            throw new NativeException('Unknown error');
53
        }
54
55
        // add comment post or answer to database and get response active record row
56
        $record = $model->buildRecord();
57
        // pass row to entity builder model
58
        $response = new EntityCommentData($record);
59
60
        return json_encode([
61
            'status' => 1,
62
            'data' => $response->make() // build row to standard format
63
        ]);
64
    }
65
}
66