use App\Helpers\EmailBlacklistUpdater;
use Illuminate\Support\Str;
use Psr\SimpleCache\InvalidArgumentException;
class EmailBlacklistValidator
{
private $domains = [];
public function message($message, $attribute, $rule, $parameters)
{
return \sprintf('%s domain is not allowed. Throwaway email providers are blacklisted.', $attribute);
}
public function validate($attribute, $value, $parameters)
{
$this->refresh();
$domain = Str::after(\strtolower($value), '@');
return ! \in_array($domain, $this->domains, true);
}
public function refresh()
{
$this->shouldUpdate();
$this->domains = \cache()->get(\config('email-blacklist.cache-key'));
$this->appendCustomDomains();
}
protected function shouldUpdate()
{
$autoupdate = \config('email-blacklist.auto-update');
try {
if ($autoupdate && ! \cache()->has(\config('email-blacklist.cache-key'))) {
EmailBlacklistUpdater::update();
}
} catch (InvalidArgumentException) {
}
}
protected function appendCustomDomains()
{
$appendList = \config('email-blacklist.append');
if ($appendList === null) {
return;
}
$appendDomains = \explode('|', \strtolower($appendList));
$this->domains = \array_merge($this->domains, $appendDomains);
}
}