Completed
Pull Request — master (#3)
by ARCANEDEV
06:34
created

Spammers::allowOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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