SpammerCollection::whereBlocked()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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