FormIgnoreAdd::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Apps\Model\Front\Profile;
4
5
use Apps\ActiveRecord\Blacklist;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Interfaces\iUser;
8
9
/**
10
 * Class FormIgnoreAdd. Business logic of add users to ignore list in database for current user.
11
 * @package Apps\Model\Front\Profile
12
 */
13
class FormIgnoreAdd extends Model
14
{
15
    public $id;
16
    public $comment;
17
18
    private $_user;
19
20
    /**
21
     * FormIgnoreAdd constructor. Pass user object inside.
22
     * @param iUser $user
23
     */
24
    public function __construct(iUser $user)
25
    {
26
        $this->_user = $user;
27
        parent::__construct(true);
28
    }
29
30
    /**
31
     * Labels for form
32
     * @return array
33
     */
34
    public function labels(): array
35
    {
36
        return [
37
            'id' => __('User ID'),
38
            'comment' => __('Comment')
39
        ];
40
    }
41
42
    /**
43
     * Validation rules
44
     */
45
    public function rules(): array
46
    {
47
        return [
48
            ['id', 'required'],
49
            ['comment', 'used'],
50
            ['id', 'int'],
51
            ['id', 'App::$User::isExist']
52
        ];
53
    }
54
55
    /**
56
     * Make save
57
     * @return bool
58
     */
59
    public function save()
60
    {
61
        // check if target is myself or always exist in block list
62
        if ($this->_user->getId() === (int)$this->id || Blacklist::have($this->_user->getId(), $this->id)) {
63
            return false;
64
        }
65
66
        // save data to db
67
        $record = new Blacklist();
68
        $record->user_id = $this->_user->getId();
69
        $record->target_id = $this->id;
70
        $record->comment = $this->comment;
71
        $record->save();
72
73
        return true;
74
    }
75
}
76