| Total Complexity | 53 |
| Total Lines | 295 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SendEventRemindersCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SendEventRemindersCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | #[AsCommand( |
||
| 30 | name: 'app:send-event-reminders', |
||
| 31 | description: 'Send notification messages to users that have reminders from events in their agenda.', |
||
| 32 | )] |
||
| 33 | class SendEventRemindersCommand extends Command |
||
| 34 | { |
||
| 35 | public function __construct( |
||
| 43 | } |
||
| 44 | |||
| 45 | protected function configure(): void |
||
| 50 | ; |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Resolve the DateTimeZone to use for a given user (CLI-safe, no legacy api_* calls). |
||
| 200 | * Priority: user's timezone -> platform timezone setting -> UTC. |
||
| 201 | */ |
||
| 202 | private function resolveTimezoneForUser(?User $user): DateTimeZone |
||
| 203 | { |
||
| 204 | // User explicit timezone (if present and valid) |
||
| 205 | $tzId = $user?->getTimezone(); |
||
| 206 | if (\is_string($tzId) && $tzId !== '') { |
||
| 207 | try { |
||
| 208 | return new DateTimeZone($tzId); |
||
| 209 | } catch (\Throwable) { |
||
| 210 | // keep going |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | // Platform timezone setting (equivalent to api_get_setting('platform.timezone', false, 'timezones')) |
||
| 215 | $platformTz = (string) ($this->settingsManager->getSetting('platform.timezone', false, 'timezones') ?? ''); |
||
| 216 | if ($platformTz !== '') { |
||
| 217 | try { |
||
| 218 | return new DateTimeZone($platformTz); |
||
| 219 | } catch (\Throwable) { |
||
| 220 | // keep going |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | return new DateTimeZone('UTC'); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Format a UTC DateTime into the user's local timezone, CLI-safe. |
||
| 229 | */ |
||
| 230 | private function formatForUser(DateTime $utc, ?User $user): string |
||
| 231 | { |
||
| 232 | $dt = (clone $utc); |
||
| 233 | $dt->setTimezone($this->resolveTimezoneForUser($user)); |
||
| 234 | |||
| 235 | // Keep the existing format as used previously. |
||
| 236 | return $dt->format('Y-m-d H:i:s'); |
||
| 237 | } |
||
| 238 | |||
| 239 | private function sendReminderMessage(User $user, CCalendarEvent $event, int $senderId, bool $debug, SymfonyStyle $io, int &$sentRemindersCount): void |
||
| 240 | { |
||
| 241 | $locale = $user->getLocale() ?: 'en'; |
||
| 242 | $this->translator->setLocale($locale); |
||
| 243 | |||
| 244 | $messageSubject = \sprintf( |
||
| 245 | $this->translator->trans('Reminder for event : %s'), |
||
| 246 | $event->getTitle() |
||
| 247 | ); |
||
| 248 | |||
| 249 | // IMPORTANT: build details with user's timezone applied |
||
| 250 | $messageContent = implode(PHP_EOL, $this->generateEventDetails($event, $user)); |
||
| 251 | |||
| 252 | $this->messageHelper->sendMessage( |
||
| 253 | $user->getId(), |
||
| 254 | $messageSubject, |
||
| 255 | $messageContent, |
||
| 256 | [], |
||
| 257 | [], |
||
| 258 | 0, |
||
| 259 | 0, |
||
| 260 | 0, |
||
| 261 | $senderId, |
||
| 262 | 0, |
||
| 263 | false, |
||
| 264 | true |
||
| 265 | ); |
||
| 266 | |||
| 267 | if ($debug) { |
||
| 268 | error_log("Message sent to user ID: {$user->getId()} for event: {$event->getTitle()}"); |
||
| 269 | error_log("Message Subject: {$messageSubject}"); |
||
| 270 | error_log("Message Content: {$messageContent}"); |
||
| 271 | } |
||
| 272 | |||
| 273 | $sentRemindersCount++; |
||
| 274 | } |
||
| 275 | |||
| 276 | private function getFirstAdminId(): int |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Build event details text. If $user is provided, dates are converted to user's local timezone. |
||
| 287 | * Otherwise, keep UTC (backward compatible path). |
||
| 288 | */ |
||
| 289 | private function generateEventDetails(CCalendarEvent $event, ?User $user = null): array |
||
| 326 |