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