UserNotificationSettings.php in Comment Notify 8
File
src/UserNotificationSettings.php
View source
<?php
namespace Drupal\comment_notify;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\user\UserDataInterface;
class UserNotificationSettings {
protected $userData;
protected $configFactory;
public function __construct(UserDataInterface $userData, ConfigFactoryInterface $configFactory) {
$this->userData = $userData;
$this->configFactory = $configFactory;
}
public function getSettings($uid) {
if (!isset($users[$uid])) {
if (is_null($uid)) {
throw new \Exception('Cannot get user preference, uid missing');
}
if ($uid == 0) {
$users[0] = $this
->getDefaultSettings();
}
else {
$settings = $this->userData
->get('comment_notify', $uid);
$users[$uid] = empty($settings) ? NULL : $settings;
}
}
return $users[$uid];
}
public function getDefaultSettings() {
$config = $this->configFactory
->get('comment_notify.settings');
return [
'comment_notify' => $config
->get('enable_default.watcher'),
'entity_notify' => $config
->get('enable_default.entity_author'),
];
}
public function deleteSettings($uid) {
return $this->userData
->delete('comment_notify', $uid);
}
public function getSetting($uid, $setting) {
$settings = $this
->getSettings($uid);
if (!$settings) {
$settings = $this
->getDefaultSettings();
}
return $settings[$setting];
}
function saveSettings($uid, $entity_notification = NULL, $comment_notification = NULL) {
if (!$uid) {
throw new \Exception('Cannot set user preference, uid missing');
}
if (!is_null($entity_notification)) {
$this->userData
->set('comment_notify', $uid, 'entity_notify', $entity_notification);
}
if (!is_null($comment_notification)) {
$this->userData
->set('comment_notify', $uid, 'comment_notify', $comment_notification);
}
}
}