From 594ed4a5b4458b7bbd30bd690389cee3010e964a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 17 Jan 2025 13:25:38 +0100 Subject: [PATCH] =?UTF-8?q?Replace=20custom=20ShortMessage=20usage=20with?= =?UTF-8?q?=20Symfony=E2=80=99s=20SmsMessage.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switched the entire short message notification system to leverage Symfony's Notifier component and its TexterInterface with SmsMessage. This update simplifies the implementation, removes custom short message handling, and aligns with Symfony's standardized approach. --- config/packages/notifier.yaml | 2 +- .../SendTestShortMessageOnCalendarCommand.php | 18 ++++++------ .../BulkCalendarShortMessageSender.php | 12 ++++++-- .../DefaultShortMessageForCalendarBuilder.php | 28 +++++++++++++------ ...hortMessageForCalendarBuilderInterface.php | 4 +-- .../BulkCalendarShortMessageSenderTest.php | 20 ++++++------- ...aultShortMessageForCalendarBuilderTest.php | 11 +++----- .../CompilerPass/ShortMessageCompilerPass.php | 1 + 8 files changed, 54 insertions(+), 42 deletions(-) diff --git a/config/packages/notifier.yaml b/config/packages/notifier.yaml index ce26c77c4..158c4be03 100644 --- a/config/packages/notifier.yaml +++ b/config/packages/notifier.yaml @@ -1,7 +1,7 @@ framework: notifier: texter_transports: - twilio: '%env(resolve:SHORT_MESSAGE_DSN)%' + ovhcloud: '%env(SHORT_MESSAGE_DSN)%' channel_policy: # use chat/slack, chat/telegram, sms/twilio or sms/nexmo urgent: ['email'] diff --git a/src/Bundle/ChillCalendarBundle/Command/SendTestShortMessageOnCalendarCommand.php b/src/Bundle/ChillCalendarBundle/Command/SendTestShortMessageOnCalendarCommand.php index 80ee424b8..34e20346b 100644 --- a/src/Bundle/ChillCalendarBundle/Command/SendTestShortMessageOnCalendarCommand.php +++ b/src/Bundle/ChillCalendarBundle/Command/SendTestShortMessageOnCalendarCommand.php @@ -21,9 +21,7 @@ namespace Chill\CalendarBundle\Command; use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Service\ShortMessageNotification\ShortMessageForCalendarBuilderInterface; use Chill\MainBundle\Entity\User; -use Chill\MainBundle\Phonenumber\PhoneNumberHelperInterface; use Chill\MainBundle\Repository\UserRepositoryInterface; -use Chill\MainBundle\Service\ShortMessage\ShortMessageTransporterInterface; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Repository\PersonRepository; use libphonenumber\PhoneNumber; @@ -36,6 +34,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; +use Symfony\Component\Notifier\TexterInterface; class SendTestShortMessageOnCalendarCommand extends Command { @@ -44,9 +43,8 @@ class SendTestShortMessageOnCalendarCommand extends Command public function __construct( private readonly PersonRepository $personRepository, private readonly PhoneNumberUtil $phoneNumberUtil, - private readonly PhoneNumberHelperInterface $phoneNumberHelper, private readonly ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder, - private readonly ShortMessageTransporterInterface $transporter, + private readonly TexterInterface $transporter, private readonly UserRepositoryInterface $userRepository, ) { parent::__construct('chill:calendar:test-send-short-message'); @@ -152,10 +150,6 @@ class SendTestShortMessageOnCalendarCommand extends Command return $phone; }); - $phone = $helper->ask($input, $output, $question); - - $question = new ConfirmationQuestion('really send the message to the phone ?'); - $reallySend = (bool) $helper->ask($input, $output, $question); $messages = $this->messageForCalendarBuilder->buildMessageForCalendar($calendar); @@ -165,8 +159,12 @@ class SendTestShortMessageOnCalendarCommand extends Command foreach ($messages as $key => $message) { $output->writeln("The short message for SMS {$key} will be: "); - $output->writeln($message->getContent()); - $message->setPhoneNumber($phone); + $output->writeln($message->getSubject()); + $output->writeln('The destination number will be:'); + $output->writeln($message->getPhone()); + + $question = new ConfirmationQuestion('really send the message to the phone ?'); + $reallySend = (bool) $helper->ask($input, $output, $question); if ($reallySend) { $this->transporter->send($message); diff --git a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/BulkCalendarShortMessageSender.php b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/BulkCalendarShortMessageSender.php index 9a4a92a94..7053d905a 100644 --- a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/BulkCalendarShortMessageSender.php +++ b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/BulkCalendarShortMessageSender.php @@ -21,11 +21,17 @@ namespace Chill\CalendarBundle\Service\ShortMessageNotification; use Chill\CalendarBundle\Entity\Calendar; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; -use Symfony\Component\Messenger\MessageBusInterface; +use Symfony\Component\Notifier\TexterInterface; class BulkCalendarShortMessageSender { - public function __construct(private readonly CalendarForShortMessageProvider $provider, private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, private readonly MessageBusInterface $messageBus, private readonly ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder) {} + public function __construct( + private readonly CalendarForShortMessageProvider $provider, + private readonly EntityManagerInterface $em, + private readonly LoggerInterface $logger, + private readonly TexterInterface $texter, + private readonly ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder, + ) {} public function sendBulkMessageToEligibleCalendars() { @@ -36,7 +42,7 @@ class BulkCalendarShortMessageSender $smses = $this->messageForCalendarBuilder->buildMessageForCalendar($calendar); foreach ($smses as $sms) { - $this->messageBus->dispatch($sms); + $this->texter->send($sms); ++$countSms; } diff --git a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilder.php b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilder.php index 880c9950f..dfce0548c 100644 --- a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilder.php +++ b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilder.php @@ -19,12 +19,26 @@ declare(strict_types=1); namespace Chill\CalendarBundle\Service\ShortMessageNotification; use Chill\CalendarBundle\Entity\Calendar; -use Chill\MainBundle\Service\ShortMessage\ShortMessage; +use libphonenumber\PhoneNumberFormat; +use libphonenumber\PhoneNumberUtil; +use Symfony\Component\Notifier\Message\SmsMessage; class DefaultShortMessageForCalendarBuilder implements ShortMessageForCalendarBuilderInterface { - public function __construct(private readonly \Twig\Environment $engine) {} + private readonly PhoneNumberUtil $phoneUtil; + public function __construct(private readonly \Twig\Environment $engine) + { + $this->phoneUtil = PhoneNumberUtil::getInstance(); + } + + /** + * @return list + * + * @throws \Twig\Error\LoaderError + * @throws \Twig\Error\RuntimeError + * @throws \Twig\Error\SyntaxError + */ public function buildMessageForCalendar(Calendar $calendar): array { if (true !== $calendar->getSendSMS()) { @@ -39,16 +53,14 @@ class DefaultShortMessageForCalendarBuilder implements ShortMessageForCalendarBu } if (Calendar::SMS_PENDING === $calendar->getSmsStatus()) { - $toUsers[] = new ShortMessage( + $toUsers[] = new SmsMessage( + $this->phoneUtil->format($person->getMobilenumber(), PhoneNumberFormat::E164), $this->engine->render('@ChillCalendar/CalendarShortMessage/short_message.txt.twig', ['calendar' => $calendar]), - $person->getMobilenumber(), - ShortMessage::PRIORITY_LOW ); } elseif (Calendar::SMS_CANCEL_PENDING === $calendar->getSmsStatus()) { - $toUsers[] = new ShortMessage( + $toUsers[] = new SmsMessage( + $this->phoneUtil->format($person->getMobilenumber(), PhoneNumberFormat::E164), $this->engine->render('@ChillCalendar/CalendarShortMessage/short_message_canceled.txt.twig', ['calendar' => $calendar]), - $person->getMobilenumber(), - ShortMessage::PRIORITY_LOW ); } } diff --git a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/ShortMessageForCalendarBuilderInterface.php b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/ShortMessageForCalendarBuilderInterface.php index 65856a437..6aa21247d 100644 --- a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/ShortMessageForCalendarBuilderInterface.php +++ b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/ShortMessageForCalendarBuilderInterface.php @@ -19,12 +19,12 @@ declare(strict_types=1); namespace Chill\CalendarBundle\Service\ShortMessageNotification; use Chill\CalendarBundle\Entity\Calendar; -use Chill\MainBundle\Service\ShortMessage\ShortMessage; +use Symfony\Component\Notifier\Message\SmsMessage; interface ShortMessageForCalendarBuilderInterface { /** - * @return array|ShortMessage[] + * @return list */ public function buildMessageForCalendar(Calendar $calendar): array; } diff --git a/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/BulkCalendarShortMessageSenderTest.php b/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/BulkCalendarShortMessageSenderTest.php index 09c9ddf96..dd86ab16b 100644 --- a/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/BulkCalendarShortMessageSenderTest.php +++ b/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/BulkCalendarShortMessageSenderTest.php @@ -23,17 +23,16 @@ use Chill\CalendarBundle\Service\ShortMessageNotification\BulkCalendarShortMessa use Chill\CalendarBundle\Service\ShortMessageNotification\CalendarForShortMessageProvider; use Chill\CalendarBundle\Service\ShortMessageNotification\ShortMessageForCalendarBuilderInterface; use Chill\MainBundle\Entity\User; -use Chill\MainBundle\Service\ShortMessage\ShortMessage; use Chill\MainBundle\Test\PrepareUserTrait; use Chill\PersonBundle\DataFixtures\Helper\PersonRandomHelper; use Doctrine\ORM\EntityManagerInterface; -use libphonenumber\PhoneNumberUtil; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Psr\Log\NullLogger; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; -use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\MessageBusInterface; +use Symfony\Component\Notifier\Message\SentMessage; +use Symfony\Component\Notifier\Message\SmsMessage; +use Symfony\Component\Notifier\TexterInterface; /** * @internal @@ -101,24 +100,23 @@ final class BulkCalendarShortMessageSenderTest extends KernelTestCase $messageBuilder->buildMessageForCalendar(Argument::type(Calendar::class)) ->willReturn( [ - new ShortMessage( + new SmsMessage( + '+32470123456', 'content', - PhoneNumberUtil::getInstance()->parse('+32470123456', 'BE'), - ShortMessage::PRIORITY_MEDIUM ), ] ); - $bus = $this->prophesize(MessageBusInterface::class); - $bus->dispatch(Argument::type(ShortMessage::class)) - ->willReturn(new Envelope(new \stdClass())) + $texter = $this->prophesize(TexterInterface::class); + $texter->send(Argument::type(SmsMessage::class)) + ->will(fn ($args): SentMessage => new SentMessage($args[0], 'sms')) ->shouldBeCalledTimes(1); $bulk = new BulkCalendarShortMessageSender( $provider->reveal(), $em, new NullLogger(), - $bus->reveal(), + $texter->reveal(), $messageBuilder->reveal() ); diff --git a/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilderTest.php b/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilderTest.php index 222d3a451..f5b9c8fdd 100644 --- a/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilderTest.php +++ b/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/DefaultShortMessageForCalendarBuilderTest.php @@ -23,7 +23,6 @@ use Chill\CalendarBundle\Service\ShortMessageNotification\DefaultShortMessageFor use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Entity\Person; -use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberUtil; use PHPUnit\Framework\TestCase; use Prophecy\Argument; @@ -90,10 +89,9 @@ final class DefaultShortMessageForCalendarBuilderTest extends TestCase $this->assertCount(1, $sms); $this->assertEquals( '+32470123456', - $this->phoneNumberUtil->format($sms[0]->getPhoneNumber(), PhoneNumberFormat::E164) + $sms[0]->getPhone() ); - $this->assertEquals('message content', $sms[0]->getContent()); - $this->assertEquals('low', $sms[0]->getPriority()); + $this->assertEquals('message content', $sms[0]->getSubject()); // if the calendar is canceled $calendar @@ -105,9 +103,8 @@ final class DefaultShortMessageForCalendarBuilderTest extends TestCase $this->assertCount(1, $sms); $this->assertEquals( '+32470123456', - $this->phoneNumberUtil->format($sms[0]->getPhoneNumber(), PhoneNumberFormat::E164) + $sms[0]->getRecipientId(), ); - $this->assertEquals('message canceled', $sms[0]->getContent()); - $this->assertEquals('low', $sms[0]->getPriority()); + $this->assertEquals('message canceled', $sms[0]->getSubject()); } } diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/CompilerPass/ShortMessageCompilerPass.php b/src/Bundle/ChillMainBundle/DependencyInjection/CompilerPass/ShortMessageCompilerPass.php index 9da9154b3..5a974c926 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/CompilerPass/ShortMessageCompilerPass.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/CompilerPass/ShortMessageCompilerPass.php @@ -33,6 +33,7 @@ class ShortMessageCompilerPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { + return; $config = $container->resolveEnvPlaceholders($container->getParameter('chill_main.short_messages'), true); // weird fix for special characters $config['dsn'] = str_replace(['%%'], ['%'], (string) $config['dsn']);