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