sms_scheduler/src/Command/ContactUncontactedCommand.php
Marko Jovanovic 2d265c76d2 Bugfix
2025-10-02 16:23:42 +02:00

112 lines
4.4 KiB
PHP

<?php
namespace App\Command;
use App\Entity\Contacts;
use App\Entity\PhoneNumber;
use DateTime;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Contracts\HttpClient\HttpClientInterface;
// Assuming you have an entity for your table
class ContactUncontactedCommand extends Command
{
// The command's name, used to run it from the console (e.g., bin/console app:contact-uncontacted)
protected static $defaultName = 'app:contact-uncontacted';
protected static $defaultDescription = 'Sends out SMS messages.';
protected static $apiEndpoint = 'https://gw.cmtelecom.com/gateway.ashx';
// Inject the EntityManager and HttpClient as dependencies
private EntityManagerInterface $entityManager;
private HttpClientInterface $httpClient;
private string $sms_gateway_api_key;
public function __construct(EntityManagerInterface $entityManager,
HttpClientInterface $httpClient,
string $sms_gateway_api_key)
{
parent::__construct();
$this->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('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' => $phoneContact->getPhoneNumber(),
'from' => 'UKB',
'reference' => 'Test-Reference'
]
]);
// 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;
}
}