Completed
Push — master ( 9d13eb...060bbf )
by Antonio Carlos
06:41
created

DataRepository::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace PragmaRX\Firewall\Repositories;
4
5
use PragmaRX\Firewall\Support\ServiceInstances;
6
7
class DataRepository implements DataRepositoryInterface
8
{
9
    use ServiceInstances;
10
11
    const IP_ADDRESS_LIST_CACHE_NAME = 'firewall.ip_address_list';
12
13
    /**
14
     * @var \PragmaRX\Firewall\Firewall
15
     */
16
    public $firewall;
17
18
    /**
19
     * Check if a string is a valid country info.
20
     *
21
     * @param $country
22
     *
23
     * @return bool
24
     */
25
    public function validCountry($country)
26
    {
27
        return $this->countries()->validCountry($country);
28
    }
29
30
    /**
31
     * Get country code from an IP address.
32
     *
33
     * @param $ip_address
34
     *
35
     * @return string
36
     */
37
    public function getCountryFromIp($ip_address)
38
    {
39
        return $this->countries()->getCountryFromIp($ip_address);
40
    }
41
42
    /**
43
     * Get all IP addresses by country.
44
     *
45
     * @param $country
46
     *
47
     * @return \Illuminate\Support\Collection
48
     */
49
    public function allByCountry($country)
50
    {
51
        $country = $this->makeCountryFromString($country);
52
53
        return $this->ipList()->all()->filter(function ($item) use ($country) {
54
            return $item['ip_address'] == $country ||
55
                $this->makeCountryFromString($this->getCountryFromIp($item['ip_address'])) == $country;
56
        });
57
    }
58
59
    /**
60
     * Make a country info from a string.
61
     *
62
     * @param $country
63
     *
64
     * @return string
65
     */
66
    public function makeCountryFromString($country)
67
    {
68
        return $this->countries()->makeCountryFromString($country);
69
    }
70
71
    /**
72
     * Clear all items from all lists.
73
     *
74
     * @return int
75
     */
76
    public function clear()
77
    {
78
        $deleted = 0;
79
80
        foreach ($this->ipList()->all() as $ip) {
81
            if ($this->remove($ip['ip_address'])) {
82
                $deleted++;
83
            }
84
        }
85
86
        return $deleted;
87
    }
88
89
    /**
90
     * Get the GeoIP instance.
91
     *
92
     * @return object
93
     */
94
    public function getGeoIp()
95
    {
96
        return $this->countries()->getGeoIp();
97
    }
98
99
    /**
100
     * Add an IP to black or whitelist.
101
     *
102
     * @param $whitelist
103
     * @param $ip
104
     * @param bool $force
105
     *
106
     * @return bool
107
     */
108
    public function addToList($whitelist, $ip, $force = false)
109
    {
110
        return $this->ipList()->addToList($whitelist, $ip, $force);
111
    }
112
113
    /**
114
     * Get all IP addresses.
115
     *
116
     * @return \Illuminate\Support\Collection
117
     */
118
    public function all()
119
    {
120
        return $this->ipList()->all();
121
    }
122
123
    /**
124
     * Tell in which list (black/white) an IP address is.
125
     *
126
     * @param $ip_address
127
     *
128
     * @return null|string
129
     */
130
    public function whichList($ip_address)
131
    {
132
        return $this->ipList()->whichList($ip_address);
133
    }
134
135
    /**
136
     * Check if IP address is valid.
137
     *
138
     * @param $ip
139
     *
140
     * @return bool
141
     */
142
    public function ipIsValid($ip)
143
    {
144
        return $this->ipAddress()->isValid($ip);
145
    }
146
147
    /**
148
     * Find an IP address in the data source.
149
     *
150
     * @param string $ip
151
     *
152
     * @return mixed
153
     */
154
    public function find($ip)
155
    {
156
        return $this->ipList()->find($ip);
157
    }
158
159
    /**
160
     * Remove IP from all lists.
161
     *
162
     * @param $ip
163
     *
164
     * @return bool
165
     */
166
    public function remove($ip)
167
    {
168
        return $this->ipList()->remove($ip);
169
    }
170
}
171