| Conditions | 14 |
| Paths | 60 |
| Total Lines | 88 |
| Code Lines | 55 |
| 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 |
||
| 42 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 43 | { |
||
| 44 | $io = new SymfonyStyle($input, $output); |
||
| 45 | $debug = $input->getOption('debug'); |
||
| 46 | $now = new DateTime('now', new DateTimeZone('UTC')); |
||
| 47 | |||
| 48 | if ($debug) { |
||
| 49 | error_log('Debug mode activated'); |
||
| 50 | $io->note('Debug mode activated'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $remindersRepo = $this->entityManager->getRepository(AgendaReminder::class); |
||
| 54 | $reminders = $remindersRepo->findBy(['sent' => false]); |
||
| 55 | |||
| 56 | $senderId = $this->settingsManager->getSetting('agenda.agenda_reminders_sender_id'); |
||
| 57 | $senderId = (int) $senderId ?: $this->getFirstAdminId(); |
||
| 58 | |||
| 59 | $batchCounter = 0; |
||
| 60 | $batchSize = 100; |
||
| 61 | |||
| 62 | foreach ($reminders as $reminder) { |
||
| 63 | $event = $reminder->getEvent(); |
||
| 64 | |||
| 65 | if (!$event) { |
||
| 66 | if ($debug) { |
||
| 67 | error_log('No event found for reminder ID: ' . $reminder->getId()); |
||
| 68 | $io->note('No event found for reminder ID: ' . $reminder->getId()); |
||
| 69 | } |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | |||
| 73 | $eventId = $event->getIid(); |
||
| 74 | $eventEntity = $this->entityManager->getRepository(CCalendarEvent::class)->find($eventId); |
||
| 75 | |||
| 76 | if (!$eventEntity) { |
||
| 77 | if ($debug) { |
||
| 78 | error_log('No event entity found for event ID: ' . $eventId); |
||
| 79 | $io->note('No event entity found for event ID: ' . $eventId); |
||
| 80 | } |
||
| 81 | continue; |
||
| 82 | } |
||
| 83 | |||
| 84 | $notificationDate = clone $event->getStartDate(); |
||
| 85 | $notificationDate->sub($reminder->getDateInterval()); |
||
| 86 | if ($notificationDate > $now) { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | $messageSubject = sprintf('Reminder for event: %s', $event->getTitle()); |
||
| 91 | $messageContent = $this->generateEventDetails($event); |
||
| 92 | $invitees = $this->getInviteesForEvent($event); |
||
| 93 | |||
| 94 | foreach ($invitees as $userId) { |
||
| 95 | MessageManager::send_message_simple( |
||
| 96 | $userId, |
||
| 97 | $messageSubject, |
||
| 98 | $messageContent, |
||
| 99 | $senderId |
||
| 100 | ); |
||
| 101 | |||
| 102 | if ($debug) { |
||
| 103 | error_log("Message sent to user ID: $userId for event: " . $event->getTitle()); |
||
| 104 | $io->note("Message sent to user ID: $userId for event: " . $event->getTitle()); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | $reminder->setSent(true); |
||
| 109 | $batchCounter++; |
||
| 110 | |||
| 111 | if (($batchCounter % $batchSize) === 0) { |
||
| 112 | $this->entityManager->flush(); |
||
| 113 | |||
| 114 | if ($debug) { |
||
| 115 | error_log('Batch of reminders flushed'); |
||
| 116 | $io->note('Batch of reminders flushed'); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | $this->entityManager->flush(); |
||
| 122 | if ($debug) { |
||
| 123 | error_log('Final batch of reminders flushed'); |
||
| 124 | $io->note('Final batch of reminders flushed'); |
||
| 125 | } |
||
| 126 | |||
| 127 | $io->success('Event reminders have been sent successfully.'); |
||
| 128 | |||
| 129 | return Command::SUCCESS; |
||
| 130 | } |
||
| 172 |