Conditions | 38 |
Paths | > 20000 |
Total Lines | 97 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
79 | public static function getContacts($results) |
||
80 | { |
||
81 | if (empty($results)) { |
||
82 | return []; |
||
83 | } |
||
84 | if (Config::get('alert.default_only') === true || Config::get('alerts.email.default_only') === true) { |
||
85 | $email = Config::get('alert.default_mail', Config::get('alerts.email.default')); |
||
86 | return $email ? [$email => ''] : []; |
||
87 | } |
||
88 | $users = LegacyAuth::get()->getUserlist(); |
||
89 | $contacts = array(); |
||
90 | $uids = array(); |
||
91 | foreach ($results as $result) { |
||
92 | $tmp = null; |
||
|
|||
93 | if (is_numeric($result["bill_id"])) { |
||
94 | $tmpa = dbFetchRows("SELECT user_id FROM bill_perms WHERE bill_id = ?", array($result["bill_id"])); |
||
95 | foreach ($tmpa as $tmp) { |
||
96 | $uids[$tmp['user_id']] = $tmp['user_id']; |
||
97 | } |
||
98 | } |
||
99 | if (is_numeric($result["port_id"])) { |
||
100 | $tmpa = dbFetchRows("SELECT user_id FROM ports_perms WHERE port_id = ?", array($result["port_id"])); |
||
101 | foreach ($tmpa as $tmp) { |
||
102 | $uids[$tmp['user_id']] = $tmp['user_id']; |
||
103 | } |
||
104 | } |
||
105 | if (is_numeric($result["device_id"])) { |
||
106 | if (Config::get('alert.syscontact') == true) { |
||
107 | if (dbFetchCell("SELECT attrib_value FROM devices_attribs WHERE attrib_type = 'override_sysContact_bool' AND device_id = ?", [$result["device_id"]])) { |
||
108 | $tmpa = dbFetchCell("SELECT attrib_value FROM devices_attribs WHERE attrib_type = 'override_sysContact_string' AND device_id = ?", array($result["device_id"])); |
||
109 | } else { |
||
110 | $tmpa = dbFetchCell("SELECT sysContact FROM devices WHERE device_id = ?", array($result["device_id"])); |
||
111 | } |
||
112 | if (!empty($tmpa)) { |
||
113 | $contacts[$tmpa] = ''; |
||
114 | } |
||
115 | } |
||
116 | $tmpa = dbFetchRows("SELECT user_id FROM devices_perms WHERE device_id = ?", array($result["device_id"])); |
||
117 | foreach ($tmpa as $tmp) { |
||
118 | $uids[$tmp['user_id']] = $tmp['user_id']; |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | foreach ($users as $user) { |
||
123 | if (empty($user['email'])) { |
||
124 | continue; // no email, skip this user |
||
125 | } |
||
126 | if (empty($user['realname'])) { |
||
127 | $user['realname'] = $user['username']; |
||
128 | } |
||
129 | if (empty($user['level'])) { |
||
130 | $user['level'] = LegacyAuth::get()->getUserlevel($user['username']); |
||
131 | } |
||
132 | if (Config::get('alert.globals') && ( $user['level'] >= 5 && $user['level'] < 10 )) { |
||
133 | $contacts[$user['email']] = $user['realname']; |
||
134 | } elseif (Config::get('alert.admins') && $user['level'] == 10) { |
||
135 | $contacts[$user['email']] = $user['realname']; |
||
136 | } elseif (Config::get('alert.users') == true && in_array($user['user_id'], $uids)) { |
||
137 | $contacts[$user['email']] = $user['realname']; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $tmp_contacts = array(); |
||
142 | foreach ($contacts as $email => $name) { |
||
143 | if (strstr($email, ',')) { |
||
144 | $split_contacts = preg_split('/[,\s]+/', $email); |
||
145 | foreach ($split_contacts as $split_email) { |
||
146 | if (!empty($split_email)) { |
||
147 | $tmp_contacts[$split_email] = $name; |
||
148 | } |
||
149 | } |
||
150 | } else { |
||
151 | $tmp_contacts[$email] = $name; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | if (!empty($tmp_contacts)) { |
||
156 | // Validate contacts so we can fall back to default if configured. |
||
157 | $mail = new PHPMailer(); |
||
158 | foreach ($tmp_contacts as $tmp_email => $tmp_name) { |
||
159 | if ($mail->validateAddress($tmp_email) != true) { |
||
160 | unset($tmp_contacts[$tmp_email]); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | # Copy all email alerts to default contact if configured. |
||
166 | $default_mail = Config::get('alert.default_mail'); |
||
167 | if (!isset($tmp_contacts[$default_mail]) && Config::get('alert.default_copy')) { |
||
168 | $tmp_contacts[$default_mail] = ''; |
||
169 | } |
||
170 | # Send email to default contact if no other contact found |
||
171 | if (empty($tmp_contacts) && Config::get('alert.default_if_none') && $default_mail) { |
||
172 | $tmp_contacts[$default_mail] = ''; |
||
173 | } |
||
174 | |||
175 | return $tmp_contacts; |
||
176 | } |
||
226 |