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

FormWallPost   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 31
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 6 1
A types() 0 4 1
B makePost() 0 36 8
1
<?php
2
3
namespace Apps\Model\Front\Profile;
4
5
use Apps\ActiveRecord\WallPost as WallRecords;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Helper\Date;
8
use Ffcms\Core\Helper\Text;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Interfaces\iUser;
11
12
/**
13
 * Class FormWallPost. Add wall post business logic model
14
 * @package Apps\Model\Front\Profile
15
 */
16
class FormWallPost extends Model
17
{
18
    const MAX_MESSAGE_LENGTH = 5000; // 5000 symbols
19
    const POST_GLOBAL_DELAY = 30; // delay between 2 posts from 1 user in seconds
20
21
    public $message;
22
23
    /**
24
     * Validate rules for message field
25
     * @return array
26
     */
27
    public function rules(): array
28
    {
29
        return [
30
            ['message', 'required', null, true, true],
31
            ['message', 'length_min', 5, null, true, true],
32
            ['message', 'length_max', self::MAX_MESSAGE_LENGTH, null, true, true]
33
        ];
34
    }
35
36
    public function types(): array
37
    {
38
        return [
39
            'message' => 'html'
40
        ];
41
    }
42
43
    /**
44
     * Make post to user wall from $viewer to $target instance of iUser interface objects
45
     * @param iUser $target
46
     * @param iUser $viewer
47
     * @param int $delay
48
     * @return bool
49
     */
50
    public function makePost(iUser $target, iUser $viewer, $delay = 60)
51
    {
52
        if (!$target || !$viewer) {
0 ignored issues
show
introduced by zenn
$viewer is of type Ffcms\Core\Interfaces\iUser, thus it always evaluated to true.
Loading history...
introduced by zenn
$target is of type Ffcms\Core\Interfaces\iUser, thus it always evaluated to true.
Loading history...
53
            return false;
54
        }
55
56
        if (!Any::isInt($delay) || $delay < 0) {
57
            $delay = static::POST_GLOBAL_DELAY;
58
        }
59
60
        $find = WallRecords::where('sender_id', '=', $viewer->id)
61
            ->orderBy('updated_at', 'desc')
62
            ->first();
63
        if (!$find) {
64
            $lastPostTime = Date::convertToTimestamp($find->updated_at);
65
            if (time() - $lastPostTime < $delay) { // break execution, passed time is less then default delay
66
                return false;
67
            }
68
        }
69
70
        // save new post to db
71
        $record = new WallRecords();
72
        $record->target_id = $target->id;
73
        $record->sender_id = $viewer->id;
74
        $record->message = $this->message;
75
        $record->save();
76
77
        // add user notification
78
        if ($target->id !== $viewer->id) {
79
            $notify = new EntityAddNotification($target->id);
0 ignored issues
show
Bug introduced by zenn
$target->id of type integer is incompatible with the type boolean expected by parameter $targetId of Apps\Model\Front\Profile...fication::__construct(). ( Ignorable by Annotation )

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

79
            $notify = new EntityAddNotification(/** @scrutinizer ignore-type */ $target->id);
Loading history...
80
            $notify->add('profile/show/' . $target->id . '#wall-post-' . $record->id, EntityAddNotification::MSG_ADD_WALLPOST, ['snippet' => Text::snippet($this->message, 50)]);
81
        }
82
83
        // cleanup message
84
        $this->message = null;
85
        return true;
86
    }
87
}
88