Passed
Push — master ( c57047...e9f946 )
by Mihail
06:06
created

Blacklist::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\App as MainApp;
6
use Ffcms\Core\Arch\ActiveModel;
7
8
/**
9
 * Class Blacklist. Active record for user blacklist table
10
 * @package Apps\ActiveRecord
11
 * @property int $id
12
 * @property int $user_id
13
 * @property int $target_id
14
 * @property string $comment
15
 * @property string $created_at
16
 * @property string $updated_at
17
 * @property User $targetUser
18
 */
19
class Blacklist extends ActiveModel
20
{
21
    protected $casts = [
22
        'id' => 'integer',
23
        'user_id' => 'integer',
24
        'target_id' => 'integer',
25
        'comment' => 'string'
26
    ];
27
28
    /**
29
     * Check if current user have in blacklist target_id user
30
     * @param int $currentId
31
     * @param int $targetId
32
     * @return bool
33
     */
34
    public static function have($currentId, $targetId): bool
35
    {
36
        return self::where('user_id', $currentId)
37
            ->where('target_id', $targetId)
38
            ->count() > 0;
39
    }
40
41
    /**
42
     * Check if user1 or user2 have in themself blacklists
43
     * @param int $user1
44
     * @param int $user2
45
     * @return bool
46
     */
47
    public static function check($user1, $user2): bool
48
    {
49
        $query = self::where(function ($query) use ($user1, $user2) {
50
            $query->where('user_id', $user1)
51
                ->where('target_id', $user2);
52
        })->orWhere(function ($query) use ($user1, $user2) {
53
            $query->where('user_id', $user2)
54
                ->where('target_id', $user1);
55
        });
56
57
        return $query->count() < 1;
58
    }
59
60
    /**
61
     * Get target user object relation
62
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
63
     */
64
    public function targetUser()
65
    {
66
        return $this->belongsTo(User::class, 'target_id');
67
    }
68
}
69