public static function setPreferences($user, $templates, $options = [])
{
$templates = (array) $templates;
if (!$user) {
throw new Exception('A user must be provided for MailBlocker::setPreferences');
}
extract(array_merge(['aliases' => [], 'fillable' => [], 'verify' => false], $options));
if ($aliases) {
$fillable = array_merge($fillable, array_values($aliases));
$templates = array_build($templates, function ($key, $value) use($aliases) {
return [array_get($aliases, $key, $key), $value];
});
}
if ($fillable) {
$templates = array_intersect_key($templates, array_flip($fillable));
}
if ($verify) {
$existing = MailTemplate::listAllTemplates();
$templates = array_intersect_key($templates, $existing);
}
$currentBlocks = array_flip(static::checkAllForUser($user));
foreach ($templates as $template => $value) {
// User wants to receive mail and is blocking
if ($value && isset($currentBlocks[$template])) {
static::removeBlock($template, $user);
} elseif (!$value && !isset($currentBlocks[$template])) {
static::addBlock($template, $user);
}
}
}