bootstrap fake sms from cli

This commit is contained in:
2022-06-13 14:01:07 +02:00
parent 9e4fd6183e
commit 616be5cc8a
15 changed files with 417 additions and 2 deletions

View File

@@ -0,0 +1,190 @@
<?php
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\ShortMessage;
use Chill\MainBundle\Service\ShortMessage\ShortMessageSenderInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
use libphonenumber\ValidationResult;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class SendTestShortMessageOnCalendarCommand extends Command
{
private PersonRepository $personRepository;
private PhoneNumberUtil $phoneNumberUtil;
private PhoneNumberHelperInterface $phoneNumberHelper;
private ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder;
private ShortMessageSenderInterface $messageSender;
private UserRepositoryInterface $userRepository;
/**
* @param PersonRepository $personRepository
* @param PhoneNumberUtil $phoneNumberUtil
* @param PhoneNumberHelperInterface $phoneNumberHelper
* @param ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder
* @param ShortMessageSenderInterface $messageSender
*/
public function __construct(
PersonRepository $personRepository,
PhoneNumberUtil $phoneNumberUtil,
PhoneNumberHelperInterface $phoneNumberHelper,
ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder,
ShortMessageSenderInterface $messageSender,
UserRepositoryInterface $userRepository
) {
parent::__construct();
$this->personRepository = $personRepository;
$this->phoneNumberUtil = $phoneNumberUtil;
$this->phoneNumberHelper = $phoneNumberHelper;
$this->messageForCalendarBuilder = $messageForCalendarBuilder;
$this->messageSender = $messageSender;
$this->userRepository = $userRepository;
}
public function getName()
{
return 'chill:calendar:test-send-short-message';
}
protected function configure()
{
$this->setDescription('Test sending a SMS for a dummy calendar appointment');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$calendar = new Calendar();
$calendar->setSendSMS(true);
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
// start date
$question = new Question("When will start the appointment ? (default: \"1 hour\") ", '1 hour');
$startDate = new \DateTimeImmutable($helper->ask($input, $output, $question));
if (false === $startDate) {
throw new \UnexpectedValueException("could not create a date with this date and time");
}
$calendar->setStartDate($startDate);
// end date
$question = new Question("How long will last the appointment ? (default: \"PT30M\") ", 'PT30M');
$interval = new \DateInterval($helper->ask($input, $output, $question));
if (false === $interval) {
throw new \UnexpectedValueException("could not create the interval");
}
$calendar->setEndDate($calendar->getStartDate()->add($interval));
// a person
$question = new Question("Who will participate ? Give an id for a person. ");
$question
->setValidator(function ($answer): Person {
if (!is_numeric($answer)) {
throw new \UnexpectedValueException('the answer must be numeric');
}
if (0 >= (int) $answer) {
throw new \UnexpectedValueException('the answer must be greater than zero');
}
$person = $this->personRepository->find((int) $answer);
if (null === $person) {
throw new \UnexpectedValueException("The person is not found");
}
return $person;
});
$person = $helper->ask($input, $output, $question);
$calendar->addPerson($person);
// a main user
$question = new Question("Who will be the main user ? Give an id for a user. ");
$question
->setValidator(function ($answer): User {
if (!is_numeric($answer)) {
throw new \UnexpectedValueException('the answer must be numeric');
}
if (0 >= (int) $answer) {
throw new \UnexpectedValueException('the answer must be greater than zero');
}
$user = $this->userRepository->find((int) $answer);
if (null === $user) {
throw new \UnexpectedValueException("The user is not found");
}
return $user;
});
$user = $helper->ask($input, $output, $question);
$calendar->setMainUser($user);
// phonenumber
$question = new Question("To which number are we going to send this fake message ?",
null !== $person->getMobilenumber() ?
$this->phoneNumberHelper->format($person->getMobilenumber()):
null
);
$question->setNormalizer(function ($answer): PhoneNumber {
if (null === $answer) {
throw new \UnexpectedValueException("The person is not found");
}
$phone = $this->phoneNumberUtil->parse($answer, 'BE');
if (!$this->phoneNumberUtil->isPossibleNumberForType($phone, PhoneNumberType::MOBILE)) {
throw new \UnexpectedValueException("Phone number si not a mobile");
}
return $phone;
});
$phone = $helper->ask($input, $output, $question);
$messages = $this->messageForCalendarBuilder->buildMessageForCalendar($calendar);
if (0 === count($messages)) {
$output->writeln('no message to send to this user');
}
foreach ($messages as $key => $message) {
$output->writeln("The short message for SMS ${key} will be: ");
$output->writeln($message->getContent());
$message->setPhoneNumber($phone);
$this->messageSender->send($message);
}
return 0;
}
}