HasBlocklist::toAllowlist()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace LaraBlockList\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use LaraBlockList\Enums\BlockList;
7
8
trait HasBlocklist
9
{
10
11
    /**
12
     * Boot trait.
13
     */
14 6
    public static function bootHasBlocklist(): void
15
    {
16 6
        static::addGlobalScope(static::globalScopeAllowlisted(), function (Builder $builder) {
17 5
            $builder->where(function (Builder $builder) {
18 5
                $builder->where(static::blocklistFieldName(), '<>', BlockList::BLOCKLISTED->value)
19 5
                        ->orWhereNull(static::blocklistFieldName());
20 5
            });
21 6
        });
22
    }
23
24
    /**
25
     * Scope a query to only include blocklisted rows.
26
     *
27
     * @param $query
28
     *
29
     * @return mixed
30
     */
31 4
    public function scopeBlocklisted($query): mixed
32
    {
33 4
        return $query->withoutGlobalScope(static::globalScopeAllowlisted())
34 4
                     ->where(static::blocklistFieldName(), '=', BlockList::BLOCKLISTED->value);
35
    }
36
37
    /**
38
     * Global scope name.
39
     *
40
     * @return string
41
     */
42 6
    public static function globalScopeAllowlisted(): string
43
    {
44 6
        return 'withoutBlocklist';
45
    }
46
47
    /**
48
     * Blocklist attribute name.
49
     *
50
     * @return string
51
     */
52 6
    public static function blocklistFieldName(): string
53
    {
54 6
        return config('blocklist.default.db_field_name');
55
    }
56
57
    /**
58
     * Check is row allowlisted.
59
     *
60
     * @return bool
61
     */
62 4
    public function isAllowlisted(): bool
63
    {
64 4
        return $this->{static::blocklistFieldName()} == BlockList::ALLOWLISTED->value;
65
    }
66
67
    /**
68
     * Check is row blocklisted.
69
     *
70
     * @return bool
71
     */
72 2
    public function isBlocklisted(): bool
73
    {
74 2
        return $this->{static::blocklistFieldName()} == BlockList::BLOCKLISTED->value;
75
    }
76
77
    /**
78
     * Add entity to blocklist.
79
     *
80
     * @param bool $permanently
81
     *
82
     * @return static
83
     */
84 5
    public function toBlocklist(bool $permanently = false): static
85
    {
86 5
        $this->{static::blocklistFieldName()} = BlockList::BLOCKLISTED->value;
87 5
        if ($permanently) {
88 2
            $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

88
            $this->/** @scrutinizer ignore-call */ 
89
                   save();
Loading history...
89
        }
90
91 5
        return $this;
92
    }
93
94
    /**
95
     * Add entity to allowlist.
96
     *
97
     * @param bool $permanently
98
     *
99
     * @return static
100
     */
101 4
    public function toAllowlist(bool $permanently = false): static
102
    {
103 4
        $this->{static::blocklistFieldName()} = BlockList::ALLOWLISTED->value;
104 4
        if ($permanently) {
105 4
            $this->save();
106
        }
107
108 4
        return $this;
109
    }
110
}
111