Passed
Push — master ( d3fd5d...81f334 )
by Mihail
03:51
created

ActionMessageSend::messageSend()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 5
nop 1
dl 0
loc 34
rs 5.3846
c 0
b 0
f 0
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
Bug introduced by
It seems like $msg defined by \Ffcms\Core\App::$Securi...equest->get('message')) on line 51 can also be of type array; however, Ffcms\Core\Helper\Type\Str::length() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
            throw new NativeException('Wrong input data');
54
        }
55
56
        $this->setJsonHeader();
0 ignored issues
show
Bug introduced by
It seems like setJsonHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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