entityManager = $entityManager; $this->httpClient = $httpClient; $this->sms_gateway_api_key = $sms_gateway_api_key; } protected function configure(): void { $this ->setName(self::$defaultName) ->setDescription(self::$defaultDescription); } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $io->title('Starting uncontacted number check...'); // 1. Get uncontacted rows with a date in the past $now = new DateTime(); $phoneNumbersToContact = $this->entityManager->getRepository(Contacts::class)->createQueryBuilder('p') ->where('p.contacted = :state_contacted') ->andWhere('p.due_date < :now') ->setParameter('state_contacted', false) ->setParameter('now', $now) ->getQuery() ->getResult(); if (empty($phoneNumbersToContact)) { $io->success('No uncontacted phone numbers found with past dates. Nothing to do.'); return Command::SUCCESS; } $io->progressStart(count($phoneNumbersToContact)); // 2. Loop through each eligible phone number foreach ($phoneNumbersToContact as $phoneNumber) { $io->progressAdvance(); // 3. Contact the HTTP REST API try { $response = $this->httpClient->request('GET', self::$apiEndpoint, [ 'query' => [ 'producttoken' => $this->sms_gateway_api_key, 'body' => 'Bitte starten Sie die Umfrage jetzt: https://umfragetool.ukbonn.de/login?id=QMBEFR_TEST Ihr UKB.', 'to' => $phoneNumber, 'from' => 'UKB', 'reference' => 'Test-Reference' ] ]); // 4. Check the API response status if ($response->getStatusCode() === 200) { // 5. If successful, update the row in the database $phoneNumber->setContacted(true); $phoneNumber->setGatewayResponse(trim($response->getStatusCode() . ' ' . $response->getContent())); $phoneNumber->setContactedAt(new DateTimeImmutable()); $this->entityManager->persist($phoneNumber); $io->note(sprintf('Successfully contacted number %s', $phoneNumber->getPhoneNumber())); } else { $io->warning(sprintf('API call for number %s failed with status code %s', $phoneNumber->getPhoneNumber(), $response->getStatusCode())); } } catch (Exception $e) { $io->error(sprintf('An error occurred contacting the API for number %s: %s', $phoneNumber->getPhoneNumber(), $e->getMessage())); } } // Finalize all database changes $this->entityManager->flush(); $io->progressFinish(); $io->success('Contacting process complete!'); return Command::SUCCESS; } }