1 | <?php |
||
33 | class UserSettings { |
||
34 | /** @var IManager */ |
||
35 | protected $manager; |
||
36 | |||
37 | /** @var IConfig */ |
||
38 | protected $config; |
||
39 | |||
40 | /** @var Data */ |
||
41 | protected $data; |
||
42 | |||
43 | const EMAIL_SEND_HOURLY = 0; |
||
44 | const EMAIL_SEND_DAILY = 1; |
||
45 | const EMAIL_SEND_WEEKLY = 2; |
||
46 | |||
47 | /** |
||
48 | * @param IManager $manager |
||
49 | * @param IConfig $config |
||
50 | */ |
||
51 | 26 | public function __construct(IManager $manager, IConfig $config) { |
|
52 | 26 | $this->manager = $manager; |
|
53 | 26 | $this->config = $config; |
|
54 | 26 | } |
|
55 | |||
56 | /** |
||
57 | * Get a setting for a user |
||
58 | * |
||
59 | * Falls back to some good default values if the user does not have a preference |
||
60 | * |
||
61 | * @param string $user |
||
62 | * @param string $method Should be one of 'stream', 'email' or 'setting' |
||
63 | * @param string $type One of the activity types, 'batchtime' or 'self' |
||
64 | * @return bool|int |
||
65 | */ |
||
66 | 6 | public function getUserSetting($user, $method, $type) { |
|
84 | |||
85 | /** |
||
86 | * Get a good default setting for a preference |
||
87 | * |
||
88 | * @param string $method Should be one of 'stream', 'email' or 'setting' |
||
89 | * @param string $type One of the activity types, 'batchtime', 'self' or 'selfemail' |
||
90 | * @return bool|int |
||
91 | */ |
||
92 | 12 | public function getDefaultSetting($method, $type) { |
|
93 | 12 | if ($method === 'setting') { |
|
94 | 10 | if ($type === 'batchtime') { |
|
95 | 2 | return 3600; |
|
96 | 9 | } else if ($type === 'self') { |
|
97 | 7 | return true; |
|
98 | 4 | } else if ($type === 'selfemail') { |
|
99 | 3 | return false; |
|
100 | } |
||
101 | 1 | return false; |
|
102 | } |
||
103 | |||
104 | try { |
||
105 | 8 | $setting = $this->manager->getSettingById($type); |
|
106 | 8 | return ($method === 'stream') ? $setting->isDefaultEnabledStream() : $setting->isDefaultEnabledMail(); |
|
107 | 5 | } catch (\InvalidArgumentException $e) { |
|
108 | 5 | return false; |
|
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Get a list with enabled notification types for a user |
||
114 | * |
||
115 | * @param string $user Name of the user |
||
116 | * @param string $method Should be one of 'stream' or 'email' |
||
117 | * @return array |
||
118 | */ |
||
119 | 5 | public function getNotificationTypes($user, $method) { |
|
120 | 5 | $notificationTypes = array(); |
|
121 | |||
122 | 5 | $settings = $this->manager->getSettings(); |
|
123 | 5 | foreach ($settings as $setting) { |
|
124 | 5 | if ($this->getUserSetting($user, $method, $setting->getIdentifier())) { |
|
125 | 5 | $notificationTypes[] = $setting->getIdentifier(); |
|
126 | 5 | } |
|
127 | 5 | } |
|
128 | |||
129 | 5 | return $notificationTypes; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Filters the given user array by their notification setting |
||
134 | * |
||
135 | * @param array $users |
||
136 | * @param string $method |
||
137 | * @param string $type |
||
138 | * @return array Returns a "username => b:true" Map for method = stream |
||
139 | * Returns a "username => i:batchtime" Map for method = email |
||
140 | */ |
||
141 | public function filterUsersBySetting($users, $method, $type) { |
||
181 | } |
||
182 |