1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Propaganistas\LaravelDisposableEmail; |
4
|
|
|
|
5
|
|
|
use ErrorException; |
6
|
|
|
use Illuminate\Contracts\Cache\Repository as Cache; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Propaganistas\LaravelDisposableEmail\Traits\ParsesJson; |
9
|
|
|
|
10
|
|
|
class DisposableDomains |
11
|
|
|
{ |
12
|
|
|
use ParsesJson; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The storage path to retrieve from and save to. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $storagePath; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Array of retrieved disposable domains. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $domains = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The cache repository. |
30
|
|
|
* |
31
|
|
|
* @var \Illuminate\Contracts\Cache\Repository|null |
32
|
|
|
*/ |
33
|
|
|
protected $cache; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The cache key. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $cacheKey; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Disposable constructor. |
44
|
|
|
* |
45
|
|
|
* @param \Illuminate\Contracts\Cache\Repository|null $cache |
46
|
|
|
*/ |
47
|
63 |
|
public function __construct(Cache $cache = null) |
48
|
|
|
{ |
49
|
63 |
|
$this->cache = $cache; |
50
|
63 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Loads the domains from cache/storage into the class. |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
63 |
|
public function bootstrap() |
58
|
|
|
{ |
59
|
63 |
|
if ($this->cache) { |
60
|
54 |
|
$data = $inCache = $this->getFromCache(); |
61
|
|
|
} |
62
|
|
|
|
63
|
63 |
|
if (! isset($data) || ! $data) { |
64
|
63 |
|
$data = $this->getFromStorage(); |
65
|
|
|
} |
66
|
|
|
|
67
|
63 |
|
if ($this->cache && (! isset($inCache) || ! $inCache)) { |
68
|
54 |
|
$this->cache->forever($this->getCacheKey(), $data); |
69
|
|
|
} |
70
|
|
|
|
71
|
63 |
|
$this->domains = $data; |
72
|
|
|
|
73
|
63 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the domains from cache. |
78
|
|
|
* |
79
|
|
|
* @return array|null |
80
|
|
|
*/ |
81
|
54 |
|
protected function getFromCache() |
82
|
|
|
{ |
83
|
54 |
|
return $this->cache->get($this->getCacheKey()); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the domains from storage, or if non-existent, from the package. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
63 |
|
protected function getFromStorage() |
92
|
|
|
{ |
93
|
|
|
try { |
94
|
63 |
|
if ($data = $this->parseJson(file_get_contents($this->getStoragePath()))) { |
95
|
12 |
|
return $data; |
|
|
|
|
96
|
|
|
} |
97
|
51 |
|
} catch (ErrorException $e) { |
98
|
|
|
// File does not exist or could not be opened. |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Fall back to the list provided by the package. |
102
|
60 |
|
return $this->parseJson(file_get_contents(__DIR__.'/../domains.json')); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checks whether the given email address' domain matches a disposable email service. |
107
|
|
|
* |
108
|
|
|
* @param string $email |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
15 |
|
public function isDisposable($email) |
112
|
|
|
{ |
113
|
15 |
|
if ($domain = Arr::get(explode('@', $email, 2), 1)) { |
114
|
15 |
|
return in_array($domain, $this->domains); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Just ignore this validator if the value doesn't even resemble an email or domain. |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Checks whether the given email address' domain doesn't match a disposable email service. |
123
|
|
|
* |
124
|
|
|
* @param string $email |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
15 |
|
public function isNotDisposable($email) |
128
|
|
|
{ |
129
|
15 |
|
return ! $this->isDisposable($email); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Alias of "isNotDisposable". |
134
|
|
|
* |
135
|
|
|
* @param string $email |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
3 |
|
public function isIndisposable($email) |
139
|
|
|
{ |
140
|
3 |
|
return $this->isNotDisposable($email); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Flushes the cache if applicable. |
145
|
|
|
*/ |
146
|
63 |
|
public function flushCache() |
147
|
|
|
{ |
148
|
63 |
|
if ($this->cache) { |
149
|
54 |
|
$this->cache->forget($this->getCacheKey()); |
150
|
|
|
} |
151
|
63 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Flushes the source's list if applicable. |
155
|
|
|
*/ |
156
|
63 |
|
public function flushSource() |
157
|
|
|
{ |
158
|
63 |
|
if (is_file($this->getStoragePath())) { |
159
|
15 |
|
@unlink($this->getStoragePath()); |
|
|
|
|
160
|
|
|
} |
161
|
63 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the storage path. |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
63 |
|
public function getStoragePath() |
169
|
|
|
{ |
170
|
63 |
|
return $this->storagePath; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set the storage path. |
175
|
|
|
* |
176
|
|
|
* @param string $path |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
63 |
|
public function setStoragePath($path) |
180
|
|
|
{ |
181
|
63 |
|
$this->storagePath = $path; |
182
|
|
|
|
183
|
63 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the cache key. |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
57 |
|
public function getCacheKey() |
192
|
|
|
{ |
193
|
57 |
|
return $this->cacheKey; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Set the cache key. |
198
|
|
|
* |
199
|
|
|
* @param string $key |
200
|
|
|
* @return $this |
201
|
|
|
*/ |
202
|
63 |
|
public function setCacheKey($key) |
203
|
|
|
{ |
204
|
63 |
|
$this->cacheKey = $key; |
205
|
|
|
|
206
|
63 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.