| Conditions | 11 |
| Paths | 18 |
| Total Lines | 76 |
| Code Lines | 40 |
| 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 |
||
| 12 | public static function send_headlines_digests() { |
||
| 13 | |||
| 14 | $user_limit = 15; // amount of users to process (e.g. emails to send out) |
||
| 15 | $limit = 1000; // maximum amount of headlines to include |
||
| 16 | |||
| 17 | Debug::log("Sending digests, batch of max $user_limit users, headline limit = $limit"); |
||
| 18 | |||
| 19 | if (DB_TYPE == "pgsql") { |
||
|
|
|||
| 20 | $interval_qpart = "last_digest_sent < NOW() - INTERVAL '1 days'"; |
||
| 21 | } else if (DB_TYPE == "mysql") { |
||
| 22 | $interval_qpart = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)"; |
||
| 23 | } |
||
| 24 | |||
| 25 | $pdo = Db::pdo(); |
||
| 26 | |||
| 27 | $res = $pdo->query("SELECT id,email FROM ttrss_users |
||
| 28 | WHERE email != '' AND (last_digest_sent IS NULL OR $interval_qpart)"); |
||
| 29 | |||
| 30 | while ($line = $res->fetch()) { |
||
| 31 | |||
| 32 | if (@get_pref('DIGEST_ENABLE', $line['id'], false)) { |
||
| 33 | $preferred_ts = strtotime(get_pref('DIGEST_PREFERRED_TIME', $line['id'], '00:00')); |
||
| 34 | |||
| 35 | // try to send digests within 2 hours of preferred time |
||
| 36 | if ($preferred_ts && time() >= $preferred_ts && |
||
| 37 | time() - $preferred_ts <= 7200 |
||
| 38 | ) { |
||
| 39 | |||
| 40 | Debug::log("Sending digest for UID:".$line['id']." - ".$line["email"]); |
||
| 41 | |||
| 42 | $do_catchup = get_pref('DIGEST_CATCHUP', $line['id'], false); |
||
| 43 | |||
| 44 | global $tz_offset; |
||
| 45 | |||
| 46 | // reset tz_offset global to prevent tz cache clash between users |
||
| 47 | $tz_offset = -1; |
||
| 48 | |||
| 49 | $tuple = Digest::prepare_headlines_digest($line["id"], 1, $limit); |
||
| 50 | $digest = $tuple[0]; |
||
| 51 | $headlines_count = $tuple[1]; |
||
| 52 | $affected_ids = $tuple[2]; |
||
| 53 | $digest_text = $tuple[3]; |
||
| 54 | |||
| 55 | if ($headlines_count > 0) { |
||
| 56 | |||
| 57 | $mailer = new Mailer(); |
||
| 58 | |||
| 59 | //$rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text); |
||
| 60 | |||
| 61 | $rc = $mailer->mail(["to_name" => $line["login"], |
||
| 62 | "to_address" => $line["email"], |
||
| 63 | "subject" => DIGEST_SUBJECT, |
||
| 64 | "message" => $digest_text, |
||
| 65 | "message_html" => $digest]); |
||
| 66 | |||
| 67 | //if (!$rc && $debug) Debug::log("ERROR: " . $mailer->lastError()); |
||
| 68 | |||
| 69 | Debug::log("RC=$rc"); |
||
| 70 | |||
| 71 | if ($rc && $do_catchup) { |
||
| 72 | Debug::log("Marking affected articles as read..."); |
||
| 73 | Article::catchupArticlesById($affected_ids, 0, $line["id"]); |
||
| 74 | } |
||
| 75 | } else { |
||
| 76 | Debug::log("No headlines"); |
||
| 77 | } |
||
| 78 | |||
| 79 | $sth = $pdo->prepare("UPDATE ttrss_users SET last_digest_sent = NOW() |
||
| 80 | WHERE id = ?"); |
||
| 81 | $sth->execute([$line["id"]]); |
||
| 82 | |||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | Debug::log("All done."); |
||
| 88 | |||
| 219 |