| Conditions | 29 |
| Paths | 8530 |
| Total Lines | 144 |
| Code Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 38 | public function sendPendingMessages(int $urlId, bool $debug = false): int |
||
| 39 | { |
||
| 40 | if (!$this->allowed()) { |
||
| 41 | if ($debug) { |
||
| 42 | error_log("Announcements not allowed."); |
||
| 43 | } |
||
| 44 | return 0; |
||
| 45 | } |
||
| 46 | |||
| 47 | $messagesSent = 0; |
||
| 48 | $now = new \DateTime(); |
||
| 49 | |||
| 50 | if ($debug) { |
||
| 51 | error_log("Current time: " . $now->format('Y-m-d H:i:s')); |
||
| 52 | } |
||
| 53 | |||
| 54 | $announcements = $this->announcementRepository->findBy(['sent' => false]); |
||
| 55 | |||
| 56 | if ($debug) { |
||
| 57 | error_log(count($announcements) . " pending announcements found."); |
||
| 58 | } |
||
| 59 | |||
| 60 | $extraField = new ExtraField('user'); |
||
| 61 | $extraFields = $extraField->get_all(['filter = ? AND visible_to_self = ?' => [1, 1]]); |
||
| 62 | |||
| 63 | foreach ($announcements as $announcement) { |
||
| 64 | if (!$announcement->isSent() && $announcement->getDate() < $now) { |
||
| 65 | if ($debug) { |
||
| 66 | error_log("Processing announcement ID: " . $announcement->getId()); |
||
| 67 | } |
||
| 68 | |||
| 69 | $sessionId = $announcement->getSessionId(); |
||
| 70 | $session = $this->sessionRepository->find($sessionId); |
||
| 71 | |||
| 72 | if (!$session) { |
||
| 73 | if ($debug) { |
||
| 74 | error_log("Session not found for session ID: $sessionId"); |
||
| 75 | } |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | $accessUrl = $this->accessUrlRepository->find($urlId); |
||
| 80 | $sessionRelUsers = $this->sessionRepository->getUsersByAccessUrl($session, $accessUrl); |
||
| 81 | $generalCoaches = $session->getGeneralCoaches(); |
||
| 82 | |||
| 83 | if (empty($sessionRelUsers) || $generalCoaches->count() === 0) { |
||
| 84 | if ($debug) { |
||
| 85 | error_log("No users or general coaches found for session ID: $sessionId"); |
||
| 86 | } |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | $coachId = $generalCoaches->first()->getId(); |
||
| 91 | if ($debug) { |
||
| 92 | error_log("Coach ID: $coachId"); |
||
| 93 | } |
||
| 94 | |||
| 95 | $coachList = []; |
||
| 96 | |||
| 97 | $sendToCoaches = $this->shouldSendToCoaches($announcement->getId()); |
||
| 98 | |||
| 99 | $courseList = $session->getCourses(); |
||
| 100 | |||
| 101 | if ($debug) { |
||
| 102 | error_log("Number of courses in session: " . count($courseList)); |
||
| 103 | foreach ($courseList as $sessionRelCourse) { |
||
| 104 | $course = $sessionRelCourse->getCourse(); |
||
| 105 | error_log("Course ID: " . $course->getId() . ", Course Title: " . $course->getTitle()); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($sendToCoaches) { |
||
| 110 | foreach ($courseList as $course) { |
||
| 111 | $coaches = $session->getGeneralCoaches(); |
||
| 112 | $coachList = array_merge($coachList, $coaches->toArray()); |
||
| 113 | } |
||
| 114 | $coachList = array_unique($coachList); |
||
| 115 | } |
||
| 116 | |||
| 117 | $announcement->setSent(true); |
||
| 118 | $this->em->persist($announcement); |
||
| 119 | $this->em->flush(); |
||
| 120 | |||
| 121 | $attachments = ''; |
||
| 122 | $subject = $announcement->getSubject(); |
||
| 123 | |||
| 124 | foreach ($sessionRelUsers as $sessionRelUser) { |
||
| 125 | $user = $sessionRelUser->getUser(); |
||
| 126 | |||
| 127 | if ($debug) { |
||
| 128 | error_log('User ::: ' . $user->getId()); |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($user->getId() !== $coachId) { |
||
| 132 | $courseInfo = $this->getCourseInfo($courseList); |
||
| 133 | $progress = $this->getUserProgress($user->getId(), $courseInfo, $session); |
||
| 134 | |||
| 135 | $message = $this->buildMessage( |
||
| 136 | $announcement, |
||
| 137 | $session, |
||
| 138 | $user, |
||
| 139 | $courseInfo, |
||
| 140 | $attachments, |
||
| 141 | $progress |
||
| 142 | ); |
||
| 143 | |||
| 144 | if (!empty($extraFields)) { |
||
| 145 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 146 | foreach ($extraFields as $extraField) { |
||
| 147 | $valueExtra = $extraFieldValue->get_values_by_handler_and_field_variable($user->getId(), $extraField['variable'], true); |
||
| 148 | $tags['(('.strtolower($extraField['variable']).'))'] = $valueExtra['value'] ?? ''; |
||
| 149 | } |
||
| 150 | $message = str_replace(array_keys($tags), $tags, $message); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($debug) { |
||
| 154 | error_log("Sending email to user ID: " . $user->getId()); |
||
| 155 | } |
||
| 156 | |||
| 157 | $this->sendEmail($user->getId(), $subject, $message, $coachId); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | $coachMessage = $this->buildCoachMessage($announcement, $generalCoaches, $message); |
||
| 162 | foreach ($coachList as $courseCoach) { |
||
| 163 | if ($debug) { |
||
| 164 | error_log("Sending email to coach ID: " . $courseCoach->getId()); |
||
| 165 | } |
||
| 166 | $this->sendEmail($courseCoach->getId(), $subject, $coachMessage, $coachId); |
||
| 167 | } |
||
| 168 | |||
| 169 | $messagesSent++; |
||
| 170 | |||
| 171 | if ($debug) { |
||
| 172 | error_log("Messages sent for announcement ID: " . $announcement->getId()); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | if ($debug) { |
||
| 178 | error_log("Total messages sent: $messagesSent"); |
||
| 179 | } |
||
| 180 | |||
| 181 | return $messagesSent; |
||
| 182 | } |
||
| 328 |