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

Apps/Controller/Api/Profile/ActionMessageSend.php (1 issue)

Checks if the types of the passed arguments in a function/method call are compatible.

Bug Minor
1
<?php
2
3
4
namespace Apps\Controller\Api\Profile;
5
6
use Apps\ActiveRecord\Blacklist;
7
use Apps\ActiveRecord\Message;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Exception\NativeException;
11
use Ffcms\Core\Helper\Type\Any;
12
use Ffcms\Core\Helper\Type\Str;
13
use Ffcms\Core\Network\Request;
14
use Ffcms\Core\Network\Response;
15
16
/**
17
 * Trait ActionMessageSend
18
 * @package Apps\Controller\Api\Profile
19
 * @property Request $request
20
 * @property Response $response
21
 * @method void setJsonHeader
22
 */
23
trait ActionMessageSend
24
{
25
26
    /**
27
     * Send message via AJAX
28
     * @param string $targetId
29
     * @return string
30
     * @throws ForbiddenException
31
     * @throws NativeException
32
     */
33
    public function messageSend(string $targetId): ?string
34
    {
35
        if (!Any::isInt($targetId) || $targetId < 1) {
36
            throw new NativeException('Bad target id format');
37
        }
38
39
        // check if user is auth
40
        if (!App::$User->isAuth()) {
41
            throw new ForbiddenException('Auth required');
42
        }
43
44
        // get current user object and check in blacklist
45
        $user = App::$User->identity();
46
        if (!Blacklist::check($user->id, $targetId)) {
47
            throw new ForbiddenException('In blacklist');
48
        }
49
50
        // check input params
51
        $msg = App::$Security->strip_tags($this->request->get('message'));
52
        if (!Any::isInt($targetId) || $targetId < 1 || Str::length($msg) < 1) {
0 ignored issues
show
It seems like $msg can also be of type array; however, parameter $string of Ffcms\Core\Helper\Type\Str::length() does only seem to accept null|string, 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

52
        if (!Any::isInt($targetId) || $targetId < 1 || Str::length(/** @scrutinizer ignore-type */ $msg) < 1) {
Loading history...
53
            throw new NativeException('Wrong input data');
54
        }
55
56
        $this->setJsonHeader();
57
58
        // try to save message
59
        $message = new Message();
60
        $message->target_id = $targetId;
61
        $message->sender_id = $user->id;
62
        $message->message = $msg;
63
        $message->save();
64
65
        return json_encode(['status' => 1]);
66
    }
67
}
68