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 $phoneContact) { $io->progressAdvance(); // 3. Contact the HTTP REST API try { $response = $this->httpClient->request('POST', self::$apiEndpoint, [ 'headers' => [ 'X-CM-PRODUCTTOKEN' => $this->sms_gateway_api_key, 'Accept' => 'application/json', 'Content-Type' => 'application/json' ], 'query' => "{ 'messages': { 'msg': [{ 'allowedChannels': ['SMS'], 'from': 'UKB', 'to': [ { 'number': '$phoneContact->getPhoneNumber()' } ], 'body': { 'type': 'auto', 'content': 'Liebe Patientin, lieber Patient, \n' . 'gerne möchten wir von Ihnen erfahren, wie zufrieden Sie mit uns sind. Wir freuen uns, wenn Sie sich die Zeit nehmen und uns Ihre Eindrücke mitteilen.\n' . 'Ihre Anregungen landen über den direkten Weg beim Qualitäts- und Risikomanagement.\n\n' . 'Wir freuen uns über Ihr Feedback - direkt ans Qualitäts- und Risikomanagement.\n\n' . 'https://umfragetool.ukbonn.de/login?id=QMBEFR_TEST \n\n' . 'Ihr UKB.' }, 'minimumNumberOfMessageParts': 1, 'maximumNumberOfMessageParts': 8, 'reference': 'UKB' }] }}" ]); // 4. Check the API response status if ($response->getStatusCode() === 200) { // 5. If successful, update the row in the database $phoneContact->setContacted(true); $phoneContact->setGatewayResponse(trim($response->getStatusCode() . ' ' . $response->getContent())); $phoneContact->setContactedAt(new DateTimeImmutable()); $this->entityManager->persist($phoneContact); $io->note(sprintf('Successfully contacted number %s', $phoneContact->getPhoneNumber())); } else { $io->warning(sprintf('API call for number %s failed with status code %s', $phoneContact->getPhoneNumber(), $response->getStatusCode())); } } catch (Exception $e) { $io->error(sprintf('An error occurred contacting the API for number %s: %s', $phoneContact->getPhoneNumber(), $e->getMessage())); } } // Finalize all database changes $this->entityManager->flush(); $io->progressFinish(); $io->success('Contacting process complete!'); return Command::SUCCESS; } }