diff --git a/src/Command/ContactUncontactedCommand.php b/src/Command/ContactUncontactedCommand.php index 217686d..db72c23 100644 --- a/src/Command/ContactUncontactedCommand.php +++ b/src/Command/ContactUncontactedCommand.php @@ -31,8 +31,8 @@ class ContactUncontactedCommand extends Command private string $sms_gateway_api_key; public function __construct(EntityManagerInterface $entityManager, - HttpClientInterface $httpClient, - string $sms_gateway_api_key) + HttpClientInterface $httpClient, + string $sms_gateway_api_key) { parent::__construct(); $this->entityManager = $entityManager; @@ -69,9 +69,12 @@ class ContactUncontactedCommand extends Command $io->progressStart(count($phoneNumbersToContact)); + $countSent = 0; + $countTotal = 0; // 2. Loop through each eligible phone number foreach ($phoneNumbersToContact as $phoneContact) { $io->progressAdvance(); + $countTotal++; // 3. Contact the HTTP REST API try { @@ -111,11 +114,42 @@ class ContactUncontactedCommand extends Command } else { $io->warning(sprintf('API call for number %s failed with status code %s', $phoneContact->getPhoneNumber(), $response->getStatusCode())); } + $countSent++; } catch (Exception $e) { $io->error(sprintf('An error occurred contacting the API for number %s: %s', $phoneContact->getPhoneNumber(), $e->getMessage())); } } + try { + $responseStatus = $this->httpClient->request('POST', self::$apiEndpoint, [ + 'headers' => [ + 'X-CM-PRODUCTTOKEN' => $this->sms_gateway_api_key, + 'Accept' => 'application/json', + 'Content-Type' => 'application/json' + ], + + 'body' => "{ + 'messages': { 'msg': [{ + 'allowedChannels': ['SMS'], + 'from': 'UKB', + 'to': [ + { 'number': '004917672121270' } + ], + 'body': { + 'type': 'auto', + 'content': 'SMS-Versand-Update: Insg. {$countSent} versendet. Total versucht: {$countTotal}.' + }, + 'minimumNumberOfMessageParts': 1, + 'maximumNumberOfMessageParts': 8, + 'reference': 'UKB' + }] }}" + + ]); + } catch (Exception $e) { + $io->error(sprintf('An error occurred contacting the API for number %s: %s', '004917672121270', $e->getMessage())); + } + + // Finalize all database changes $this->entityManager->flush(); $io->progressFinish();