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

Controller/Api/Profile/ActionSendWallAnswer.php (5 issues)

1
<?php
2
3
namespace Apps\Controller\Api\Profile;
4
5
use Apps\ActiveRecord\Blacklist;
6
use Apps\ActiveRecord\WallAnswer;
7
use Apps\ActiveRecord\WallPost;
8
use Apps\Model\Front\Profile\EntityAddNotification;
9
use Ffcms\Core\App;
10
use Ffcms\Core\Exception\ForbiddenException;
11
use Ffcms\Core\Exception\NativeException;
12
use Ffcms\Core\Helper\Date;
13
use Ffcms\Core\Helper\Type\Any;
14
use Ffcms\Core\Helper\Type\Str;
15
use Ffcms\Core\Network\Request;
16
use Ffcms\Core\Network\Response;
17
18
/**
19
 * Trait ActionSendWallAnswer
20
 * @package Apps\Controller\Api\Profile
21
 * @property Request $request
22
 * @property Response $response
23
 * @method void setJsonHeader()
24
 */
25
trait ActionSendWallAnswer
26
{
27
    /**
28
     * Add new post answer from AJAX post
29
     * @param string $postId
30
     * @return string
31
     * @throws ForbiddenException
32
     * @throws NativeException
33
     * @throws \Ffcms\Core\Exception\SyntaxException
34
     */
35
    public function sendWallAnswer(string $postId): ?string
36
    {
37
        $this->setJsonHeader();
38
39
        // not auth? what are you doing there? ;)
40
        if (!App::$User->isAuth()) {
41
            throw new ForbiddenException('Auth required');
42
        }
43
44
        // no post id? wtf you doing man!
45
        if (!Any::isInt($postId) || $postId < 1) {
46
            throw new NativeException('Wrong input data');
47
        }
48
49
        // get current(sender) user object
50
        $viewer = App::$User->identity();
51
52
        // get message from post and validate minlength
53
        $message = $this->request->get('message');
54
        $message = App::$Security->strip_tags($message);
55
        if (!Any::isStr($message) || Str::length($message) < 3) {
0 ignored issues
show
It seems like $message 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

55
        if (!Any::isStr($message) || Str::length(/** @scrutinizer ignore-type */ $message) < 3) {
Loading history...
56
            throw new ForbiddenException('Wrong input data');
57
        }
58
59
        // try to find this post
60
        $wallPost = WallPost::where('id', '=', $postId);
61
        if ($wallPost->count() < 1) {
62
            throw new NativeException('Wrong input data');
63
        }
64
65
        $wallRow = $wallPost->first();
66
        $targetId = $wallRow->target_id;
0 ignored issues
show
The property target_id does not seem to exist on Ffcms\Core\Arch\ActiveModel. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
67
        // check if in blacklist
68
        if (!Blacklist::check($viewer->id, $targetId)) {
69
            throw new ForbiddenException('User is blocked!');
70
        }
71
72
        // check delay between user last post and current
73
        $lastAnswer = WallAnswer::where('user_id', '=', App::$User->identity()->getId())
74
            ->orderBy('created_at', 'DESC')
75
            ->first();
76
        if (!$lastAnswer) {
77
            $now = time();
78
            $answerTime = Date::convertToTimestamp($lastAnswer->created_at);
79
            $cfgs = \Apps\ActiveRecord\App::getConfigs('app', 'Profile');
80
            // hmm, maybe past less then delay required?
81
            if ($now - (int)$cfgs['delayBetweenPost'] < $answerTime) {
82
                throw new ForbiddenException('Delay between answers not pass');
83
            }
84
        }
85
86
        // make new row ;)
87
        $answers = new WallAnswer();
88
        $answers->post_id = $postId;
89
        $answers->user_id = $viewer->id;
90
        $answers->message = $message;
0 ignored issues
show
Documentation Bug introduced by zenn
It seems like $message can also be of type array. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
91
        $answers->save();
92
93
        // add notification for target user
94
        if ($viewer->id !== $targetId) {
95
            $notify = new EntityAddNotification($targetId);
96
            $notify->add('/profile/show/' . $targetId . '#wall-post-' . $wallRow->id, EntityAddNotification::MSG_ADD_WALLANSWER, [
97
                'snippet' => Text::snippet($message, 50),
0 ignored issues
show
The type Apps\Controller\Api\Profile\Text was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
                'post' => $wallRow->message
0 ignored issues
show
The property message does not seem to exist on Ffcms\Core\Arch\ActiveModel. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
99
            ]);
100
        }
101
102
        return json_encode(['status' => 1, 'message' => 'ok']);
103
    }
104
}
105