Completed
Push — master ( 9aed25...8407f4 )
by ARCANEDEV
8s
created

Spammers::whereHostIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php namespace Arcanedev\SpamBlocker\Entities;
2
3
use Arcanedev\Support\Collection;
4
5
/**
6
 * Class     Spammers
7
 *
8
 * @package  Arcanedev\SpamBlocker\Entities
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Spammers extends Collection
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Main Functions
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Load the items.
19
     *
20
     * @param  array  $spammers
21
     *
22
     * @return static
23
     */
24 232
    public static function load(array $spammers)
25
    {
26 232
        return static::make()->includes($spammers);
27
    }
28 232
29 232
    /**
30 174
     * Add many hosts to the blacklist collection.
31
     *
32 232
     * @param  array  $includes
33
     *
34
     * @return self
35
     */
36
    public function includes(array $includes)
37
    {
38
        foreach ($includes as $include) {
39
            $this->blockOne($include);
40
        }
41
42 72
        return $this;
43
    }
44 72
45 56
    /**
46 54
     * Add many hosts to the whitelist collection.
47
     *
48 72
     * @param  array  $excludes
49
     *
50
     * @return self
51
     */
52
    public function excludes(array $excludes)
53
    {
54
        foreach ($excludes as $exclude) {
55
            $this->allowOne($exclude);
56
        }
57
58 72
        return $this;
59
    }
60 72
61 40
    /**
62 54
     * Add a host to a blacklist collection.
63
     *
64 72
     * @param  string  $host
65
     *
66
     * @return static
67
     */
68
    public function blockOne($host)
69
    {
70
        return $this->exists($host)
71
            ? $this->updateOne($host)
72
            : $this->addOne($host);
73
    }
74 264
75
    /**
76 264
     * Add a host to a whitelist collection.
77 204
     *
78 264
     * @param  string  $host
79
     *
80
     * @return static
81
     */
82
    public function allowOne($host)
83
    {
84
        return $this->exists($host)
85
            ? $this->updateOne($host, false)
86
            : $this->addOne($host, false);
87
    }
88 64
89
    /**
90 64
     * Add a host to the collection.
91 54
     *
92 64
     * @param  string  $host
93
     * @param  bool    $blocked
94
     *
95
     * @return static
96
     */
97
    private function addOne($host, $blocked = true)
98
    {
99
        $this->push([
100
            'host'  => trim(utf8_encode($host)),
101
            'block' => $blocked,
102
        ]);
103 280
104
        return $this;
105 280
    }
106 280
107 280
    /**
108 210
     * Update a host/spammer.
109
     *
110 280
     * @param  string  $host
111
     * @param  bool    $blocked
112
     *
113
     * @return static
114
     */
115
    private function updateOne($host, $blocked = true)
116
    {
117
        $this->items = $this->map(function ($spammer) use ($host, $blocked) {
118
            if ($spammer['host'] === $host && $spammer['block'] !== $blocked) {
119
                $spammer['block'] = $blocked;
120
            }
121
122
            return $spammer;
123 48
        })->all();
124 48
125 16
        return $this;
126 12
    }
127
128 48
    /**
129 48
     * Get a spammer from the collection.
130
     *
131 48
     * @param  string  $host
132
     *
133
     * @return array|null
134
     */
135
    public function getOne($host)
136
    {
137
        return $this->where('host', trim(utf8_encode($host)))
138
            ->first();
139
    }
140
141 280
    /**
142
     * Filter the spammer by the given hosts.
143 280
     *
144 280
     * @param  array  $hosts
145
     * @param  bool   $strict
146
     *
147
     * @return static
148
     */
149
    public function whereHostIn(array $hosts, $strict = false)
150
    {
151
        $values = $this->getArrayableItems($hosts);
152
153
        return $this->filter(function ($item) use ($values, $strict) {
154 280
            return in_array(data_get($item, 'host'), $values, $strict);
155
        });
156 280
    }
157
158
    /**
159
     * Check if a spammer exists in the collection.
160
     *
161
     * @param  string  $host
162
     *
163
     * @return bool
164
     */
165
    public function exists($host)
166
    {
167
        return $this->getOne($host) !== null;
168
    }
169
}
170